Files
acms/frontend/app/Page/NewCollection.hs
2024-06-08 09:56:20 +02:00

63 lines
1.5 KiB
Haskell

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 = ""}))
data Action
= NoOp
| FormChanged T.Text
| FormSubmitted T.Text
| CollectionCreated (Either String ())
deriving (Eq, Show)
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 =
( m <# do
pure NoOp <* consoleLog (toMisoString err),
[]
)
updateModel (CollectionCreated (Right _)) m = (noEff m, [E.ReloadCollections])
viewModel :: Model -> View Action
viewModel m = do
div_ [] $
[ h3_ [] [text "new collection"],
either FormChanged 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"