Skip to content

Commit bead9ce

Browse files
committed
feature bulking: added edition modes
1 parent 481218f commit bead9ce

File tree

1 file changed

+100
-50
lines changed
  • app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit

1 file changed

+100
-50
lines changed

app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit/index.tsx

Lines changed: 100 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ import { useToasts } from 'hooks/toast';
1010
import Button from 'components/button';
1111
import Field from 'components/forms/field';
1212
import Label from 'components/forms/label';
13+
import Radio from 'components/forms/radio';
1314
import { composeValidators } from 'components/forms/validations';
1415
import { Feature } from 'types/api/feature';
1516

1617
export type FormValues = {
1718
target: number;
1819
spf: number;
20+
mode: 'all' | 'only-target' | 'only-spf';
1921
};
2022

23+
const EDITION_MODES = [
24+
{ value: 'all', label: 'Target & SPF' },
25+
{ value: 'only-target', label: 'Target' },
26+
{ value: 'only-spf', label: 'SPF' },
27+
];
28+
2129
const INPUT_CLASSES =
2230
'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';
2331

@@ -75,7 +83,7 @@ const EditModal = ({
7583

7684
const onEditSubmit = useCallback(
7785
(values: FormValues) => {
78-
const { target, spf = 1 } = values;
86+
const { target, spf = 1, mode } = values;
7987

8088
const data = {
8189
status: 'created',
@@ -117,14 +125,34 @@ const EditModal = ({
117125
};
118126
}
119127

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+
120151
return {
121152
featureId,
122153
kind,
123154
marxanSettings: selectedFeatures.find((f) => f.id === featureId)
124-
? {
125-
prop: target / 100 || 0.5,
126-
fpf: +spf,
127-
}
155+
? newMarxanSettings
128156
: sf.marxanSettings,
129157
};
130158
}),
@@ -181,65 +209,87 @@ const EditModal = ({
181209
return (
182210
<FormRFF<FormValues>
183211
initialValues={{
212+
mode: 'all',
184213
target:
185214
(selectedFeatures?.length === 1 && selectedFeatures?.[0]?.marxanSettings?.prop) || 50,
186215
spf: (selectedFeatures?.length === 1 && selectedFeatures?.[0]?.marxanSettings?.fpf) || 1,
187216
}}
188-
ref={formRef}
189217
onSubmit={onEditSubmit}
190218
render={({ form, handleSubmit }) => {
191219
formRef.current = form;
192220

221+
const currentMode = form?.getState()?.values?.mode;
222+
193223
return (
194224
<form onSubmit={handleSubmit} className="relative">
195225
<div className="flex flex-col space-y-5 px-8 py-1">
196226
<h2 className="font-heading font-bold text-black">Edit selected features</h2>
197227

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+
198245
<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+
)}
243293
</div>
244294

245295
<div className="mt-16 flex justify-center space-x-6">

0 commit comments

Comments
 (0)