-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalityTab.tsx
193 lines (175 loc) · 6.3 KB
/
LocalityTab.tsx
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
import { Editable, LocalityDetailsType, LocalitySynonym } from '@/shared/types'
import { useDetailContext } from '@/components/DetailView/Context/DetailContext'
import { Grouped, ArrayFrame, HalfFrames } from '@/components/DetailView/common/tabLayoutHelpers'
import { Box, TextField } from '@mui/material'
import { MRT_ColumnDef } from 'material-react-table'
import { useForm } from 'react-hook-form'
import { EditableTable } from '@/components/DetailView/common/EditableTable'
import { EditingModal } from '@/components/DetailView/common/EditingModal'
import { emptyOption } from '@/components/DetailView/common/misc'
import { CoordinateSelectionMap } from '@/components/Map/CoordinateSelectionMap'
import { useState } from 'react'
import { convertDmsToDec, convertDecToDms } from '@/util/coordinateConversion'
import { validCountries } from '@/shared/validators/countryList'
export const LocalityTab = () => {
const { textField, radioSelection, dropdown, dropdownWithSearch, mode, bigTextField } =
useDetailContext<LocalityDetailsType>()
const { editData, setEditData } = useDetailContext<LocalityDetailsType>()
const generalLocalityOptions = [emptyOption, { display: 'No', value: 'n' }, { display: 'Yes', value: 'y' }]
const siteAreaOptions = [
emptyOption,
{ display: '<10 m2', value: '<10m2' },
{ display: '10-50 m2', value: '10-50m2' },
{ display: '50-100 m2', value: '50-100m2' },
{ display: '100-1000 m2', value: '100-1000m2' },
{ display: '>1000 m2', value: '>1000m2' },
]
const countryOptions = ['', ...validCountries]
const info = [
['Name', textField('loc_name')],
[
'Visibility status',
radioSelection(
'loc_status',
[
{ value: 'false', display: 'Public' },
{ value: 'true', display: 'Private' },
],
'Status'
),
],
]
const country = [
['Country', dropdownWithSearch('country', countryOptions, 'Country')],
['State', textField('state')],
['County', textField('county')],
['Detail', bigTextField('loc_detail')],
['Site Area', dropdown('site_area', siteAreaOptions, 'Site Area')],
['General Locality', dropdown('gen_loc', generalLocalityOptions, 'General Locality')],
['Plate', textField('plate')],
]
const handleCoordinateChange = (
value: number | string | Date,
dmsOrDec: 'dms' | 'dec',
latitudeOrLongitude: 'latitude' | 'longitude'
) => {
const latOrLong = latitudeOrLongitude === 'latitude' ? 'lat' : 'long'
const dmsField = 'dms' + '_' + latOrLong
const decField = 'dec' + '_' + latOrLong
if (dmsOrDec === 'dms') {
const valueAsString = String(value)
setEditData({
...editData,
[dmsField]: valueAsString,
[decField]: convertDmsToDec(valueAsString, latitudeOrLongitude),
})
} else {
if (value === '') {
setEditData({ ...editData, [decField]: undefined, [dmsField]: undefined })
return
}
const valueAsNumber = Number(value)
setEditData({
...editData,
[decField]: Number(value),
[dmsField]: convertDecToDms(valueAsNumber, latitudeOrLongitude),
})
}
}
const latlong = [
['', 'dms', 'dec'],
[
'Latitude',
textField('dms_lat', {
type: 'text',
handleSetEditData: value => handleCoordinateChange(value, 'dms', 'latitude'),
}),
textField('dec_lat', {
type: 'number',
handleSetEditData: value => handleCoordinateChange(value, 'dec', 'latitude'),
}),
],
[
'Longitude',
textField('dms_long', {
type: 'text',
handleSetEditData: value => handleCoordinateChange(value, 'dms', 'longitude'),
}),
textField('dec_long', {
type: 'number',
handleSetEditData: value => handleCoordinateChange(value, 'dec', 'longitude'),
}),
],
[
'Approximate Coordinates',
radioSelection(
'approx_coord',
[
{ value: 'false', display: 'No' },
{ value: 'true', display: 'Yes' },
],
'Approximate Coordinates'
),
],
['Altitude (m)', textField('altitude')],
]
const {
register,
formState: { errors },
} = useForm()
const columns: MRT_ColumnDef<LocalitySynonym>[] = [
{
accessorKey: 'synonym',
header: 'Synonym',
},
]
// eslint-disable-next-line @typescript-eslint/require-await
const onSave = async () => {
// TODO: Saving logic here (add Synonym to editData)
return Object.keys(errors).length === 0
}
const editingModal = (
<EditingModal buttonText="Add new Synonym" onSave={onSave}>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '1em' }}>
<TextField {...register('synonym', { required: true })} label="Synonym" required />
</Box>
</EditingModal>
)
// Kumpula Coordinates, could be changed later to be existing coordinates if exists
const [markerCoordinates, setMarkerCoordinates] = useState({ lat: 60.202665856, lng: 24.957662836 })
// eslint-disable-next-line @typescript-eslint/require-await
const onCoordinateSelectorSave = async () => {
setEditData({
...editData,
dec_lat: markerCoordinates.lat,
dms_lat: convertDecToDms(markerCoordinates.lat, 'latitude'),
dec_long: markerCoordinates.lng,
dms_long: convertDecToDms(markerCoordinates.lng, 'longitude'),
})
return Object.keys(errors).length === 0 //no idea if this is needed, just copypasted
}
// TODO name this better, plagiarized from editingModal
const coordinateButton = (
<EditingModal buttonText="Get Coordinates" onSave={onCoordinateSelectorSave}>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '1em' }}>
<CoordinateSelectionMap markerCoordinates={markerCoordinates} setMarkerCoordinates={setMarkerCoordinates} />
</Box>
</EditingModal>
)
return (
<>
<HalfFrames>
<ArrayFrame half array={info} title="Info" />
<ArrayFrame half array={country} title="Country" />
</HalfFrames>
<Grouped>
<ArrayFrame array={latlong} title="Latitude & Longitude" />
{!mode.read && coordinateButton}
</Grouped>
<Grouped title="Synonyms">
{!mode.read && editingModal}
<EditableTable<Editable<LocalitySynonym>, LocalityDetailsType> columns={columns} field="now_syn_loc" />
</Grouped>
</>
)
}