Skip to content

Commit

Permalink
Merge pull request #1614 from Vizzuality/MRXN23-547-back-after-saving
Browse files Browse the repository at this point in the history
[FE](fix): remove automatic back action when saved PAs and show save btn only when necessary [MRXN23-547]
  • Loading branch information
anamontiaga authored Dec 22, 2023
2 parents 26990b9 + 5bd4613 commit 9a7e185
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useDebouncedCallback } from 'use-debounce';

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

import Button from 'components/button';
import ConfirmationPrompt from 'components/confirmation-prompt';
Expand All @@ -26,6 +26,8 @@ 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 All @@ -39,11 +41,6 @@ export const ScenariosFeaturesTargets = ({ onGoBack }: { onGoBack: () => void })
const editable = useCanEditScenario(pid, sid);

const selectedFeaturesMutation = useSaveSelectedFeatures({});
const saveScenarioMutation = useSaveScenario({
requestConfig: {
method: 'PATCH',
},
});

const { data: selectedFeaturesData } = useSelectedFeatures(sid, {});

Expand All @@ -53,9 +50,6 @@ export const ScenariosFeaturesTargets = ({ onGoBack }: { onGoBack: () => void })
isFetched: targetedFeaturesIsFetched,
} = useTargetedFeatures(sid);

const { data: scenarioData } = useScenario(sid);
const { metadata } = scenarioData || {};

const INITIAL_VALUES = useMemo(() => {
return {
features: targetedFeaturesData,
Expand Down Expand Up @@ -137,76 +131,93 @@ export const ScenariosFeaturesTargets = ({ onGoBack }: { onGoBack: () => void })
}, []);

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

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

if (kind === 'withGeoprocessing') {
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);
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,
},
};
}),
};
}),
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
};
}
}),
};

const { target, fpf = 1 } = features.find((f) => f.featureId === featureId);
return {
featureId,
kind,
marxanSettings: {
prop: target / 100 || 0.5,
fpf,
},
};
}),
};

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

const toggleSeeOnMap = useCallback(
Expand Down
Loading

1 comment on commit 9a7e185

@vercel
Copy link

@vercel vercel bot commented on 9a7e185 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marxan – ./

marxan-git-develop-vizzuality1.vercel.app
marxan-vizzuality1.vercel.app
marxan23.vercel.app

Please sign in to comment.