2024-06-06 23:05:41 +02:00
|
|
|
module Page.NewCollection
|
|
|
|
|
( Model,
|
|
|
|
|
initialModel,
|
|
|
|
|
Action,
|
|
|
|
|
updateModel,
|
|
|
|
|
viewModel,
|
|
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
|
2024-06-07 16:14:52 +02:00
|
|
|
import Api
|
|
|
|
|
import Data.Aeson qualified as A
|
2024-06-06 23:05:41 +02:00
|
|
|
import Data.Text qualified as T
|
2024-06-07 17:08:01 +02:00
|
|
|
import Effect (Eff)
|
|
|
|
|
import Effect qualified as E
|
2024-06-06 23:05:41 +02:00
|
|
|
import Form qualified as F
|
|
|
|
|
import Miso
|
2024-06-07 16:14:52 +02:00
|
|
|
import Miso.String (toMisoString)
|
2024-06-06 23:05:41 +02:00
|
|
|
|
|
|
|
|
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
|
2024-06-07 16:14:52 +02:00
|
|
|
| CollectionCreated (Either String ())
|
2024-06-06 23:05:41 +02:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
2024-06-07 17:08:01 +02:00
|
|
|
updateModel :: Action -> Model -> (Effect Action Model, [Eff])
|
|
|
|
|
updateModel NoOp m = (noEff m, [])
|
|
|
|
|
updateModel (FormChanged input) m = (noEff m {input}, [])
|
2024-06-07 16:14:52 +02:00
|
|
|
updateModel (FormSubmitted collection) m =
|
2024-06-07 17:08:01 +02:00
|
|
|
( m <# do
|
|
|
|
|
CollectionCreated <$> createCollection (T.unpack collection),
|
|
|
|
|
[]
|
|
|
|
|
)
|
2024-06-07 16:14:52 +02:00
|
|
|
updateModel (CollectionCreated (Left err)) m =
|
2024-06-07 17:08:01 +02:00
|
|
|
( m <# do
|
|
|
|
|
pure NoOp <* consoleLog (toMisoString err),
|
|
|
|
|
[]
|
|
|
|
|
)
|
|
|
|
|
updateModel (CollectionCreated (Right _)) m = (noEff m, [E.ReloadCollections])
|
2024-06-06 23:05:41 +02:00
|
|
|
|
|
|
|
|
viewModel :: Model -> View Action
|
|
|
|
|
viewModel m = do
|
|
|
|
|
div_ [] $
|
|
|
|
|
[ h3_ [] [text "new collection"],
|
|
|
|
|
either FormChanged FormSubmitted
|
2024-06-07 16:14:52 +02:00
|
|
|
<$> F.runForm collectionForm m.input,
|
|
|
|
|
pre_ [] [text (toMisoString (A.encode m.input))],
|
|
|
|
|
pre_ [] [text (toMisoString (A.encode (collectionForm.fill m.input)))]
|
2024-06-06 23:05:41 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
collectionForm :: F.Form T.Text T.Text
|
|
|
|
|
collectionForm =
|
|
|
|
|
F.input "name"
|