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
|
import { h, Html, Rx } from 'lib/rx'
import * as rx from 'lib/rx'
import * as request from 'request'
import * as modal from 'ui/modal'
import * as layout from 'ui/layout'
import * as markerModel from 'models/marker'
import * as form from 'lib/form'
import * as icons from 'lib/icons'
import * as L from 'lib/loadable'
interface MarkerFormParams {
lat: number
lng: number
marker?: markerModel.Marker
colors: Array<string>
label: string
req: (body: string) => Promise<markerModel.Marker>
onSuccess: (m: markerModel.Marker) => void
onClose: () => void
lastUserAdded?: markerModel.Marker
onDeleteMarker?: () => void
}
export function view({ lat, lng, marker, colors, label, req, onSuccess, onClose, lastUserAdded, onDeleteMarker }: MarkerFormParams): Html {
const initName = marker?.name || ''
const initDescription = marker?.description || ''
const initColor = marker?.color || lastUserAdded?.color || (colors.length > 0 ? colors[0] : '#3584e4')
const initIcon = marker?.icon || ''
const initRadius = marker?.radius || 0
const isCreation = marker === undefined
const iconValues: { [key: string]: string } = {}
Object.keys(icons.values).forEach(key => iconValues[key] = icons.values[key].name)
return modal.view({
header: marker == undefined ? 'Nouveau marqueur' : 'Modification du marqueur',
body: rx.withState7<string, string, string, string, number, L.Loadable<void>, L.Loadable<void>>(
[initName, initDescription, initColor, initIcon, initRadius, L.init, L.init],
(nameVar, descriptionVar, colorVar, iconVar, radiusVar, updateReqVar, deleteReqVar) =>
h('form',
{
onsubmit: rx.map5([nameVar, descriptionVar, colorVar, iconVar, radiusVar], (name, description, color, icon, radius) =>
(event: Event) => {
event.preventDefault()
updateReqVar.update(_ => L.loading)
const payload = { lat, lng, color, name, description, icon, radius }
req(JSON.stringify(payload))
.then((m: markerModel.Marker) => {
updateReqVar.update(_ => L.loaded(undefined))
onSuccess(m)
})
.catch(({ message }) => updateReqVar.update(_ => L.failure(message)))
}
)
},
form.input({
label: 'Nom',
select: isCreation,
initValue: initName,
onUpdate: value => {
nameVar.update(_ => value)
updateReqVar.update(_ => L.init)
deleteReqVar.update(_ => L.init)
}
}),
form.textarea({
label: 'Description',
initValue: initDescription,
onUpdate: value => {
descriptionVar.update(_ => value)
updateReqVar.update(_ => L.init)
deleteReqVar.update(_ => L.init)
}
}),
colorSection({
colorRx: colorVar,
colors,
onUpdateColor: color => {
colorVar.update(_ => color)
updateReqVar.update(_ => L.init)
deleteReqVar.update(_ => L.init)
}
}),
layout.columns([
form.select({
label: 'Icône',
initValue: initIcon,
values: iconValues,
onUpdate: value => {
iconVar.update(_ => value)
updateReqVar.update(_ => L.init)
deleteReqVar.update(_ => L.init)
}
}),
form.input({
type: 'number',
label: 'Radius',
initValue: initRadius.toString(),
onUpdate: value => {
radiusVar.update(_ => parseInt(value) || 0)
updateReqVar.update(_ => L.init)
deleteReqVar.update(_ => L.init)
}
})
]),
form.error(updateReqVar),
form.error(deleteReqVar),
form.submit({
label,
className: 'g-Button--FullWidth',
requestVar: updateReqVar,
disabled: deleteReqVar.map(l => l != L.init)
}),
marker !== undefined && onDeleteMarker !== undefined && form.button({
style: 'margin-top: 1rem',
label: 'Supprimer',
className: 'g-Button--FullWidth g-Button--Danger',
requestVar: deleteReqVar,
disabled: updateReqVar.map(l => l != L.init),
onClick: () => {
deleteReqVar.update(_ => L.loading)
request
.del_(`/api/markers/${marker.id}`)
.then(_ => onDeleteMarker())
.catch(({ message }) => updateReqVar.update(_ => L.failure(message)))
}
})
)
),
onClose: onClose
})
}
interface ColorSectionParams {
colorRx: Rx<string>,
colors: Array<string>,
onUpdateColor: (color: string) => void,
}
function colorSection({ colorRx, colors, onUpdateColor }: ColorSectionParams): Html {
return h('label',
{ className: 'g-Label' },
'Couleur',
h('div',
{ className: 'g-ColorLine' },
h('input',
{
type: 'color',
className: 'g-Input',
oninput: (event: Event) => onUpdateColor((event.target as HTMLInputElement).value),
value: colorRx
}
),
h('div',
{ className: 'g-ColorButtons' },
colors.map(c =>
h('input',
{
type: 'button',
className: 'g-ColorButton',
style: `background-color: ${c}`,
onclick: (event: Event) => onUpdateColor(c)
}
)
)
)
)
)
}
|