0 | module Web.Async.Form
2 | import Data.List.Quantifiers.Extra
4 | import public Control.Barbie
5 | import public Data.Singleton
6 | import public Monocle
7 | import public Web.Async
8 | import public Web.Async.Widget
9 | import Text.HTML.DomID
17 | record FormField where
22 | toField : (String,Widget t) -> Maybe FormField
23 | toField (nm, W [] _) = Nothing
24 | toField (nm, W ns _) = Just (FF nm ns)
26 | formStream : List (Widget (t -> t)) -> t -> JSStream t
27 | formStream ws ini = merge (map events ws) |> scanFrom1 ini
29 | parameters {auto loc : DOMLocal}
31 | {auto ipf : Interpolation f}
32 | (0 rec : (f -> Type) -> Type)
33 | (formNode : FormField -> HTMLNode)
35 | {auto sings : rec Singleton}
36 | {auto fuc : FunctorB f rec}
37 | {auto app : ApplicativeB f rec}
38 | {auto trv : TraversableB f rec}
39 | {auto rrd : RecordB f rec}
45 | WForm = (String,Widget (rec (EditRes . g) -> rec (EditRes . g)))
58 | editField (Val v) (E fun) mrec = do
60 | W n s <- fun $
map (field g v).get_ mrec
64 | let s2 := P.observe (logFormField v) s |> P.mapOutput (set (field' v))
66 | pure $
(interpolate v, W n s2)
84 | form : rec (Editor . g) -> Editor (rec g)
87 | let eflds := bzipWith editField sings edits
93 | missAll := the (rec (EditRes . g)) (bpure @{app} Missing)
97 | recw <- btraverse (flip apply recm) eflds
100 | let ws := bfoldMap (Prelude.Lin:<) recw <>> []
103 | ns := formNode <$> mapMaybe toField ws
106 | formStream (map snd ws) missAll
107 | |> mapOutput bsequence
108 | |> P.observe logFormRes
110 | parameters {auto loc : DOMLocal}
112 | {auto els : All (`Elem` ts) ts}
115 | HForm = (Widget (All EditRes ts -> All EditRes ts))
117 | hfield : Elem t ts -> Editor t -> Maybe (HList ts) -> Act HForm
118 | hfield e (E fun) mrec = Prelude.do
119 | W n s <- fun $
Lens.get_ (prod t) <$> mrec
121 | $
observe (logFormFieldN $
elemToNat e) s |> P.mapOutput (set $
prod t)
137 | hform : All Editor ts -> Editor (HList ts)
139 | let eflds := hzipWith hfield els edits
140 | miss := mapProperty {q = EditRes} (\_ => Missing) edits
144 | recw <- hsequence $
mapProperty (flip apply recm) eflds
146 | let ws := hfoldMap (Prelude.Lin:<) recw <>> []
152 | formStream ws miss |> mapOutput hsequence |> P.observe logFormRes