module Page.NewCollection ( Model, initialModel, Action, updateModel, viewModel, ) where import Api import Data.Aeson qualified as A import Data.Text qualified as T import Effect (Eff) import Effect qualified as E import Form qualified as F import Miso import Miso.String (toMisoString) data Model = Model { input :: T.Text } deriving (Show, Eq) initialModel :: JSM (Either String Model) initialModel = do pure (Right (Model {input = ""})) newtype Action = Action (Model -> (Effect Action Model, [Eff])) 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 update__collectionCreated <$> createCollection (T.unpack collection), [] ) 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 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)))] ] collectionForm :: F.Form T.Text T.Text collectionForm = F.input "name"