Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature parametrer phases vil 316 #910

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/api/villages/villages.put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useMutation } from 'react-query';
import type { Village } from 'server/entities/village';

import { axiosRequest } from 'src/utils/axiosRequest';

export const updateVillage = async (id: number, villageData: Partial<Village>) => {
const response = await axiosRequest({
method: 'PUT',
url: `/villages/${id}`,
data: villageData,
});

return response.data;
};

export const useUpdateVillage = (args: { id: number; villageData: Partial<Village> }) => {
const { id, villageData } = args;
return useMutation({
mutationFn: () => {
return updateVillage(id, villageData);
},
});
};

export const useUpdateVillages = () => {
return useMutation({
mutationFn: async (village: { id: number; villageData: Partial<Village> }) => {
return await updateVillage(village.id, village.villageData);
},
});
};
48 changes: 0 additions & 48 deletions src/components/admin/managa/settings/SavePhasesModal.tsx

This file was deleted.

70 changes: 70 additions & 0 deletions src/components/admin/manage/settings/SavePhasesModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useSnackbar } from 'notistack';
import React, { useState, useEffect } from 'react';

import { useUpdateVillages } from 'src/api/villages/villages.put';
import { Modal } from 'src/components/Modal';
import type { VillagePhase } from 'types/village.type';

interface SavePhasesModalProps {
villagePhases: { [villageId: number]: VillagePhase };
isModalOpen: boolean;
setIsModalOpen: (val: boolean) => void;
}

export function SavePhasesModal({ villagePhases, isModalOpen, setIsModalOpen }: SavePhasesModalProps) {
const [isModalLoading, setIsModalLoading] = useState(false);
const updateVillages = useUpdateVillages();
const { enqueueSnackbar } = useSnackbar();

const handleConfirm = async (villagePhases: { [villageId: number]: VillagePhase }) => {
const promises = [];
for (const key in villagePhases) {
const villageId: number = +key;
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);
}
}, [updateVillages.isLoading, updateVillages.isSuccess, updateVillages.isError, setIsModalOpen, enqueueSnackbar]);

return (
<Modal
open={isModalOpen}
noCloseButton={true}
maxWidth="sm"
title="Es-tu sûr ?"
onConfirm={async () => handleConfirm(villagePhases)}
onClose={() => {
setIsModalOpen(false);
}}
loading={isModalLoading}
cancelLabel="Annuler"
confirmLabel="Enregistrer"
color="primary"
ariaLabelledBy="phaseModal"
ariaDescribedBy="Modal de validation des phases"
>
<div id="brouillon-desc" style={{ padding: '0.5rem' }}>
<p>Les modifications que tu souhaites apporter vont modifier les phases actives.</p>
</div>
</Modal>
);
}
4 changes: 2 additions & 2 deletions src/pages/admin/newportal/manage/settings/phases/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useState, useEffect } from 'react';
import { Button, Checkbox, Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@mui/material';

import { useGetVillages } from 'src/api/villages/villages.get';
import { SavePhasesModal } from 'src/components/admin/managa/settings/SavePhasesModal';
import { SavePhasesModal } from 'src/components/admin/manage/settings/SavePhasesModal';
import { UserContext } from 'src/contexts/userContext';
import { primaryColor } from 'src/styles/variables.const';
import BackArrow from 'src/svg/back-arrow.svg';
Expand All @@ -17,7 +17,7 @@ const Phases = () => {
const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN];
const [villagePhases, setVillagePhases] = useState<{ [villageId: number]: VillagePhase }>({});
const villages = useGetVillages();
const [isModalOpen, setIsModalOpen] = React.useState<boolean>(false);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);

useEffect(() => {
if (villages.data) {
Expand Down
Loading