-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview.ur
357 lines (320 loc) · 17.8 KB
/
review.ur
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
open Bootstrap3
functor Make(M : sig
con reviewer :: Name
con reviewed :: {Type}
con other :: {(Type * Type * Type)}
constraint reviewed ~ other
constraint [reviewer] ~ [When]
constraint [reviewer, When] ~ reviewed
constraint [reviewer, When] ~ other
constraint [Channel] ~ reviewed
table tab : ([When = time, reviewer = string] ++ reviewed ++ map fst3 other)
val widgets : $(map Widget.t' other)
val reviewedFl : folder reviewed
val otherFl : folder other
val reviewedInj : $(map sql_injectable reviewed)
val otherInj : $(map (fn p => sql_injectable p.1) other)
val labels : $(map (fn _ => string) other)
val show_reviewed : show $reviewed
val summarize : $(map fst3 other) -> xbody
val whoami : transaction (option string)
end) = struct
style summary
style full
style fullHeader
style editing
style editingHeader
open M
type fields = {Reviewed : $reviewed, Reviewer : string, When : time, Other : $(map fst3 other)}
datatype action =
Add of fields
| Edit of fields
table specificListeners : (reviewed ++ [Channel = channel action])
structure One = struct
type input = _
datatype review_state =
Summary of $(map (fn p => id * p.1) other)
| Full of $(map (fn p => id * p.1) other)
| Editing of $(map (fn p => id * p.1) other) * $(map (fn p => id * p.2) other)
type review = {Reviewer : string,
When : time,
State : source review_state,
WaitingForRpc : source bool}
type a = {Config : $(map thd3 other),
Whoami : string,
Reviewed : $reviewed,
Reviews : source (list review),
Widgets : $(map (fn p => id * p.2) other),
Channel : channel action}
val freshWidgets =
@Monad.mapR2 _ [Widget.t'] [thd3] [fn p => id * p.2]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (cfg : p.3) =>
id <- fresh;
w <- @Widget.create w cfg;
return (id, w))
otherFl widgets
val widgetsFrom =
@Monad.mapR3 _ [Widget.t'] [thd3] [fn p => id * p.1] [fn p => id * p.2]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (cfg : p.3) (_, x : p.1) =>
id <- fresh;
w <- @Widget.initialize w cfg x;
return (id, w))
otherFl widgets
val readWidgets =
@Monad.mapR2 _ [Widget.t'] [fn p => id * p.2] [fst3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (id, x : p.2) =>
current (@Widget.value w x))
otherFl widgets
fun create key =
u <- whoami;
u <- (case u of
None => error <xml>Must be logged in</xml>
| Some u => return u);
cfg <- @Monad.mapR _ [Widget.t'] [thd3]
(fn [nm ::_] [p ::_] (x : Widget.t' p) => @Widget.configure x)
otherFl widgets;
rs <- List.mapQueryM (SELECT tab.{reviewer}, tab.When, tab.{{map fst3 other}}
FROM tab
WHERE {@Sql.easy_where [#Tab] ! ! reviewedInj reviewedFl key}
ORDER BY tab.When)
(fn {Tab = r} =>
other <- @Monad.mapR _ [fst3] [fn p => id * p.1]
(fn [nm ::_] [p ::_] (x : p.1) =>
id <- fresh;
return (id, x))
otherFl (r -- reviewer -- #When);
rs <- source (Summary other);
waiting <- source False;
return {Reviewer = r.reviewer,
When = r.When,
State = rs,
WaitingForRpc = waiting});
rs <- source rs;
ws <- freshWidgets cfg;
ch <- channel;
@@Sql.easy_insert [[Channel = _] ++ reviewed] [_]
({Channel = _} ++ reviewedInj)
(@Folder.cons [#Channel] [_] ! reviewedFl)
specificListeners ({Channel = ch} ++ key);
return {Config = cfg,
Whoami = u,
Reviewed = key,
Reviews = rs,
Widgets = ws,
Channel = ch}
fun onload a =
let
fun loop () =
act <- recv a.Channel;
(case act of
Add r =>
rs <- get a.Reviews;
other <- @Monad.mapR _ [fst3] [fn p => id * p.1]
(fn [nm ::_] [p ::_] (x : p.1) =>
id <- fresh;
return (id, x))
otherFl r.Other;
st <- source (Summary other);
waiting <- source False;
set a.Reviews (List.append rs ({Reviewer = r.Reviewer,
When = r.When,
State = st,
WaitingForRpc = waiting} :: []))
| Edit r =>
rs <- get a.Reviews;
other <- @Monad.mapR _ [fst3] [fn p => id * p.1]
(fn [nm ::_] [p ::_] (x : p.1) =>
id <- fresh;
return (id, x))
otherFl r.Other;
st <- source (Summary other);
rs <- List.mapM (fn r' =>
if r'.Reviewer = r.Reviewer then
set r'.WaitingForRpc False;
return {Reviewer = r.Reviewer,
When = r.When,
State = st,
WaitingForRpc = r'.WaitingForRpc}
else
return r') rs;
set a.Reviews (List.sort (fn r1 r2 => r1.When > r2.When) rs));
loop ()
in
spawn (loop ())
end
fun edit key other =
u <- whoami;
case u of
None => error <xml>Must be logged in to edit a record</xml>
| Some u =>
tm <- now;
@@Sql.easy_update' [[reviewer = _] ++ reviewed]
[[When = _] ++ map fst3 other] [_] !
({reviewer = _} ++ reviewedInj)
({When = _} ++ otherInj)
(@Folder.cons [reviewer] [_] ! reviewedFl)
(@Folder.cons [#When] [_] ! (@Folder.mp otherFl))
tab ({reviewer = u} ++ key)
({When = tm, reviewer = u} ++ key ++ other);
queryI1 (SELECT specificListeners.Channel
FROM specificListeners
WHERE {@Sql.easy_where [#SpecificListeners] ! ! reviewedInj reviewedFl key})
(fn r => send r.Channel (Edit {Reviewed = key,
Reviewer = u,
When = tm,
Other = other}))
fun add key other =
u <- whoami;
case u of
None => error <xml>Must be logged in to add a record</xml>
| Some u =>
tm <- now;
@@Sql.easy_insert [[When = _, reviewer = _] ++ reviewed ++ map fst3 other] [_]
({When = _, reviewer = _} ++ reviewedInj ++ otherInj)
(@Folder.cons [#When] [_] ! (@Folder.cons [reviewer] [_] !
(@Folder.concat ! reviewedFl (@Folder.mp otherFl))))
tab ({When = tm, reviewer = u} ++ key ++ other);
queryI1 (SELECT specificListeners.Channel
FROM specificListeners
WHERE {@Sql.easy_where [#SpecificListeners] ! ! reviewedInj reviewedFl key})
(fn r => send r.Channel (Add {Reviewed = key,
Reviewer = u,
When = tm,
Other = other}))
fun render _ a = <xml>
<dyn signal={rs <- signal a.Reviews;
return (List.mapX (fn r => <xml>
<dyn signal={st <- signal r.State;
return (case st of
Summary o => <xml>
<div class={summary}
onclick={fn _ => set r.State (Full o)}>
{[r.Reviewer]}
({[r.When]})
{[summarize (@mp [fn p => id * p.1] [fst3]
(fn [p] (_, x) => x)
otherFl o)]}
</div>
</xml>
| Full o => <xml>
<div class={full}>
<div class={fullHeader}
onclick={fn _ => set r.State (Summary o)}>
{[r.Reviewer]}
({[r.When]})
</div>
{if r.Reviewer <> a.Whoami then
<xml/>
else <xml>
<dyn signal={b <- signal r.WaitingForRpc;
return (if b then
<xml/>
else <xml>
<button class="btn btn-primary"
value="Edit Review"
onclick={fn _ =>
ws <- widgetsFrom a.Config o;
set r.State (Editing (o, ws))}/>
</xml>)}/>
</xml>}
{@mapX3 [fn _ => string] [Widget.t'] [fn p => id * p.1] [body]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (lab : string) (w : Widget.t' p) (id, v : p.1) => <xml>
<div class="form-group">
<label class="control-label" for={id}>{[lab]}</label>
<div id={id}>
{@Widget.asValue w v}
</div>
</div>
</xml>)
otherFl labels widgets o}
</div>
</xml>
| Editing (o, ws) => <xml>
<div class={editing}>
<div class={editingHeader}>
{[r.Reviewer]}
({[r.When]})
</div>
<button class="btn btn-primary"
value="Save Review"
onclick={fn _ =>
vs <- readWidgets ws;
set r.State (Full o);
set r.WaitingForRpc True;
rpc (edit a.Reviewed vs)}/>
<button class="btn"
value="Cancel Editing"
onclick={fn _ => set r.State (Full o)}/>
{@mapX3 [fn _ => string] [Widget.t'] [fn p => id * p.2] [body]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (lab : string) (w : Widget.t' p) (id, v : p.2) => <xml>
<div class="form-group">
<label class="control-label" for={id}>{[lab]}</label>
{@Widget.asWidget w v (Some id)}
</div>
</xml>)
otherFl labels widgets ws}
</div>
</xml>)}/>
</xml>) rs)}/>
<dyn signal={rs <- signal a.Reviews;
return (if List.exists (fn r => r.Reviewer = a.Whoami) rs then
<xml/>
else <xml>
<hr/>
<h2>Add Review</h2>
{@mapX3 [fn _ => string] [Widget.t'] [fn p => id * p.2] [body]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (lab : string) (w : Widget.t' p) (id, x : p.2) => <xml>
<div class="form-group">
<label class="control-label" for={id}>{[lab]}</label>
{@Widget.asWidget w x (Some id)}
</div>
</xml>)
otherFl labels widgets a.Widgets}
<button class="btn btn-primary"
value="Add Review"
onclick={fn _ =>
vs <- readWidgets a.Widgets;
rpc (add a.Reviewed vs)}/>
</xml>)}/>
</xml>
fun ui inp = {Create = create inp,
Onload = onload,
Render = render}
end
con hidden_fields = _
constraint hidden_fields ~ reviewed
structure Several = struct
type input = _
type a = _
fun create e =
queryL1 (SELECT *
FROM tab AS T
WHERE {e}
ORDER BY T.When)
fun onload _ = return ()
fun render _ rs = <xml>
{List.mapX (fn r => <xml>
<div class={full}>
<div class={fullHeader}>
{[r -- #When -- reviewer --- (map fst3 other)]}
-- {[r.reviewer]}
({[r.When]})
</div>
{@mapX3 [fn _ => string] [Widget.t'] [fst3] [body]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (lab : string) (w : Widget.t' p) (v : p.1) => <xml>
<div class="form-group">
<label class="control-label">{[lab]}</label>
<div>
{@Widget.asValue w v}
</div>
</div>
</xml>)
otherFl labels widgets (r -- #When -- reviewer --- reviewed)}
</div>
</xml>) rs}
</xml>
fun ui e = {Create = create e,
Onload = onload,
Render = render}
end
end