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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
module Page.EditValue
( Model,
initialModel,
Action,
updateModel,
viewModel,
)
where
import Api
import Data.Aeson qualified as A
import Data.Aeson.KeyMap qualified as AM
import Data.ByteString.Lazy.UTF8 as LB
import Data.Maybe
import Effect (Eff)
import Form qualified as F
import Miso
import Miso.String (toMisoString)
import Schema
data Model = Model
{ collection :: String,
fileName :: String,
input :: Maybe A.Value,
schema :: Schema
}
deriving (Show, Eq)
initialModel :: String -> String -> JSM (Either String Model)
initialModel collection fileName = do
schema' <- fetchSchema
input' <- fetchPost fileName
pure do
schema <- schema'
input <- input'
pure $ Model {..}
newtype Action = Action (Model -> (Effect Action Model, [Eff]))
update__formChanged :: A.Value -> Action
update__formChanged (Just -> input) = Action $ \m -> (noEff m {input}, [])
update__formSubmitted :: A.Value -> Action
update__formSubmitted output = Action $ \m ->
(m <# do update__entityWritten <$> updatePost m.fileName output, [])
update__entityWritten :: Either String () -> Action
update__entityWritten _ = Action $ \m -> (noEff m, [])
updateModel :: Action -> Model -> (Effect Action Model, [Eff])
updateModel (Action f) m = f m
viewModel :: Model -> View Action
viewModel m = do
let input = (fromMaybe (A.Object AM.empty) m.input)
div_ [] $
[ viewForm input m.schema,
viewInput input,
viewOutput input m.schema
]
viewForm :: A.Value -> Schema -> View Action
viewForm input =
fmap (either update__formChanged update__formSubmitted)
. flip F.runForm input
. schemaForm
viewInput :: A.Value -> View Action
viewInput input =
pre_ [] [text (toMisoString (A.encode input))]
viewOutput :: A.Value -> Schema -> View Action
viewOutput input schema =
pre_ [] $
[ text $
toMisoString
( either ("Left " <>) (("Right " <>) . LB.toString) $
(A.encode <$> ((schemaForm schema).fill input))
)
]
|