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/NewCollection.hs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'frontend/app/Page/NewCollection.hs') 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)))] -- cgit v1.2.3