/
Form.js
153 lines (141 loc) · 5.17 KB
/
Form.js
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
import { useEffect, useMemo, useState } from 'react'
import { useForm, Controller } from 'react-hook-form'
import { Box, Flex, Input, Button } from 'ooni-components'
import { MultiSelect } from 'react-multi-select-component'
import { useIntl } from 'react-intl'
import dayjs from 'services/dayjs'
import { format } from 'date-fns'
import { getLocalisedRegionName } from '../../utils/i18nCountries'
import { StyledLabel } from '../aggregation/mat/Form'
import DateRangePicker from '../DateRangePicker'
export const Form = ({ onChange, query, availableCountries }) => {
const intl = useIntl()
const countryOptions = useMemo(() => availableCountries
.map(cc => ({
label: getLocalisedRegionName(cc, intl.locale),
value: cc
}))
.sort((a, b) => (new Intl.Collator(intl.locale).compare(a.label, b.label)))
, [availableCountries, intl])
const query2formValues = useMemo(() => {
const countriesInQuery = query.probe_cc?.split(',') ?? ''
return {
since: query?.since,
until: query?.until,
probe_cc: countryOptions.filter(country => countriesInQuery.includes(country.value)),
}
}, [countryOptions, query])
const multiSelectStrings = useMemo(() => ({
'allItemsAreSelected': intl.formatMessage({ id: 'ReachabilityDash.Form.Label.CountrySelect.AllSelected' }),
// 'clearSearch': 'Clear Search',
// 'clearSelected': 'Clear Selected',
// 'noOptions': 'No options',
'search': intl.formatMessage({ id: 'ReachabilityDash.Form.Label.CountrySelect.SearchPlaceholder' }),
'selectAll': intl.formatMessage({ id: 'ReachabilityDash.Form.Label.CountrySelect.SelectAll' }),
'selectAllFiltered': intl.formatMessage({ id: 'ReachabilityDash.Form.Label.CountrySelect.SelectAllFiltered' }),
'selectSomeItems': intl.formatMessage({ id: 'ReachabilityDash.Form.Label.CountrySelect.InputPlaceholder' }),
// 'create': 'Create',
}), [intl])
const { control, getValues, watch, setValue, reset } = useForm({
defaultValues: query2formValues
})
useEffect(()=> {
reset(query2formValues)
}, [query2formValues, reset])
const cleanedUpData = (values) => {
const { since, until, probe_cc } = values
return {
since,
until,
probe_cc: probe_cc.length > 0 ? probe_cc.map(d => d.value).join(',') : undefined
}
}
const [showDatePicker, setShowDatePicker] = useState(false)
const handleRangeSelect = (range) => {
if (range?.from) {
setValue('since', format(range.from, 'y-MM-dd'))
} else {
setValue('since', '')
}
if (range?.to) {
setValue('until', format(range.to, 'y-MM-dd'))
} else {
setValue('until', '')
}
setShowDatePicker(false)
onChange(cleanedUpData(getValues()))
}
useEffect(() => {
const subscription = watch((value, { name, type }) => {
if (name === 'probe_cc' && type === 'change') onChange(cleanedUpData(getValues()))
})
return () => subscription.unsubscribe()
}, [watch, getValues])
return (
<form>
<Flex alignItems={['center']} flexDirection={['column', 'row']}>
<Box width={[1, 1/4]} mr={3} sx={{ zIndex: 2 }}>
<StyledLabel>{intl.formatMessage({id: 'Search.Sidebar.Country'})}</StyledLabel>
{<Controller
render={({field}) => (
<MultiSelect
options={countryOptions}
overrideStrings={multiSelectStrings}
name={field.name}
value={field.value}
onChange={field.onChange}
/>
)}
name='probe_cc'
control={control}
/>}
</Box>
<Box width={[1, 1/5]}>
<Flex>
<Box width={1/2} mr={3}>
<StyledLabel>{intl.formatMessage({id: 'Search.Sidebar.From'})}</StyledLabel>
<Controller
name='since'
control={control}
render={({field}) => (
<Input
{...field}
onFocus={() => setShowDatePicker(true)}
onKeyDown={() => setShowDatePicker(false)}
name={field.name}
value={field.value}
onChange={field.onChange}
/>
)}
/>
</Box>
<Box width={1/2} mr={3}>
<StyledLabel>{intl.formatMessage({id: 'Search.Sidebar.Until'})}</StyledLabel>
<Controller
name='until'
control={control}
render={({field}) => (
<Input
{...field}
onFocus={() => setShowDatePicker(true)}
onKeyDown={() => setShowDatePicker(false)}
name={field.name}
value={field.value}
onChange={field.onChange}
/>
)}
/>
</Box>
</Flex>
{ showDatePicker &&
<DateRangePicker
handleRangeSelect={handleRangeSelect}
initialRange={{from: getValues('since'), to: getValues('until')}}
close={() => setShowDatePicker(false)}
/>
}
</Box>
</Flex>
</form>
)
}