Skip to content

Commit 40b8d7f

Browse files
committed
LocationBased: store location in the config, in addition to the selected
option
1 parent 8464943 commit 40b8d7f

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

src/features/schema/fields/location/LocationBased.jsx

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,95 @@ import { set } from '../../../config/configSlice';
1212
import { callHandler } from '../../../handlers/actions';
1313

1414
export default function LocationBased({ field }) {
15-
const [locationValue, setLocationValue] = useState({
15+
const [value, setValue] = useState({
1616
// Default to Brooklyn, because that's where tidbyt folks
1717
// are and we can only dispatch a location object which
1818
// has all fields set.
1919
'lat': 40.678,
2020
'lng': -73.944,
2121
'locality': 'Brooklyn, New York',
22-
'timezone': 'America/New_York'
22+
'timezone': 'America/New_York',
23+
// But overwrite with app-specific defaults set in config.
24+
'display': '',
25+
'value': '',
26+
...field.default
2327
});
2428

25-
const [value, setValue] = useState(field.default);
26-
2729
const config = useSelector(state => state.config);
2830
const dispatch = useDispatch();
2931
const handlerResults = useSelector(state => state.handlers)
3032

33+
const getLocationAsJson = (v) => {
34+
return JSON.stringify({
35+
lat: v.lat,
36+
lng: v.lng,
37+
locality: v.locality,
38+
timezone: v.timezone
39+
});
40+
}
41+
3142
useEffect(() => {
3243
if (field.id in config) {
33-
setValue(config[field.id].value);
44+
let v = JSON.parse(config[field.id].value);
45+
setValue(v);
46+
callHandler(field.id, field.handler, getLocationAsJson(v));
3447
} else if (field.default) {
48+
value = field.default;
3549
dispatch(set({
3650
id: field.id,
37-
value: field.default,
51+
value: JSON.stringify(field.default),
3852
}));
3953
}
40-
callHandler(field.id, field.handler, JSON.stringify(locationValue));
41-
}, [config])
42-
43-
const setPart = (partName, partValue) => {
44-
let newLocationValue = { ...locationValue };
45-
newLocationValue[partName] = partValue;
46-
setLocationValue(newLocationValue);
47-
callHandler(field.id, field.handler, JSON.stringify(newLocationValue));
54+
}, [config]);
55+
56+
useEffect(() => {
57+
if (!(field.id in config)) {
58+
callHandler(field.id, field.handler, getLocationAsJson(value));
59+
}
60+
}, []);
61+
62+
const setLocationPart = (partName, partValue) => {
63+
let newValue = { ...value };
64+
newValue[partName] = partValue;
65+
setValue(newValue);
66+
dispatch(set({
67+
id: field.id,
68+
value: JSON.stringify(newValue),
69+
}));
70+
callHandler(field.id, field.handler, getLocationAsJson(newValue));
4871
}
4972

5073
const truncateLatLng = (value) => {
5174
return String(Number(value).toFixed(3));
5275
}
5376

5477
const onChangeLatitude = (event) => {
55-
setPart('lat', truncateLatLng(event.target.value));
78+
setLocationPart('lat', truncateLatLng(event.target.value));
5679
}
5780

5881
const onChangeLongitude = (event) => {
59-
setPart('lng', truncateLatLng(event.target.value));
82+
setLocationPart('lng', truncateLatLng(event.target.value));
6083
}
6184

6285
const onChangeLocality = (event) => {
63-
setPart('locality', event.target.value);
86+
setLocationPart('locality', event.target.value);
6487
}
6588

6689
const onChangeTimezone = (event) => {
67-
setPart('timezone', event.target.value);
90+
setLocationPart('timezone', event.target.value);
6891
}
6992

7093
const onChangeOption = (event) => {
71-
setValue(event.target.value);
94+
let newValue = { ...value };
95+
newValue.value = event.target.value;
7296
const selectedOption = options.find(option => option.value === event.target.value);
73-
let option = { 'value': event.target.value };
7497
if (selectedOption) {
75-
option['display'] = selectedOption.display;
98+
newValue.display = selectedOption.display;
7699
}
100+
setValue(newValue);
77101
dispatch(set({
78102
id: field.id,
79-
value: JSON.stringify(option)
103+
value: JSON.stringify(newValue),
80104
}));
81105
}
82106

@@ -93,7 +117,7 @@ export default function LocationBased({ field }) {
93117
max={90}
94118
step={0.1}
95119
onChange={onChangeLatitude}
96-
value={locationValue['lat']}
120+
value={value['lat']}
97121
>
98122
</InputSlider>
99123
<Typography>Longitude</Typography>
@@ -102,7 +126,7 @@ export default function LocationBased({ field }) {
102126
max={180}
103127
step={0.1}
104128
onChange={onChangeLongitude}
105-
value={locationValue['lng']}
129+
value={value['lng']}
106130
>
107131
</InputSlider>
108132
<Typography>Locality</Typography>
@@ -111,13 +135,13 @@ export default function LocationBased({ field }) {
111135
variant="outlined"
112136
onChange={onChangeLocality}
113137
style={{ marginBottom: '0.5rem' }}
114-
value={locationValue['locality']}
138+
value={value['locality']}
115139
/>
116140
<Typography>Timezone</Typography>
117141
<Select
118142
onChange={onChangeTimezone}
119143
style={{ marginBottom: '0.5rem' }}
120-
value={locationValue['timezone']}
144+
value={value['timezone']}
121145
>
122146
{Intl.supportedValuesOf('timeZone').map((zone) => {
123147
return <MenuItem value={zone}>{zone}</MenuItem>
@@ -126,7 +150,7 @@ export default function LocationBased({ field }) {
126150
<Typography>Options for chosen location</Typography>
127151
<Select
128152
onChange={onChangeOption}
129-
value={value}
153+
value={value['value']}
130154
>
131155
{options.map((option) => {
132156
return <MenuItem key={option.value} value={option.value}>{option.display}</MenuItem>

0 commit comments

Comments
 (0)