aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Page.hs
blob: 7c200c838c79fe4750638856a2e55bd192c6e3fb (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
59
60
61
62
63
64
module Page
  ( Page (..),
    Action,
    initialPage,
    updatePage,
    viewPage,
  )
where

import Data.Bifunctor
import Data.Default
import Data.Function
import Miso
import Page.EditValue qualified as EditValue
import Page.ListCollection qualified as ListCollection
import Page.NewCollection qualified as NewCollection
import Route (Route)
import Route qualified as Route
import Effect (Eff)

data Page
  = Home
  | ListCollection ListCollection.Model
  | EditValue EditValue.Model
  | NewCollection NewCollection.Model
  deriving (Show, Eq)

data Action
  = HandleListCollection ListCollection.Action
  | HandleEditValue EditValue.Action
  | HandleNewCollection NewCollection.Action
  deriving (Show, Eq)

instance Default Page where
  def = Home

initialPage :: Route -> JSM (Either String Page)
initialPage Route.Home = pure (Right Home)
initialPage (Route.ListCollection c) =
  fmap ListCollection <$> ListCollection.initialModel c
initialPage (Route.EditValue c f) =
  fmap EditValue <$> EditValue.initialModel c f
initialPage Route.NewCollection =
  fmap NewCollection <$> NewCollection.initialModel

updatePage :: Action -> Page -> (Effect Action Page, [Eff])
updatePage (HandleListCollection action) (ListCollection m) =
  ListCollection.updateModel action m
    & first (bimap HandleListCollection ListCollection)
updatePage (HandleListCollection _) p = (noEff p, [])
updatePage (HandleEditValue action) (EditValue m) =
  EditValue.updateModel action m
    & first (bimap HandleEditValue EditValue)
updatePage (HandleEditValue _) p = (noEff p, [])
updatePage (HandleNewCollection action) (NewCollection m) =
  NewCollection.updateModel action m
    & first (bimap HandleNewCollection NewCollection)
updatePage (HandleNewCollection _) p = (noEff p, [])

viewPage :: Page -> View Action
viewPage Home = text "home"
viewPage (ListCollection m) = HandleListCollection <$> ListCollection.viewModel m
viewPage (EditValue m) = HandleEditValue <$> EditValue.viewModel m
viewPage (NewCollection m) = HandleNewCollection <$> NewCollection.viewModel m