@@ -10,14 +10,22 @@ import { useToasts } from 'hooks/toast';
10
10
import Button from 'components/button' ;
11
11
import Field from 'components/forms/field' ;
12
12
import Label from 'components/forms/label' ;
13
+ import Radio from 'components/forms/radio' ;
13
14
import { composeValidators } from 'components/forms/validations' ;
14
15
import { Feature } from 'types/api/feature' ;
15
16
16
17
export type FormValues = {
17
18
target : number ;
18
19
spf : number ;
20
+ mode : 'all' | 'only-target' | 'only-spf' ;
19
21
} ;
20
22
23
+ const EDITION_MODES = [
24
+ { value : 'all' , label : 'Target & SPF' } ,
25
+ { value : 'only-target' , label : 'Target' } ,
26
+ { value : 'only-spf' , label : 'SPF' } ,
27
+ ] ;
28
+
21
29
const INPUT_CLASSES =
22
30
'h-10 w-full rounded-md border border-gray-400 px-3 text-gray-900 focus:border-none focus:outline-none focus:ring-1 focus:ring-blue-600' ;
23
31
@@ -75,7 +83,7 @@ const EditModal = ({
75
83
76
84
const onEditSubmit = useCallback (
77
85
( values : FormValues ) => {
78
- const { target, spf = 1 } = values ;
86
+ const { target, spf = 1 , mode } = values ;
79
87
80
88
const data = {
81
89
status : 'created' ,
@@ -117,14 +125,34 @@ const EditModal = ({
117
125
} ;
118
126
}
119
127
128
+ let newMarxanSettings = sf . marxanSettings ;
129
+
130
+ if ( mode === 'only-target' ) {
131
+ newMarxanSettings = {
132
+ ...newMarxanSettings ,
133
+ prop : target / 100 ,
134
+ } ;
135
+ }
136
+
137
+ if ( mode === 'only-spf' ) {
138
+ newMarxanSettings = {
139
+ ...newMarxanSettings ,
140
+ fpf : + spf ,
141
+ } ;
142
+ }
143
+
144
+ if ( mode === 'all' ) {
145
+ newMarxanSettings = {
146
+ prop : target / 100 ,
147
+ fpf : + spf ,
148
+ } ;
149
+ }
150
+
120
151
return {
121
152
featureId,
122
153
kind,
123
154
marxanSettings : selectedFeatures . find ( ( f ) => f . id === featureId )
124
- ? {
125
- prop : target / 100 || 0.5 ,
126
- fpf : + spf ,
127
- }
155
+ ? newMarxanSettings
128
156
: sf . marxanSettings ,
129
157
} ;
130
158
} ) ,
@@ -181,65 +209,87 @@ const EditModal = ({
181
209
return (
182
210
< FormRFF < FormValues >
183
211
initialValues = { {
212
+ mode : 'all' ,
184
213
target :
185
214
( selectedFeatures ?. length === 1 && selectedFeatures ?. [ 0 ] ?. marxanSettings ?. prop ) || 50 ,
186
215
spf : ( selectedFeatures ?. length === 1 && selectedFeatures ?. [ 0 ] ?. marxanSettings ?. fpf ) || 1 ,
187
216
} }
188
- ref = { formRef }
189
217
onSubmit = { onEditSubmit }
190
218
render = { ( { form, handleSubmit } ) => {
191
219
formRef . current = form ;
192
220
221
+ const currentMode = form ?. getState ( ) ?. values ?. mode ;
222
+
193
223
return (
194
224
< form onSubmit = { handleSubmit } className = "relative" >
195
225
< div className = "flex flex-col space-y-5 px-8 py-1" >
196
226
< h2 className = "font-heading font-bold text-black" > Edit selected features</ h2 >
197
227
228
+ < div className = "flex space-x-2" >
229
+ { EDITION_MODES . map ( ( { value, label } ) => {
230
+ return (
231
+ < FieldRFF key = { value } name = "mode" type = "radio" value = { value } >
232
+ { ( fprops ) => (
233
+ < div className = "flex space-x-2" >
234
+ < Radio theme = "light" id = { value } { ...fprops . input } />
235
+ < Label theme = "light" id = { value } className = "ml-2" >
236
+ { label }
237
+ </ Label >
238
+ </ div >
239
+ ) }
240
+ </ FieldRFF >
241
+ ) ;
242
+ } ) }
243
+ </ div >
244
+
198
245
< div className = "flex w-full space-x-2" >
199
- < FieldRFF < FormValues [ 'target' ] >
200
- name = "target"
201
- validate = { composeValidators ( [ { presence : true } ] ) }
202
- >
203
- { ( fprops ) => (
204
- < Field id = "target" { ...fprops } className = "flex-1" >
205
- < Label theme = "light" className = "mb-3 text-xs font-semibold uppercase" >
206
- Target (%)
207
- </ Label >
208
-
209
- < input
210
- { ...fprops . input }
211
- type = "number"
212
- className = { INPUT_CLASSES }
213
- defaultValue = { fprops . input . value }
214
- min = { 0 }
215
- max = { 100 }
216
- step = { 0.01 }
217
- />
218
- </ Field >
219
- ) }
220
- </ FieldRFF >
221
-
222
- < FieldRFF < FormValues [ 'spf' ] >
223
- name = "spf"
224
- validate = { composeValidators ( [ { presence : true } ] ) }
225
- >
226
- { ( fprops ) => (
227
- < Field id = "spf" { ...fprops } className = "flex-1" >
228
- < Label theme = "light" className = "mb-3 text-xs font-semibold uppercase" >
229
- SPF
230
- </ Label >
231
-
232
- < input
233
- { ...fprops . input }
234
- type = "number"
235
- className = { INPUT_CLASSES }
236
- defaultValue = { fprops . input . value }
237
- min = { 1 }
238
- step = { 0.01 }
239
- />
240
- </ Field >
241
- ) }
242
- </ FieldRFF >
246
+ { [ 'only-target' , 'all' ] . includes ( currentMode ) && (
247
+ < FieldRFF < FormValues [ 'target' ] >
248
+ name = "target"
249
+ validate = { composeValidators ( [ { presence : true } ] ) }
250
+ >
251
+ { ( fprops ) => (
252
+ < Field id = "target" { ...fprops } className = "flex-1" >
253
+ < Label theme = "light" className = "mb-3 text-xs font-semibold uppercase" >
254
+ Target (%)
255
+ </ Label >
256
+
257
+ < input
258
+ { ...fprops . input }
259
+ type = "number"
260
+ className = { INPUT_CLASSES }
261
+ defaultValue = { fprops . input . value }
262
+ min = { 0 }
263
+ max = { 100 }
264
+ step = { 0.01 }
265
+ />
266
+ </ Field >
267
+ ) }
268
+ </ FieldRFF >
269
+ ) }
270
+ { [ 'only-spf' , 'all' ] . includes ( currentMode ) && (
271
+ < FieldRFF < FormValues [ 'spf' ] >
272
+ name = "spf"
273
+ validate = { composeValidators ( [ { presence : true } ] ) }
274
+ >
275
+ { ( fprops ) => (
276
+ < Field id = "spf" { ...fprops } className = "flex-1" >
277
+ < Label theme = "light" className = "mb-3 text-xs font-semibold uppercase" >
278
+ SPF
279
+ </ Label >
280
+
281
+ < input
282
+ { ...fprops . input }
283
+ type = "number"
284
+ className = { INPUT_CLASSES }
285
+ defaultValue = { fprops . input . value }
286
+ min = { 1 }
287
+ step = { 0.01 }
288
+ />
289
+ </ Field >
290
+ ) }
291
+ </ FieldRFF >
292
+ ) }
243
293
</ div >
244
294
245
295
< div className = "mt-16 flex justify-center space-x-6" >
0 commit comments