diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-06-04 14:36:26 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-06-04 14:46:41 +0200 |
commit | ed753b04104b27a9b258f174a501e2ae058b41ee (patch) | |
tree | ddddddddbe2e92e13140926eea7193f093383754 /frontend/app/Page/ListCollection.hs | |
parent | 03b019ca96ceb83113a5ea34f4b48b039c56f02d (diff) |
refactor pages
Diffstat (limited to 'frontend/app/Page/ListCollection.hs')
-rw-r--r-- | frontend/app/Page/ListCollection.hs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/frontend/app/Page/ListCollection.hs b/frontend/app/Page/ListCollection.hs new file mode 100644 index 0000000..102973e --- /dev/null +++ b/frontend/app/Page/ListCollection.hs @@ -0,0 +1,70 @@ +module Page.ListCollection + ( Model, + initialModel, + Action, + updateModel, + viewModel, + ) +where + +import Api +import Data.Aeson qualified as A +import Data.Aeson.KeyMap qualified as AM +import Form qualified as F +import Miso +import Miso.String (toMisoString) +import Schema + +data Model = Model + { collection :: String, + input :: A.Value, + schema :: Schema, + posts :: [A.Value] + } + deriving (Show, Eq) + +initialModel :: String -> JSM (Either String Model) +initialModel collection = do + schema' <- fetchSchema + posts' <- fetchPosts + pure do + schema <- schema' + posts <- posts' + pure $ Model {input = A.Object AM.empty, ..} + +data Action + = NoOp + | FormChanged A.Value + | FormSubmitted A.Value + deriving (Eq, Show) + +updateModel :: Action -> Model -> Effect Action Model +updateModel NoOp m = noEff m +updateModel (FormChanged input) m = noEff m {input} +updateModel (FormSubmitted output) m = + m <# do + const NoOp <$> consoleLog (toMisoString (A.encode output)) + +viewModel :: Model -> View Action +viewModel m = + div_ [] $ + [ viewSchema m.schema, + viewPosts m.posts, + viewForm m.input m.schema, + viewInput m.input + ] + +viewForm :: A.Value -> Schema -> View Action +viewForm input = + fmap (either FormChanged FormSubmitted) + . flip F.runForm input + . schemaForm + +viewInput :: A.Value -> View Action +viewInput input = + pre_ [] [text (toMisoString (A.encode input))] + +viewPosts :: [A.Value] -> View Action +viewPosts posts = ol_ [] (viewPost <$> posts) + where + viewPost post = pre_ [] [text (toMisoString (A.encode post))] |