From 0a5a746a8305ec3a2ad067b97ba6cf1d9ee71727 Mon Sep 17 00:00:00 2001 From: Matija Sosic Date: Thu, 9 Jan 2020 10:58:07 +0100 Subject: [PATCH] Generation of entity form - set foundations. (#61) --- examples/todoMVC/todoMVC.wasp | 8 ++++++++ src/Generator/EntityGenerator.hs | 27 ++++++++++++++++++++------- src/Wasp.hs | 8 ++++++++ test/Fixtures.hs | 27 +++++++++++++++++++++++++++ test/Parser/EntityFormTest.hs | 4 +++- test/WaspTest.hs | 25 +++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 test/Fixtures.hs create mode 100644 test/WaspTest.hs diff --git a/examples/todoMVC/todoMVC.wasp b/examples/todoMVC/todoMVC.wasp index 05dd6892d..241ba4e1f 100644 --- a/examples/todoMVC/todoMVC.wasp +++ b/examples/todoMVC/todoMVC.wasp @@ -52,3 +52,11 @@ page Main { jsx=} } + +// NOTE(matija): this is only being parsed for now, no code is generated yet. +// Entity form definition. +entity-form CreateTaskForm { + submit: { + onEnter: false + } +} diff --git a/src/Generator/EntityGenerator.hs b/src/Generator/EntityGenerator.hs index fd2843323..2a3680b53 100644 --- a/src/Generator/EntityGenerator.hs +++ b/src/Generator/EntityGenerator.hs @@ -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 diff --git a/src/Wasp.hs b/src/Wasp.hs index 22b53d931..9d7260b97 100644 --- a/src/Wasp.hs +++ b/src/Wasp.hs @@ -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 diff --git a/test/Fixtures.hs b/test/Fixtures.hs new file mode 100644 index 000000000..f1d84d942 --- /dev/null +++ b/test/Fixtures.hs @@ -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 + ] diff --git a/test/Parser/EntityFormTest.hs b/test/Parser/EntityFormTest.hs index 318ea25fb..1982049e0 100644 --- a/test/Parser/EntityFormTest.hs +++ b/test/Parser/EntityFormTest.hs @@ -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 diff --git a/test/WaspTest.hs b/test/WaspTest.hs new file mode 100644 index 000000000..1a0f12279 --- /dev/null +++ b/test/WaspTest.hs @@ -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` []