diff --git a/src/front/Components/Layouts/Users/UserInformations/index.tsx b/src/front/Components/Layouts/Users/UserInformations/index.tsx index dbb3f4b2..f0cdd1a2 100644 --- a/src/front/Components/Layouts/Users/UserInformations/index.tsx +++ b/src/front/Components/Layouts/Users/UserInformations/index.tsx @@ -33,6 +33,37 @@ export default function UserInformations(props: IProps) { const [currentAppointment, setCurrentAppointment] = useState(null); + /** When page change, get the user of the page */ + + const getUser = useCallback(async () => { + if (!userUid) return; + const user = await Users.getInstance().getByUid(userUid as string, { + q: { + contact: true, + office_role: true, + office_membership: true, + role: true, + appointment: { + include: { + votes: true, + }, + }, + votes: true, + }, + }); + if (!user) return; + + console.log(user); + const roles = await OfficeRoles.getInstance().get(); + if (!roles) return; + setAvailableRoles(roles.map((role) => ({ value: role.uid, label: role.name }))); + setUserSelected(user); + }, [userUid]); + + useEffect(() => { + getUser(); + }, [getUser, userUid]); + useEffect(() => { if (!userSelected) return; setCurrentAppointment(userSelected?.appointment?.find((appointment) => appointment.status === EAppointmentStatus.OPEN) ?? null); @@ -84,19 +115,19 @@ export default function UserInformations(props: IProps) { if (!userSelected) return; let vote = Vote.hydrate({ - appointment: - currentAppointment ?? - Appointment.hydrate({ - targeted_user: User.hydrate({ - uid: userSelected.uid, - }), - choice: superAdminModalType === "add" ? EVote.NOMINATE : EVote.DISMISS, + appointment: Appointment.hydrate({ + uid: currentAppointment?.uid ?? undefined, + targeted_user: User.hydrate({ + uid: userSelected.uid, }), + choice: superAdminModalType === "add" ? EVote.NOMINATE : EVote.DISMISS, + }), }); - await LiveVotes.getInstance().post(vote); + const votes = await LiveVotes.getInstance().post(vote); + await getUser(); setIsSuperAdminModalOpened(false); - }, [userSelected, currentAppointment, superAdminModalType]); + }, [userSelected, currentAppointment, superAdminModalType, getUser]); /** Reset switch state when userSelect change */ useEffect(() => { @@ -104,33 +135,6 @@ export default function UserInformations(props: IProps) { setIsSuperAdminChecked(userSelected.role?.name === "super-admin"); setIsAdminChecked(userSelected.role?.name === "admin" && !userSelected.office_role); }, [userSelected]); - - /** When page change, get the user of the page */ - useEffect(() => { - async function getUser() { - if (!userUid) return; - const user = await Users.getInstance().getByUid(userUid as string, { - q: { - contact: true, - office_role: true, - office_membership: true, - role: true, - appointment: true, - votes: true, - }, - }); - if (!user) return; - - console.log(user); - const roles = await OfficeRoles.getInstance().get(); - if (!roles) return; - setAvailableRoles(roles.map((role) => ({ value: role.uid, label: role.name }))); - setUserSelected(user); - } - - getUser(); - }, [userUid]); - return (
@@ -198,13 +202,17 @@ export default function UserInformations(props: IProps) {
- 1/3 + {currentAppointment.votes?.length}/3
{currentAppointment.choice === EVote.NOMINATE - ? `Vous avez voté pour attribuer le titre de Super Admin. Il manque 2 votes pour que le collaborateur se voit attribuer le titre.` - : `Vous avez voté pour retirer le titre de Super Admin. Il manque 2 votes pour que le collaborateur se voit retirer le titre.`} + ? `Vous avez voté pour attribuer le titre de Super Admin. Il manque ${ + 3 - currentAppointment.votes?.length! + } vote(s) pour que le collaborateur se voit attribuer le titre.` + : `Vous avez voté pour retirer le titre de Super Admin. Il manque ${ + 3 - currentAppointment.votes?.length! + } vote(s) pour que le collaborateur se voit retirer le titre.`}