-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcapture-set-details-form.tsx
More file actions
403 lines (395 loc) · 11.3 KB
/
Copy pathcapture-set-details-form.tsx
File metadata and controls
403 lines (395 loc) · 11.3 KB
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
import { FormController } from 'components/form/form-controller'
import { FormField } from 'components/form/form-field'
import {
FormActions,
FormError,
FormMessage,
FormRow,
FormSection,
} from 'components/form/layout/layout'
import { FormConfig } from 'components/form/types'
import { CaptureSet } from 'data-services/models/capture-set'
import { SaveButton } from 'design-system/components/button/save-button'
import { InputContent } from 'design-system/components/input/input'
import { DatePicker } from 'design-system/components/select/date-picker'
import { XIcon } from 'lucide-react'
import { Button, Select } from 'nova-ui-kit'
import { SERVER_SAMPLING_METHODS } from 'pages/project/capture-sets/constants'
import { useForm } from 'react-hook-form'
import {
formatIntegerList,
parseIntegerList,
validateInteger,
validateIntegerList,
} from 'utils/fieldProcessors'
import { STRING, translate } from 'utils/language'
import { snakeCaseToSentenceCase } from 'utils/snakeCaseToSentenceCase'
import { useFormError } from 'utils/useFormError'
import { DetailsFormProps, FormValues } from './types'
type CaptureSetFormValues = FormValues & {
method: string
kwargs: {
date_start: string | undefined
date_end: string | undefined
hour_start: number | undefined
hour_end: number | undefined
max_num: number | undefined
minute_interval: number | undefined
size: number | undefined
deployment_ids: string | undefined
research_site_ids: string | undefined
event_ids: string | undefined
}
}
const config: FormConfig = {
name: {
label: translate(STRING.FIELD_LABEL_NAME),
rules: {
required: true,
},
},
description: {
label: translate(STRING.FIELD_LABEL_DESCRIPTION),
},
method: {
label: 'Method',
rules: {
required: true,
},
},
'kwargs.date_start': {
label: 'Earliest date',
},
'kwargs.date_end': {
label: 'Latest date',
},
'kwargs.hour_start': {
label: 'Earliest hour',
description: 'Enter a number between 0 and 24.',
rules: {
min: 0,
max: 24,
validate: validateInteger,
},
},
'kwargs.hour_end': {
label: 'Latest hour',
description: 'Enter a number between 0 and 24.',
rules: {
min: 0,
max: 24,
validate: validateInteger,
},
},
'kwargs.max_num': {
label: 'Max number of captures',
rules: {
min: 0,
validate: validateInteger,
},
},
'kwargs.minute_interval': {
label: 'Minutes between captures',
rules: {
min: 0,
required: true,
validate: validateInteger,
},
},
'kwargs.size': {
label: 'Number of captures',
rules: {
min: 0,
required: true,
validate: validateInteger,
},
},
'kwargs.deployment_ids': {
label: 'Station IDs',
description: 'Enter comma-separated numbers (e.g., 1, 2, 3).',
rules: {
validate: validateIntegerList,
},
toApiValue: parseIntegerList,
toFormValue: formatIntegerList,
},
'kwargs.event_ids': {
label: 'Session IDs',
description: 'Enter comma-separated numbers (e.g., 1, 2, 3).',
rules: {
validate: validateIntegerList,
},
toApiValue: parseIntegerList,
toFormValue: formatIntegerList,
},
}
export const CaptureSetDetailsForm = ({
entity,
error,
isLoading,
isSuccess,
onSubmit,
}: DetailsFormProps) => {
const captureSet = entity as CaptureSet | undefined
const { control, handleSubmit, setError, watch } =
useForm<CaptureSetFormValues>({
defaultValues: {
name: entity?.name ?? '',
description: entity?.description ?? '',
kwargs: {
minute_interval: 10,
size: 100,
...Object.fromEntries(
Object.entries(captureSet?.kwargs || {}).map(([key, value]) => {
const fieldConfig = config[`kwargs.${key}`]
const formValue = fieldConfig?.toFormValue
? fieldConfig.toFormValue(value)
: value
return [key, formValue]
})
),
},
method: captureSet?.method ?? SERVER_SAMPLING_METHODS[0],
},
mode: 'onChange',
})
const errorMessage = useFormError({ error, setFieldError: setError })
const method = watch('method')
return (
<form
onSubmit={handleSubmit((values) => {
const processedKwargs = Object.fromEntries(
Object.entries(values.kwargs)
.filter(([key]) => {
// Make sure method specific fields are only passed for related method
if (key === 'max_num' || key === 'minute_interval') {
return values.method === 'interval'
}
if (key === 'size') {
return values.method === 'random'
}
return true
})
.map(([key, value]) => {
const fieldConfig = config[`kwargs.${key}`]
const processedValue = fieldConfig?.toApiValue
? fieldConfig.toApiValue(value)
: value === ''
? null
: value
return [key, processedValue]
})
)
onSubmit({
name: values.name,
description: values.description,
customFields: {
method: values.method,
kwargs: processedKwargs,
},
})
})}
>
{errorMessage && (
<FormError
inDialog
intro={translate(STRING.MESSAGE_COULD_NOT_SAVE)}
message={errorMessage}
/>
)}
<FormSection>
<FormMessage
message={translate(STRING.MESSAGE_CAPTURE_SET_FORM_INTRO)}
/>
<h3 className="body-large font-bold text-muted-foreground/50">
General
</h3>
<FormRow>
<FormField
name="name"
type="text"
config={config}
control={control}
/>
<FormField
name="description"
type="text"
config={config}
control={control}
/>
</FormRow>
</FormSection>
<FormSection>
<h3 className="body-large font-bold text-muted-foreground/50">
Filters
</h3>
<FormRow>
<FormController
name="kwargs.date_start"
control={control}
config={config['kwargs.date_start']}
render={({ field, fieldState }) => (
<InputContent
description={config[field.name].description}
label={config[field.name].label}
error={fieldState.error?.message}
>
<div className="flex items-center justify-between gap-2">
<DatePicker
value={field.value}
onValueChange={field.onChange}
/>
{field.value && (
<Button
aria-label={translate(STRING.CLEAR)}
size="icon"
className="shrink-0 text-muted-foreground"
variant="ghost"
onClick={() => field.onChange('')}
>
<XIcon className="w-4 h-4" />
</Button>
)}
</div>
</InputContent>
)}
/>
<FormController
name="kwargs.date_end"
control={control}
config={config['kwargs.date_end']}
render={({ field, fieldState }) => (
<InputContent
description={config[field.name].description}
label={config[field.name].label}
error={fieldState.error?.message}
>
<div className="flex items-center justify-between gap-2">
<DatePicker
value={field.value}
onValueChange={field.onChange}
/>
{field.value && (
<Button
aria-label={translate(STRING.CLEAR)}
size="icon"
className="shrink-0 text-muted-foreground"
variant="ghost"
onClick={() => field.onChange('')}
>
<XIcon className="w-4 h-4" />
</Button>
)}
</div>
</InputContent>
)}
/>
</FormRow>
<FormRow>
<FormField
name="kwargs.hour_start"
type="number"
config={config}
control={control}
/>
<FormField
name="kwargs.hour_end"
type="number"
config={config}
control={control}
/>
</FormRow>
<FormRow>
<FormField
name="kwargs.deployment_ids"
type="text"
config={config}
control={control}
/>
<FormField
name="kwargs.event_ids"
type="text"
config={config}
control={control}
/>
</FormRow>
</FormSection>
<FormSection>
<h3 className="body-large font-bold text-muted-foreground/50">
Sampling settings
</h3>
<FormRow>
<FormController
name="method"
control={control}
config={config['method']}
render={({ field, fieldState }) => (
<InputContent
description={config[field.name].description}
label={`${config[field.name].label} *`}
error={fieldState.error?.message}
>
<SamplingTypePicker
value={field.value}
onValueChange={field.onChange}
/>
</InputContent>
)}
/>
{method === 'interval' ? (
<>
<FormField
name="kwargs.minute_interval"
type="number"
config={config}
control={control}
/>
<div />
<FormField
name="kwargs.max_num"
type="number"
config={config}
control={control}
/>
</>
) : null}
{method === 'random' ? (
<FormField
name="kwargs.size"
type="number"
config={config}
control={control}
/>
) : null}
</FormRow>
<FormMessage
intro={translate(STRING.TIP)}
message={translate(STRING.MESSAGE_CAPTURE_SET_TIP)}
/>
</FormSection>
<FormActions>
<SaveButton isLoading={isLoading} isSuccess={isSuccess} />
</FormActions>
</form>
)
}
export const SamplingTypePicker = ({
value,
onValueChange,
}: {
value: string
onValueChange: (value: string) => void
}) => (
<Select.Root value={value ?? ''} onValueChange={onValueChange}>
<Select.Trigger>
<Select.Value />
</Select.Trigger>
<Select.Content>
{SERVER_SAMPLING_METHODS.map((samplingMethod) => (
<Select.Item key={samplingMethod} value={samplingMethod}>
{snakeCaseToSentenceCase(samplingMethod)}
</Select.Item>
))}
</Select.Content>
</Select.Root>
)