add collections

This commit is contained in:
Alexander Foremny
2024-06-07 16:14:52 +02:00
parent 79dd6af899
commit 8a34cc822c
5 changed files with 74 additions and 46 deletions

View File

@@ -2,6 +2,7 @@
module Api
( fetchCollections,
createCollection,
fetchSchema,
fetchSchemaVersion,
fetchPosts,
@@ -33,6 +34,15 @@ fetchCollections :: JSM (Either String [String])
fetchCollections =
A.eitherDecode <$> fetch (fromString "http://localhost:8081/collections")
createCollection :: String -> JSM (Either String ())
createCollection collection =
A.eitherDecode
<$> fetch
( fromString "http://localhost:8081/collections"
& setRequestMethod "POST"
& setRequestBodyLBS (A.encode (A.toJSON collection))
)
fetchSchemaVersion :: JSM (Either String Version)
fetchSchemaVersion =
A.eitherDecode <$> fetch (fromString "http://localhost:8081/schemaVersion")

View File

@@ -7,9 +7,12 @@ module Page.NewCollection
)
where
import Api
import Data.Aeson qualified as A
import Data.Text qualified as T
import Form qualified as F
import Miso
import Miso.String (toMisoString)
data Model = Model
{ input :: T.Text
@@ -24,19 +27,29 @@ data Action
= NoOp
| FormChanged T.Text
| FormSubmitted T.Text
| CollectionCreated (Either String ())
deriving (Eq, Show)
updateModel :: Action -> Model -> Effect Action Model
updateModel NoOp m = noEff m
updateModel (FormChanged input) m = noEff m {input}
updateModel (FormSubmitted _) m = noEff m
updateModel (FormSubmitted collection) m =
m <# do
CollectionCreated <$> createCollection (T.unpack collection)
updateModel (CollectionCreated (Left err)) m =
m <# do
pure NoOp <* consoleLog (toMisoString err)
-- TODO reload collections in main app
updateModel (CollectionCreated (Right _)) m = noEff m
viewModel :: Model -> View Action
viewModel m = do
div_ [] $
[ h3_ [] [text "new collection"],
either FormChanged FormSubmitted
<$> F.runForm collectionForm m.input
<$> F.runForm collectionForm m.input,
pre_ [] [text (toMisoString (A.encode m.input))],
pre_ [] [text (toMisoString (A.encode (collectionForm.fill m.input)))]
]
collectionForm :: F.Form T.Text T.Text