Skip to content

Commit

Permalink
VIL-316 Paramétrer les phases
Browse files Browse the repository at this point in the history
  • Loading branch information
GaspardRivoire committed Apr 25, 2024
1 parent a1f107a commit 4645aa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/api/villages/villages.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const useUpdateVillage = (args: { id: number; villageData: Partial<Villag

export const useUpdateVillages = () => {
return useMutation({
mutationFn: (village: { id: number; villageData: Partial<Village> }) => {
return updateVillage(village.id, village.villageData);
mutationFn: async (village: { id: number; villageData: Partial<Village> }) => {
return await updateVillage(village.id, village.villageData);
},
});
};
23 changes: 16 additions & 7 deletions src/components/admin/manage/settings/SavePhasesModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSnackbar } from 'notistack';
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';

import { useUpdateVillages } from 'src/api/villages/villages.put';
import { Modal } from 'src/components/Modal';
Expand All @@ -16,25 +16,34 @@ export function SavePhasesModal({ villagePhases, isModalOpen, setIsModalOpen }:
const updateVillages = useUpdateVillages();
const { enqueueSnackbar } = useSnackbar();

const handleConfirm = (villagePhases: { [villageId: number]: VillagePhase }) => {
setIsModalLoading(true);
const handleConfirm = async (villagePhases: { [villageId: number]: VillagePhase }) => {
const promises = [];
for (const key in villagePhases) {
const villageId: number = +key;
updateVillages.mutate({ id: villageId, villageData: { activePhase: villagePhases[villageId] } });
promises.push(updateVillages.mutateAsync({ id: villageId, villageData: { activePhase: villagePhases[villageId] } }));
}
await Promise.allSettled(promises);
};

useEffect(() => {
if (updateVillages.isLoading) {
setIsModalLoading(true);
}
if (updateVillages.isSuccess) {
enqueueSnackbar('Modifications enregistrées !', {
variant: 'success',
});
setIsModalOpen(false);
setIsModalLoading(false);
}
if (updateVillages.isError) {
enqueueSnackbar("Une erreur s'est produite lors de la modifications !", {
variant: 'error',
});
setIsModalOpen(false);
setIsModalLoading(false);
}
setIsModalOpen(false);
setIsModalLoading(false);
};
}, [updateVillages.isLoading, updateVillages.isSuccess, updateVillages.isError, setIsModalOpen, enqueueSnackbar]);

return (
<Modal
Expand Down

0 comments on commit 4645aa2

Please sign in to comment.