From 2e0cf98254976e443ea7f693961fc105ed6cf563 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 14 Jun 2024 20:56:07 +0200 Subject: refactor actions --- frontend/app/Page/EditValue.hs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'frontend/app/Page/EditValue.hs') 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 -- cgit v1.2.3