/
SchoolCustomize.re
497 lines (477 loc) · 16 KB
/
SchoolCustomize.re
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
open SchoolCustomize__Types;
[%bs.raw {|require("./SchoolCustomize.css")|}];
let str = ReasonReact.string;
type editor =
| LinksEditor(SchoolCustomize__LinksEditor.kind)
| ImagesEditor
| ContactsEditor
| AgreementsEditor(SchoolCustomize__AgreementsEditor.kind);
type state = {
visibleEditor: option(editor),
customizations: Customizations.t,
};
type action =
| ShowEditor(editor)
| CloseEditor
| AddLink(Customizations.link)
| RemoveLink(Customizations.linkId)
| UpdateTermsOfUse(string)
| UpdatePrivacyPolicy(string)
| UpdateAddress(string)
| UpdateEmailAddress(string)
| UpdateImages(Js.Json.t);
let component = ReasonReact.reducerComponent("SchoolCustomize");
let headerLogo = (schoolName, logoOnLightBg) =>
switch (logoOnLightBg) {
| Some(logo) =>
<div className="w-30">
<img className="w-full" src={logo |> Customizations.url} />
</div>
| None => <span className="text-2xl font-bold"> {schoolName |> str} </span>
};
let headerLink = ((id, title, _)) =>
<div className="ml-6 text-sm font-semibold cursor-default" key=id>
<span> {title |> str} </span>
</div>;
let headerLinks = links => {
let (visibleLinks, dropdownLinks) =
switch (links) {
| [l1, l2, l3, l4, l5, ...rest] => ([l1, l2, l3], [l4, l5, ...rest])
| fourOrLessLinks => (fourOrLessLinks, [])
};
switch (visibleLinks) {
| [] =>
<div
className="border border-grey-400 rounded-lg italic text-grey-dark cursor-default text-sm py-2 px-4">
{"You can customize links on the header." |> str}
</div>
| visibleLinks =>
(visibleLinks |> List.map(l => headerLink(l)))
->List.append([
<SchoolCustomize__MoreLinks links=dropdownLinks key="more-links" />,
])
|> Array.of_list
|> ReasonReact.array
};
};
let sitemap = links =>
switch (links) {
| [] =>
<div
className="border border-grey rounded-lg italic text-grey-400 cursor-default text-sm max-w-fc mt-3 py-2 px-4">
{"You can customize links in the footer." |> str}
</div>
| links =>
<div className="flex flex-wrap">
{
links
|> List.map(((id, title, _)) =>
<div className="w-1/3 pr-4 mt-3 text-sm" key=id>
{title |> str}
</div>
)
|> Array.of_list
|> ReasonReact.array
}
</div>
};
let socialLinks = links =>
switch (links) {
| [] =>
<div
className="border border-grey rounded-lg italic text-grey-400 cursor-default text-sm max-w-fc mt-3 py-2 px-4">
{"Add social media links?" |> str}
</div>
| links =>
<div className="flex flex-wrap">
{
links
|> List.map(((id, _title, url)) =>
<SchoolCustomize__SocialLink url key=id />
)
|> Array.of_list
|> ReasonReact.array
}
</div>
};
let address = a =>
switch (a) {
| Some(a) =>
<div
className="text-sm mt-3 leading-normal"
dangerouslySetInnerHTML={"__html": a |> Markdown.parse}
/>
| None =>
<div
className="border border-grey rounded-lg italic text-grey-400 cursor-default text-sm max-w-fc mt-3 py-2 px-4">
{"Add an address?" |> str}
</div>
};
let emailAddress = email =>
switch (email) {
| Some(email) =>
<div className="text-sm mt-4">
{"React us at " |> str}
<span className="font-bold"> {email |> str} </span>
</div>
| None =>
<div
className="border border-grey rounded-lg italic text-grey-400 cursor-default text-sm max-w-fc mt-4 py-2 px-4">
{"Add a contact email?" |> str}
</div>
};
let footerLogo = (schoolName, logoOnDarkBg) =>
switch (logoOnDarkBg) {
| Some(logo) => <img className="h-8" src={logo |> Customizations.url} />
| None => <span className="text-xl font-bold"> {schoolName |> str} </span>
};
let editIcon = (additionalClasses, clickHandler, title) =>
<div
className={
"cursor-pointer bg-primary-100 border border-primary-400 text-primary-500 hover:bg-primary-200 hover:border-primary-500 hover:text-primary-dark p-2 rounded flex items-center "
++ additionalClasses
}
title
onClick=clickHandler>
<i className="fas fa-pencil text-xs" />
<span className="text-xs font-semibold ml-2"> {"Edit" |> str} </span>
</div>;
let showEditor = (editor, send, event) => {
event |> ReactEvent.Mouse.preventDefault;
send(ShowEditor(editor));
};
let editor = (state, send, authenticityToken) =>
switch (state.visibleEditor) {
| Some(editor) =>
<SchoolAdmin__EditorDrawer closeDrawerCB=(() => send(CloseEditor))>
{
switch (editor) {
| LinksEditor(kind) =>
<SchoolCustomize__LinksEditor
key="sc-drawer__links-editor"
kind
customizations={state.customizations}
authenticityToken
addLinkCB=(link => send(AddLink(link)))
removeLinkCB=(linkId => send(RemoveLink(linkId)))
/>
| AgreementsEditor(kind) =>
<SchoolCustomize__AgreementsEditor
key="sc-drawer__agreements-editor"
kind
customizations={state.customizations}
updatePrivacyPolicyCB=(
agreement => send(UpdatePrivacyPolicy(agreement))
)
updateTermsOfUseCB=(
agreement => send(UpdateTermsOfUse(agreement))
)
authenticityToken
/>
| ContactsEditor =>
<SchoolCustomize__ContactsEditor
key="sc-drawer__contacts-editor"
customizations={state.customizations}
updateAddressCB=(address => send(UpdateAddress(address)))
updateEmailAddressCB=(
emailAddress => send(UpdateEmailAddress(emailAddress))
)
authenticityToken
/>
| ImagesEditor =>
<SchoolCustomize__ImagesEditor
key="sc-drawer__images-editor"
customizations={state.customizations}
updateImagesCB=(json => send(UpdateImages(json)))
authenticityToken
/>
}
}
</SchoolAdmin__EditorDrawer>
| None => ReasonReact.null
};
let make = (~authenticityToken, ~customizations, ~schoolName, _children) => {
...component,
initialState: () => {visibleEditor: None, customizations},
reducer: (action, state) =>
switch (action) {
| ShowEditor(editor) =>
ReasonReact.Update({...state, visibleEditor: Some(editor)})
| CloseEditor => ReasonReact.Update({...state, visibleEditor: None})
| AddLink(link) =>
ReasonReact.Update({
...state,
customizations: state.customizations |> Customizations.addLink(link),
})
| RemoveLink(linkId) =>
ReasonReact.Update({
...state,
customizations:
state.customizations |> Customizations.removeLink(linkId),
})
| UpdatePrivacyPolicy(agreement) =>
ReasonReact.Update({
...state,
customizations:
state.customizations
|> Customizations.updatePrivacyPolicy(agreement),
})
| UpdateTermsOfUse(agreement) =>
ReasonReact.Update({
...state,
customizations:
state.customizations |> Customizations.updateTermsOfUse(agreement),
})
| UpdateAddress(address) =>
ReasonReact.Update({
...state,
customizations:
state.customizations |> Customizations.updateAddress(address),
})
| UpdateEmailAddress(emailAddress) =>
ReasonReact.Update({
...state,
customizations:
state.customizations
|> Customizations.updateEmailAddress(emailAddress),
})
| UpdateImages(json) =>
ReasonReact.Update({
...state,
customizations:
state.customizations |> Customizations.updateImages(json),
})
},
render: ({state, send}) =>
<div>
<div className="px-6 pt-6 w-full xl:max-w-2xl mx-auto">
<div className="font-bold"> {"Header" |> str} </div>
<div
className="border rounded-lg px-5 py-4 flex justify-between mt-3 shadow">
<div className="flex items-center bg-grey-200 rounded p-2">
{
headerLogo(
schoolName,
state.customizations |> Customizations.logoOnLightBg,
)
}
{
editIcon(
"ml-6",
showEditor(ImagesEditor, send),
"Edit logo (on light backgrounds)",
)
}
</div>
<div className="flex items-center">
<div
className="school-customize__header-links flex items-center bg-grey-200 rounded px-3 py-2 h-full">
{
headerLinks(
state.customizations
|> Customizations.headerLinks
|> Customizations.unpackLinks,
)
}
{
editIcon(
"ml-3",
showEditor(
LinksEditor(SchoolCustomize__LinksEditor.HeaderLink),
send,
),
"Edit header links",
)
}
</div>
<div className="ml-6 w-12 h-12 rounded-full bg-grey-400" />
</div>
</div>
<div className="mt-6 font-bold"> {"Footer" |> str} </div>
<div className="mt-3 w-full">
<div
className="school-customize__footer-top-container rounded-t-lg text-white p-6 flex">
<div className="w-1/2">
<div
className="p-3 bg-black border border-dashed border-grey-darkest rounded h-full mr-2">
<div className="flex items-center">
<span className="uppercase font-bold text-sm">
{"Sitemap" |> str}
</span>
{
editIcon(
"ml-3",
showEditor(
LinksEditor(SchoolCustomize__LinksEditor.FooterLink),
send,
),
"Edit footer links",
)
}
</div>
{
sitemap(
state.customizations
|> Customizations.footerLinks
|> Customizations.unpackLinks,
)
}
</div>
</div>
<div className="w-1/2">
<div className="flex">
<div className="w-1/2">
<div
className="p-3 bg-black border border-dashed border-grey-darkest rounded h-full mr-2">
<div className="flex items-center">
<span className="uppercase font-bold text-sm">
{"Social" |> str}
</span>
{
editIcon(
"ml-3",
showEditor(
LinksEditor(
SchoolCustomize__LinksEditor.SocialLink,
),
send,
),
"Edit social media links",
)
}
</div>
{
socialLinks(
state.customizations
|> Customizations.socialLinks
|> Customizations.unpackLinks,
)
}
</div>
</div>
<div className="w-1/2">
<div
className="p-3 bg-black border border-dashed border-grey-darkest rounded h-full">
<div className="flex items-center">
<span className="uppercase font-bold text-sm">
{"Contact" |> str}
</span>
{
editIcon(
"ml-3",
showEditor(ContactsEditor, send),
"Edit contact details",
)
}
</div>
{address(state.customizations |> Customizations.address)}
{
emailAddress(
state.customizations |> Customizations.emailAddress,
)
}
</div>
</div>
</div>
</div>
</div>
<div
className="school-customize__footer-bottom-container rounded-b-lg text-white p-6 flex justify-between">
<div
className="flex items-center bg-black border border-dashed border-grey-darkest rounded p-2">
{
footerLogo(
schoolName,
state.customizations |> Customizations.logoOnDarkBg,
)
}
{
editIcon(
"ml-3",
showEditor(ImagesEditor, send),
"Edit logo (on dark backgrounds)",
)
}
</div>
<div className="flex items-center text-sm">
<div
className="flex items-center bg-black border border-dashed border-grey-darkest rounded p-2">
<div> {"Privacy Policy" |> str} </div>
{
editIcon(
"ml-3",
showEditor(
AgreementsEditor(
SchoolCustomize__AgreementsEditor.PrivacyPolicy,
),
send,
),
"Edit privacy policy",
)
}
</div>
<div
className="flex items-center bg-black border border-dashed border-grey-darkest rounded p-2 ml-6">
<div> {"Terms of Use" |> str} </div>
{
editIcon(
"ml-3",
showEditor(
AgreementsEditor(
SchoolCustomize__AgreementsEditor.TermsOfUse,
),
send,
),
"Edit terms of use",
)
}
</div>
<div className="ml-6 flex items-center">
<i className="far fa-copyright" />
<span className="ml-1">
{
(
Js.Date.make()
|> Js.Date.getFullYear
|> int_of_float
|> string_of_int
)
++ " "
++ schoolName
|> str
}
</span>
</div>
</div>
</div>
</div>
<div className="mt-6 font-bold"> {"Icon" |> str} </div>
<div className="mt-3 w-2/4 max-w-sm">
<div className="bg-grey-400 rounded-t-lg h-12 flex items-end">
<div className="w-full flex items-center pr-3">
<div className="h-3 w-3 rounded-full bg-grey ml-4" />
<div className="h-3 w-3 rounded-full bg-grey ml-2" />
<div className="h-3 w-3 rounded-full bg-grey ml-2" />
<div
className="p-3 ml-4 bg-grey-100 rounded-t-lg flex items-center">
<img
src={
state.customizations
|> Customizations.icon
|> Customizations.url
}
className="h-5 w-5"
/>
<span className="ml-1 text-sm font-semibold">
{schoolName |> str}
</span>
</div>
{editIcon("ml-2", showEditor(ImagesEditor, send), "Edit icon")}
</div>
</div>
<div className="bg-grey-100 border border-t-0 h-16 rounded-b-lg" />
</div>
</div>
{editor(state, send, authenticityToken)}
</div>,
};