aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Page/ListCollection.hs
blob: 6b999f0937e57b58326ce8480988172fbe3ebd67 (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
module Page.ListCollection
  ( Model,
    initialModel,
    Action,
    updateModel,
    viewModel,
  )
where

import ACMS.API.REST.Collection qualified as API.REST.Collection
import Control.Monad.Catch (SomeException, try)
import Data.Aeson qualified as A
import Data.Aeson.KeyMap qualified as AM
import Effect (Eff)
import Miso
import Schema
import Collection

data Model = Model
  { collection :: Collection,
    input :: A.Object,
    schema :: Schema,
    posts :: [A.Object]
  }
  deriving (Show, Eq)

initialModel :: Collection -> JSM (Either SomeException Model)
initialModel collection = do
  schema' <- try (API.REST.Collection.schema collection)
  posts' <- try (API.REST.Collection.list collection)
  pure do
    schema <- schema'
    posts <- posts'
    pure $ Model {input = AM.empty, ..}

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

updateModel :: Action -> Model -> (Effect Action Model, [Eff])
updateModel (Action f) m = f m

viewModel :: Model -> View Action
viewModel m =
  div_ [] $
    [ h3_ [] [text "entities"],
      schemaTable m.collection.name m.schema m.posts,
      h3_ [] [text "schema"],
      viewSchema m.schema
    ]