From bead9ce1bc48faa130ffca4a8293258d58b2e11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez=20Mu=C3=B1oz?= Date: Wed, 4 Sep 2024 18:43:42 +0200 Subject: [PATCH] feature bulking: added edition modes --- .../bulk-action-menu/modals/edit/index.tsx | 150 ++++++++++++------ 1 file changed, 100 insertions(+), 50 deletions(-) diff --git a/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit/index.tsx b/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit/index.tsx index 5a95be5a51..840ca73a74 100644 --- a/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit/index.tsx +++ b/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/bulk-action-menu/modals/edit/index.tsx @@ -10,14 +10,22 @@ import { useToasts } from 'hooks/toast'; import Button from 'components/button'; import Field from 'components/forms/field'; import Label from 'components/forms/label'; +import Radio from 'components/forms/radio'; import { composeValidators } from 'components/forms/validations'; import { Feature } from 'types/api/feature'; export type FormValues = { target: number; spf: number; + mode: 'all' | 'only-target' | 'only-spf'; }; +const EDITION_MODES = [ + { value: 'all', label: 'Target & SPF' }, + { value: 'only-target', label: 'Target' }, + { value: 'only-spf', label: 'SPF' }, +]; + const INPUT_CLASSES = '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'; @@ -75,7 +83,7 @@ const EditModal = ({ const onEditSubmit = useCallback( (values: FormValues) => { - const { target, spf = 1 } = values; + const { target, spf = 1, mode } = values; const data = { status: 'created', @@ -117,14 +125,34 @@ const EditModal = ({ }; } + let newMarxanSettings = sf.marxanSettings; + + if (mode === 'only-target') { + newMarxanSettings = { + ...newMarxanSettings, + prop: target / 100, + }; + } + + if (mode === 'only-spf') { + newMarxanSettings = { + ...newMarxanSettings, + fpf: +spf, + }; + } + + if (mode === 'all') { + newMarxanSettings = { + prop: target / 100, + fpf: +spf, + }; + } + return { featureId, kind, marxanSettings: selectedFeatures.find((f) => f.id === featureId) - ? { - prop: target / 100 || 0.5, - fpf: +spf, - } + ? newMarxanSettings : sf.marxanSettings, }; }), @@ -181,65 +209,87 @@ const EditModal = ({ return ( initialValues={{ + mode: 'all', target: (selectedFeatures?.length === 1 && selectedFeatures?.[0]?.marxanSettings?.prop) || 50, spf: (selectedFeatures?.length === 1 && selectedFeatures?.[0]?.marxanSettings?.fpf) || 1, }} - ref={formRef} onSubmit={onEditSubmit} render={({ form, handleSubmit }) => { formRef.current = form; + const currentMode = form?.getState()?.values?.mode; + return (

Edit selected features

+
+ {EDITION_MODES.map(({ value, label }) => { + return ( + + {(fprops) => ( +
+ + +
+ )} +
+ ); + })} +
+
- - name="target" - validate={composeValidators([{ presence: true }])} - > - {(fprops) => ( - - - - - - )} - - - - name="spf" - validate={composeValidators([{ presence: true }])} - > - {(fprops) => ( - - - - - - )} - + {['only-target', 'all'].includes(currentMode) && ( + + name="target" + validate={composeValidators([{ presence: true }])} + > + {(fprops) => ( + + + + + + )} + + )} + {['only-spf', 'all'].includes(currentMode) && ( + + name="spf" + validate={composeValidators([{ presence: true }])} + > + {(fprops) => ( + + + + + + )} + + )}