diff options
Diffstat (limited to 'frontend/app/Page')
-rw-r--r-- | frontend/app/Page/EditValue.hs | 27 | ||||
-rw-r--r-- | frontend/app/Page/ListCollection.hs | 8 | ||||
-rw-r--r-- | frontend/app/Page/NewCollection.hs | 32 |
3 files changed, 31 insertions, 36 deletions
diff --git a/frontend/app/Page/EditValue.hs b/frontend/app/Page/EditValue.hs index 43c6f17..cdb1dd0 100644 --- a/frontend/app/Page/EditValue.hs +++ b/frontend/app/Page/EditValue.hs @@ -12,10 +12,10 @@ import Data.Aeson qualified as A import Data.Aeson.KeyMap qualified as AM import Data.ByteString.Lazy.UTF8 as LB import Data.Maybe +import Effect (Eff) import Form qualified as F import Miso import Miso.String (toMisoString) -import Effect (Eff) import Schema data Model = Model @@ -35,19 +35,20 @@ initialModel collection fileName = do input <- input' pure $ Model {..} -data Action - = NoOp - | FormChanged A.Value - | FormSubmitted A.Value - | EntityWritten (Either String ()) - deriving (Eq, Show) +newtype Action = Action (Model -> (Effect Action Model, [Eff])) + +update__formChanged :: A.Value -> Action +update__formChanged (Just -> input) = Action $ \m -> (noEff m {input}, []) + +update__formSubmitted :: A.Value -> Action +update__formSubmitted output = Action $ \m -> + (m <# do update__entityWritten <$> updatePost m.fileName output, []) + +update__entityWritten :: Either String () -> Action +update__entityWritten _ = Action $ \m -> (noEff m, []) updateModel :: Action -> Model -> (Effect Action Model, [Eff]) -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, []) +updateModel (Action f) m = f m viewModel :: Model -> View Action viewModel m = do @@ -60,7 +61,7 @@ viewModel m = do viewForm :: A.Value -> Schema -> View Action viewForm input = - fmap (either FormChanged FormSubmitted) + fmap (either update__formChanged update__formSubmitted) . flip F.runForm input . schemaForm diff --git a/frontend/app/Page/ListCollection.hs b/frontend/app/Page/ListCollection.hs index 93ea389..9acca3c 100644 --- a/frontend/app/Page/ListCollection.hs +++ b/frontend/app/Page/ListCollection.hs @@ -10,9 +10,9 @@ where import Api import Data.Aeson qualified as A import Data.Aeson.KeyMap qualified as AM +import Effect (Eff) import Miso import Schema -import Effect (Eff) data Model = Model { collection :: String, @@ -31,12 +31,10 @@ initialModel collection = do posts <- posts' pure $ Model {input = A.Object AM.empty, ..} -data Action - = NoOp - deriving (Eq, Show) +newtype Action = Action (Model -> (Effect Action Model, [Eff])) updateModel :: Action -> Model -> (Effect Action Model, [Eff]) -updateModel NoOp m = (noEff m, []) +updateModel (Action f) m = f m viewModel :: Model -> View Action viewModel m = diff --git a/frontend/app/Page/NewCollection.hs b/frontend/app/Page/NewCollection.hs index dbc448b..12b9cf1 100644 --- a/frontend/app/Page/NewCollection.hs +++ b/frontend/app/Page/NewCollection.hs @@ -25,33 +25,29 @@ initialModel :: JSM (Either String Model) initialModel = do pure (Right (Model {input = ""})) -data Action - = NoOp - | FormChanged T.Text - | FormSubmitted T.Text - | CollectionCreated (Either String ()) - deriving (Eq, Show) +newtype Action = Action (Model -> (Effect Action Model, [Eff])) -updateModel :: Action -> Model -> (Effect Action Model, [Eff]) -updateModel NoOp m = (noEff m, []) -updateModel (FormChanged input) m = (noEff m {input}, []) -updateModel (FormSubmitted collection) m = - ( m <# do - CollectionCreated <$> createCollection (T.unpack collection), - [] - ) -updateModel (CollectionCreated (Left err)) m = +update__formChanged :: T.Text -> Action +update__formChanged input = Action $ \m -> (noEff m {input}, []) + +update__formSubmitted :: T.Text -> Action +update__formSubmitted collection = Action $ \m -> ( m <# do - pure NoOp <* consoleLog (toMisoString err), + update__collectionCreated <$> createCollection (T.unpack collection), [] ) -updateModel (CollectionCreated (Right _)) m = (noEff m, [E.ReloadCollections]) + +update__collectionCreated :: Either String () -> Action +update__collectionCreated _ = Action $ \m -> (noEff m, [E.ReloadCollections]) + +updateModel :: Action -> Model -> (Effect Action Model, [Eff]) +updateModel (Action f) m = f m viewModel :: Model -> View Action viewModel m = do div_ [] $ [ h3_ [] [text "new collection"], - either FormChanged FormSubmitted + either update__formChanged update__formSubmitted <$> F.runForm collectionForm m.input, pre_ [] [text (toMisoString (A.encode m.input))], pre_ [] [text (toMisoString (A.encode (collectionForm.fill m.input)))] |