Generation of entity form - set foundations. (#61)

This commit is contained in:
Matija Sosic 2020-01-09 10:58:07 +01:00 committed by GitHub
parent 068c4ac60e
commit 0a5a746a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 8 deletions

View File

@ -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
}
}

View File

@ -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

View File

@ -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
View 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
]

View File

@ -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
View 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` []