mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-11-27 14:55:20 +03:00
Generation of entity form - set foundations. (#61)
This commit is contained in:
parent
068c4ac60e
commit
0a5a746a83
@ -52,3 +52,11 @@ page Main {
|
||||
</div>
|
||||
jsx=}
|
||||
}
|
||||
|
||||
// NOTE(matija): this is only being parsed for now, no code is generated yet.
|
||||
// Entity form definition.
|
||||
entity-form<Task> CreateTaskForm {
|
||||
submit: {
|
||||
onEnter: false
|
||||
}
|
||||
}
|
||||
|
@ -55,10 +55,13 @@ generateEntityActions :: Wasp -> Entity -> FileDraft
|
||||
generateEntityActions wasp entity
|
||||
= createSimpleEntityFileDraft wasp entity (entityActionsPathInSrc entity) "actions.js"
|
||||
|
||||
-- TODO(matija): currently we are generating these components automatically, as soon as the
|
||||
-- entity is defined. Now we are changing this and will generate them on-demand, depending on
|
||||
-- what is in Wasp.
|
||||
generateEntityComponents :: Wasp -> Entity -> [FileDraft]
|
||||
generateEntityComponents wasp entity =
|
||||
[ generateEntityCreateForm wasp entity
|
||||
, generateEntityList wasp entity
|
||||
generateEntityComponents wasp entity = concat
|
||||
[ generateEntityCreateFormsForEntity wasp entity
|
||||
-- TODO: , generateEntityLists wasp entity
|
||||
]
|
||||
|
||||
-- TODO: add tests / update tests.
|
||||
@ -74,10 +77,20 @@ generateEntityComponents wasp entity =
|
||||
-- ... Code when field is boolean. ...
|
||||
-- {=/ booleanField =}
|
||||
-- {=/ typedFields =}
|
||||
generateEntityCreateForm :: Wasp -> Entity -> FileDraft
|
||||
generateEntityCreateForm wasp entity
|
||||
= createSimpleEntityFileDraft wasp entity (entityCreateFormPathInSrc entity)
|
||||
("components" </> "CreateForm.js")
|
||||
--generateEntityCreateForm :: Wasp -> Entity -> FileDraft
|
||||
--generateEntityCreateForm wasp entity
|
||||
-- = createSimpleEntityFileDraft wasp entity (entityCreateFormPathInSrc entity)
|
||||
-- ("components" </> "CreateForm.js")
|
||||
-- TODO(matija): in the next PR
|
||||
generateEntityCreateForm :: Wasp -> EntityForm -> FileDraft
|
||||
-- TODO(matija): if there is no entity for the given form, should throw an error.
|
||||
generateEntityCreateForm wasp entityForm = undefined
|
||||
|
||||
generateEntityCreateFormsForEntity :: Wasp -> Entity -> [FileDraft]
|
||||
generateEntityCreateFormsForEntity wasp entity =
|
||||
map (generateEntityCreateForm wasp) entityForms
|
||||
where
|
||||
entityForms = getEntityFormsForEntity wasp entity
|
||||
|
||||
-- TODO(matija): do I need wasp at all?
|
||||
-- | Generates list component for the specified entity, so user can see all the
|
||||
|
@ -24,6 +24,7 @@ module Wasp
|
||||
|
||||
, EntityForm (..)
|
||||
, EntityFormSubmitConfig (..)
|
||||
, getEntityFormsForEntity
|
||||
) where
|
||||
|
||||
import Data.Aeson ((.=), object, ToJSON(..))
|
||||
@ -145,6 +146,13 @@ data EntityFormSubmitConfig = EntityFormSubmitConfig
|
||||
{ onEnter :: !Bool
|
||||
} deriving (Show, Eq)
|
||||
|
||||
-- | Retrieves all entity forms for a given entity from a Wasp record.
|
||||
getEntityFormsForEntity :: Wasp -> Entity -> [EntityForm]
|
||||
getEntityFormsForEntity wasp entity = filter isFormOfGivenEntity allEntityForms
|
||||
where
|
||||
allEntityForms = [entityForm | (WaspElementEntityForm entityForm) <- waspElements wasp]
|
||||
isFormOfGivenEntity ef = entityName entity == efEntityName ef
|
||||
|
||||
-- * ToJSON instances.
|
||||
|
||||
-- NOTE(martin): Here I define general transformation of App into JSON that I can then easily use
|
||||
|
27
test/Fixtures.hs
Normal file
27
test/Fixtures.hs
Normal file
@ -0,0 +1,27 @@
|
||||
module Fixtures where
|
||||
|
||||
import Wasp
|
||||
|
||||
taskEntity :: Entity
|
||||
taskEntity = Entity
|
||||
{ entityName = "Task"
|
||||
, entityFields =
|
||||
[ Wasp.EntityField "description" Wasp.EftString
|
||||
, Wasp.EntityField "isDone" Wasp.EftBoolean
|
||||
]
|
||||
}
|
||||
|
||||
taskCreateForm :: EntityForm
|
||||
taskCreateForm = EntityForm
|
||||
{ efName = "CreateTaskForm"
|
||||
, efEntityName = "Task"
|
||||
, efSubmitConfig = Just EntityFormSubmitConfig
|
||||
{ onEnter = False
|
||||
}
|
||||
}
|
||||
|
||||
wasp :: Wasp
|
||||
wasp = fromWaspElems
|
||||
[ WaspElementEntity taskEntity
|
||||
, WaspElementEntityForm taskCreateForm
|
||||
]
|
@ -21,7 +21,9 @@ spec_parseEntityForm = do
|
||||
`shouldBe` Right Wasp.EntityForm
|
||||
{ Wasp.efName = "someEntityForm"
|
||||
, Wasp.efEntityName = "Task"
|
||||
, Wasp.efSubmitConfig = Just (Wasp.EntityFormSubmitConfig { Wasp.onEnter = True })
|
||||
, Wasp.efSubmitConfig = Just (
|
||||
Wasp.EntityFormSubmitConfig { Wasp.onEnter = True }
|
||||
)
|
||||
}
|
||||
|
||||
it "When given an entity form without submit config, it is not included in the result." $ do
|
||||
|
25
test/WaspTest.hs
Normal file
25
test/WaspTest.hs
Normal file
@ -0,0 +1,25 @@
|
||||
module WaspTest where
|
||||
|
||||
import Test.Tasty.Hspec
|
||||
|
||||
import Wasp
|
||||
import qualified Fixtures as F
|
||||
|
||||
spec_getEntityFormsForEntity :: Spec
|
||||
spec_getEntityFormsForEntity = do
|
||||
|
||||
let waspWithEntityForm = fromWaspElems
|
||||
[ WaspElementEntity F.taskEntity
|
||||
, WaspElementEntityForm F.taskCreateForm
|
||||
]
|
||||
|
||||
let waspWithoutEntityForm = fromWaspElems
|
||||
[ WaspElementEntity F.taskEntity
|
||||
]
|
||||
|
||||
it "When given Wasp record which contains an entity-form for a given entity, returns it." $ do
|
||||
getEntityFormsForEntity waspWithEntityForm F.taskEntity
|
||||
`shouldBe` [F.taskCreateForm]
|
||||
|
||||
it "When given Wasp record without an entity-form for a given entity, returns Nothing." $ do
|
||||
getEntityFormsForEntity waspWithoutEntityForm F.taskEntity `shouldBe` []
|
Loading…
Reference in New Issue
Block a user