aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Page/NewCollection.hs
blob: 4b2cf9cb763b1f12c92258ec8c3f157e5a143e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Page.NewCollection
  ( Model,
    initialModel,
    Action,
    updateModel,
    viewModel,
  )
where

import ACMS.API.REST qualified as API.REST
import Control.Monad.Catch (SomeException, try)
import Data.Aeson qualified as A
import Effect (Eff)
import Effect qualified as E
import Form qualified as F
import Miso
import Miso.String (MisoString, toMisoString)

data Model = Model
  { input :: MisoString
  }
  deriving (Show, Eq)

initialModel :: JSM (Either SomeException Model)
initialModel = do
  pure (Right (Model {input = ""}))

newtype Action = Action (Model -> (Effect Action Model, [Eff]))

update__formChanged :: MisoString -> Action
update__formChanged input = Action $ \m -> (noEff m {input}, [])

update__formSubmitted :: MisoString -> Action
update__formSubmitted collection = Action $ \m ->
  ( m <# do
      update__collectionCreated <$> try (API.REST.createCollection collection),
    []
  )

update__collectionCreated :: Either SomeException () -> 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 MisoString MisoString
collectionForm =
  F.inputText "name"