aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Page/EditValue.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-05 10:41:02 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-05 10:41:02 +0200
commita7a4dd01127506dba991cc5f3f39c4a370fff699 (patch)
tree086c6b306a1bbfb9cd13a727bc2284894991f24d /frontend/app/Page/EditValue.hs
parentd5f3f2333a4a167054c0a8556dfd8cd87f955595 (diff)
add edit page
Diffstat (limited to 'frontend/app/Page/EditValue.hs')
-rw-r--r--frontend/app/Page/EditValue.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/frontend/app/Page/EditValue.hs b/frontend/app/Page/EditValue.hs
new file mode 100644
index 0000000..294d1c9
--- /dev/null
+++ b/frontend/app/Page/EditValue.hs
@@ -0,0 +1,66 @@
+module Page.EditValue
+ ( Model,
+ initialModel,
+ Action,
+ updateModel,
+ viewModel,
+ )
+where
+
+import Api
+import Data.Aeson qualified as A
+import Data.Aeson.KeyMap qualified as AM
+import Data.Maybe
+import Form qualified as F
+import Miso
+import Miso.String (toMisoString)
+import Schema
+
+data Model = Model
+ { collection :: String,
+ fileName :: String,
+ input :: Maybe A.Value,
+ schema :: Schema
+ }
+ deriving (Show, Eq)
+
+initialModel :: String -> String -> JSM (Either String Model)
+initialModel collection fileName = do
+ schema' <- fetchSchema
+ input' <- fetchPost fileName
+ pure do
+ schema <- schema'
+ input <- input'
+ pure $ Model {..}
+
+data Action
+ = NoOp
+ | FormChanged A.Value
+ | FormSubmitted A.Value
+ | EntityWritten (Either String ())
+ deriving (Eq, Show)
+
+updateModel :: Action -> Model -> Effect Action Model
+updateModel NoOp m = noEff m
+updateModel (FormChanged (Just -> input)) m = noEff m {input}
+updateModel (FormSubmitted output) m =
+ m <# do EntityWritten <$> updatePost m.fileName output
+updateModel (EntityWritten _) m = noEff m
+
+viewModel :: Model -> View Action
+viewModel m = do
+ let input = (fromMaybe (A.Object AM.empty) m.input)
+ div_ [] $
+ [ viewForm input m.schema,
+ viewInput 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))]