Votes working

This commit is contained in:
Maxime Lalo 2023-07-27 14:40:29 +02:00
parent 1c3460ffeb
commit 98acafb1fd

View File

@ -33,6 +33,37 @@ export default function UserInformations(props: IProps) {
const [currentAppointment, setCurrentAppointment] = useState<Appointment | null>(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<Vote>({
appointment:
currentAppointment ??
Appointment.hydrate<Appointment>({
targeted_user: User.hydrate<User>({
uid: userSelected.uid,
}),
choice: superAdminModalType === "add" ? EVote.NOMINATE : EVote.DISMISS,
appointment: Appointment.hydrate<Appointment>({
uid: currentAppointment?.uid ?? undefined,
targeted_user: User.hydrate<User>({
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 (
<DefaultUserDashboard mobileBackText={"Liste des utilisateurs"}>
<div className={classes["root"]}>
@ -198,13 +202,17 @@ export default function UserInformations(props: IProps) {
</div>
<div className={classes["right"]}>
<div>
<Typography typo={ITypo.P_SB_18}>1/3</Typography>
<Typography typo={ITypo.P_SB_18}>{currentAppointment.votes?.length}/3</Typography>
</div>
<div>
<Typography typo={ITypo.CAPTION_14}>
{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.`}
</Typography>
</div>
</div>