Skip to content

Commit

Permalink
recover save default features
Browse files Browse the repository at this point in the history
  • Loading branch information
anamontiaga committed Jan 8, 2024
1 parent a24db6a commit eeed3e9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export const ScenariosFeaturesList = ({ onContinue }): JSX.Element => {
const scenarioSlice = getScenarioEditSlice(sid);
const { setFeatures, setLayerSettings, setSelectedFeatures, setSelectedContinuousFeatures } =
scenarioSlice.actions;
const { selectedFeatures, selectedContinuousFeatures, layerSettings } = useSelector(
const { selectedFeatures, selectedContinuousFeatures, layerSettings, features } = useSelector(
(state) => state[`/scenarios/${sid}/edit`]
);

console.log({ featuresRedux: features });

const dispatch = useDispatch();
const queryClient = useQueryClient();
const editable = useCanEditScenario(pid, sid);
Expand Down Expand Up @@ -204,6 +206,7 @@ export const ScenariosFeaturesList = ({ onContinue }): JSX.Element => {
const onSubmit = useCallback(
(values) => {
const { features } = values;
console.log({ onSubmit: features });
const data = getFeaturesRecipe(features);
setSubmitting(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { useDebouncedCallback } from 'use-debounce';

import { useSaveSelectedFeatures, useSelectedFeatures, useTargetedFeatures } from 'hooks/features';
import { useCanEditScenario } from 'hooks/permissions';
import { useToasts } from 'hooks/toast';

import Button from 'components/button';
import ConfirmationPrompt from 'components/confirmation-prompt';
Expand All @@ -26,8 +25,6 @@ export const ScenariosFeaturesTargets = ({ onGoBack }: { onGoBack: () => void })
const [confirmationTarget, setConfirmationTarget] = useState(null);
const [confirmationFPF, setConfirmationFPF] = useState(null);

const { addToast } = useToasts();

const queryClient = useQueryClient();
const { query } = useRouter();
const { pid, sid } = query as { pid: string; sid: string };
Expand Down Expand Up @@ -131,93 +128,77 @@ export const ScenariosFeaturesTargets = ({ onGoBack }: { onGoBack: () => void })
}, []);

const onSubmit = useCallback(
(values, form) => {
(values) => {
const { features } = values;

const featuresFieldTouched = form.getFieldState('features')?.dirty;

if (featuresFieldTouched) {
setSubmitting(true);

const data = {
status: 'created',
features: selectedFeaturesData.map((sf) => {
const { featureId, kind, geoprocessingOperations } = sf;

if (kind === 'withGeoprocessing') {
return {
featureId,
kind,
geoprocessingOperations: geoprocessingOperations.map((go) => {
const { splits } = go;

return {
...go,
splits: splits
.filter((s) => {
return features.find((f) => {
return f.parentId === featureId && f.value === s.value;
});
})
.map((s) => {
const { target, fpf } = features.find((f) => {
return f.parentId === featureId && f.value === s.value;
});

return {
...s,
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
};
}),
};
}),
};
}

const { target, fpf = 1 } = features.find((f) => f.featureId === featureId);
setSubmitting(true);

const data = {
status: 'created',
features: selectedFeaturesData.map((sf) => {
const { featureId, kind, geoprocessingOperations } = sf;

if (kind === 'withGeoprocessing') {
return {
featureId,
kind,
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
geoprocessingOperations: geoprocessingOperations.map((go) => {
const { splits } = go;

return {
...go,
splits: splits
.filter((s) => {
return features.find((f) => {
return f.parentId === featureId && f.value === s.value;
});
})
.map((s) => {
const { target, fpf } = features.find((f) => {
return f.parentId === featureId && f.value === s.value;
});

return {
...s,
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
};
}),
};
}),
};
}),
};
}

selectedFeaturesMutation.mutate(
{
id: `${sid}`,
data,
},
{
onSuccess: async () => {
await queryClient.invalidateQueries(['selected-features', sid]);
},
onSettled: () => {
setSubmitting(false);
const { target, fpf = 1 } = features.find((f) => f.featureId === featureId);
return {
featureId,
kind,
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
}
);
}
if (!featuresFieldTouched) {
addToast(
'save-selected-features',
<>
<h2 className="font-medium"></h2>
<p className="text-sm">No modifications have been made to the selected features.</p>
</>,
{
level: 'info',
}
);
}
};
}),
};

selectedFeaturesMutation.mutate(
{
id: `${sid}`,
data,
},
{
onSuccess: async () => {
await queryClient.invalidateQueries(['selected-features', sid]);
},
onSettled: () => {
setSubmitting(false);
},
}
);
},
[sid, queryClient, selectedFeaturesData, selectedFeaturesMutation, addToast]
[sid, queryClient, selectedFeaturesData, selectedFeaturesMutation]
);

const toggleSeeOnMap = useCallback(
Expand Down

0 comments on commit eeed3e9

Please sign in to comment.