@@ -12,71 +12,95 @@ import { set } from '../../../config/configSlice';
12
12
import { callHandler } from '../../../handlers/actions' ;
13
13
14
14
export default function LocationBased ( { field } ) {
15
- const [ locationValue , setLocationValue ] = useState ( {
15
+ const [ value , setValue ] = useState ( {
16
16
// Default to Brooklyn, because that's where tidbyt folks
17
17
// are and we can only dispatch a location object which
18
18
// has all fields set.
19
19
'lat' : 40.678 ,
20
20
'lng' : - 73.944 ,
21
21
'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
23
27
} ) ;
24
28
25
- const [ value , setValue ] = useState ( field . default ) ;
26
-
27
29
const config = useSelector ( state => state . config ) ;
28
30
const dispatch = useDispatch ( ) ;
29
31
const handlerResults = useSelector ( state => state . handlers )
30
32
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
+
31
42
useEffect ( ( ) => {
32
43
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 ) ) ;
34
47
} else if ( field . default ) {
48
+ value = field . default ;
35
49
dispatch ( set ( {
36
50
id : field . id ,
37
- value : field . default ,
51
+ value : JSON . stringify ( field . default ) ,
38
52
} ) ) ;
39
53
}
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 ) ) ;
48
71
}
49
72
50
73
const truncateLatLng = ( value ) => {
51
74
return String ( Number ( value ) . toFixed ( 3 ) ) ;
52
75
}
53
76
54
77
const onChangeLatitude = ( event ) => {
55
- setPart ( 'lat' , truncateLatLng ( event . target . value ) ) ;
78
+ setLocationPart ( 'lat' , truncateLatLng ( event . target . value ) ) ;
56
79
}
57
80
58
81
const onChangeLongitude = ( event ) => {
59
- setPart ( 'lng' , truncateLatLng ( event . target . value ) ) ;
82
+ setLocationPart ( 'lng' , truncateLatLng ( event . target . value ) ) ;
60
83
}
61
84
62
85
const onChangeLocality = ( event ) => {
63
- setPart ( 'locality' , event . target . value ) ;
86
+ setLocationPart ( 'locality' , event . target . value ) ;
64
87
}
65
88
66
89
const onChangeTimezone = ( event ) => {
67
- setPart ( 'timezone' , event . target . value ) ;
90
+ setLocationPart ( 'timezone' , event . target . value ) ;
68
91
}
69
92
70
93
const onChangeOption = ( event ) => {
71
- setValue ( event . target . value ) ;
94
+ let newValue = { ...value } ;
95
+ newValue . value = event . target . value ;
72
96
const selectedOption = options . find ( option => option . value === event . target . value ) ;
73
- let option = { 'value' : event . target . value } ;
74
97
if ( selectedOption ) {
75
- option [ ' display' ] = selectedOption . display ;
98
+ newValue . display = selectedOption . display ;
76
99
}
100
+ setValue ( newValue ) ;
77
101
dispatch ( set ( {
78
102
id : field . id ,
79
- value : JSON . stringify ( option )
103
+ value : JSON . stringify ( newValue ) ,
80
104
} ) ) ;
81
105
}
82
106
@@ -93,7 +117,7 @@ export default function LocationBased({ field }) {
93
117
max = { 90 }
94
118
step = { 0.1 }
95
119
onChange = { onChangeLatitude }
96
- value = { locationValue [ 'lat' ] }
120
+ value = { value [ 'lat' ] }
97
121
>
98
122
</ InputSlider >
99
123
< Typography > Longitude</ Typography >
@@ -102,7 +126,7 @@ export default function LocationBased({ field }) {
102
126
max = { 180 }
103
127
step = { 0.1 }
104
128
onChange = { onChangeLongitude }
105
- value = { locationValue [ 'lng' ] }
129
+ value = { value [ 'lng' ] }
106
130
>
107
131
</ InputSlider >
108
132
< Typography > Locality</ Typography >
@@ -111,13 +135,13 @@ export default function LocationBased({ field }) {
111
135
variant = "outlined"
112
136
onChange = { onChangeLocality }
113
137
style = { { marginBottom : '0.5rem' } }
114
- value = { locationValue [ 'locality' ] }
138
+ value = { value [ 'locality' ] }
115
139
/>
116
140
< Typography > Timezone</ Typography >
117
141
< Select
118
142
onChange = { onChangeTimezone }
119
143
style = { { marginBottom : '0.5rem' } }
120
- value = { locationValue [ 'timezone' ] }
144
+ value = { value [ 'timezone' ] }
121
145
>
122
146
{ Intl . supportedValuesOf ( 'timeZone' ) . map ( ( zone ) => {
123
147
return < MenuItem value = { zone } > { zone } </ MenuItem >
@@ -126,7 +150,7 @@ export default function LocationBased({ field }) {
126
150
< Typography > Options for chosen location</ Typography >
127
151
< Select
128
152
onChange = { onChangeOption }
129
- value = { value }
153
+ value = { value [ 'value' ] }
130
154
>
131
155
{ options . map ( ( option ) => {
132
156
return < MenuItem key = { option . value } value = { option . value } > { option . display } </ MenuItem >
0 commit comments