import WarningIcon from "@Assets/images/warning.png"; //import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; import Roles from "@Front/Api/LeCoffreApi/Admin/Roles/Roles"; import LiveVotes from "@Front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes"; import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; import Confirm from "@Front/Components/DesignSystem/OldModal/Confirm"; import Switch from "@Front/Components/DesignSystem/Switch"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultUserDashboard from "@Front/Components/LayoutTemplates/DefaultUserDashboard"; import JwtService from "@Front/Services/JwtService/JwtService"; import Toasts from "@Front/Stores/Toasts"; import User, { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin"; import { EAppointmentStatus, EVote } from "le-coffre-resources/dist/SuperAdmin/Appointment"; import Image from "next/image"; import { useRouter } from "next/router"; import { useCallback, useEffect, useState } from "react"; import classes from "./classes.module.scss"; import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; import Loader from "@Front/Components/DesignSystem/Loader"; type IProps = {}; export default function UserInformations(props: IProps) { const router = useRouter(); let { userUid } = router.query; const [userSelected, setUserSelected] = useState(null); const [isSuperAdminModalOpened, setIsSuperAdminModalOpened] = useState(false); const [superAdminModalType, setSuperAdminModalType] = useState<"add" | "remove">("add"); const [adminModalType, setAdminModalType] = useState<"add" | "remove">("add"); const [isSuperAdminChecked, setIsSuperAdminChecked] = useState(false); const [isAdminChecked, setIsAdminChecked] = useState(false); const [isAdminModalOpened, setIsAdminModalOpened] = useState(false); const [currentAppointment, setCurrentAppointment] = useState(null); const [isLoading, setIsLoading] = useState(true); /** When page change, get the user of the page */ const getUser = useCallback(async () => { if (!userUid) return; setIsLoading(true); const user = await Users.getInstance().getByUid(userUid as string, { q: { contact: true, office_role: true, office_membership: true, role: true, appointment: { include: { votes: { include: { voter: true, }, }, }, }, votes: true, }, }); if (!user) return; const roles = await OfficeRoles.getInstance().get({ where: { office: { uid: user.office_membership?.uid }, NOT: { OR: [{ name: "super-admin" }, { name: "admin" }] }, }, }); if (!roles) return; setIsLoading(false); setUserSelected(user); }, [userUid]); useEffect(() => { getUser(); }, [getUser, userUid]); useEffect(() => { if (!userSelected) return; setCurrentAppointment( userSelected?.appointment?.find( (appointment) => appointment.status === EAppointmentStatus.OPEN && appointment.votes?.length != 0, ) ?? null, ); }, [userSelected]); /** Functions for the admin modal */ const openAdminModal = () => { setIsAdminModalOpened(true); }; const closeAdminModal = useCallback(() => { setIsAdminModalOpened(false); setIsAdminChecked(userSelected?.role?.name === "admin"); }, [userSelected]); const handleAdminChanged = (checked: boolean) => { setIsAdminChecked(checked); setAdminModalType(checked ? "add" : "remove"); openAdminModal(); }; const handleAdminModalAccepted = useCallback(async () => { if (!userSelected) return; if (adminModalType === "add") { const adminRole = await Roles.getInstance().getOne({ where: { name: "admin", }, }); if (!adminRole) return; await Users.getInstance().put( userSelected?.uid as string, User.hydrate({ uid: userSelected?.uid as string, office_role: undefined, role: adminRole, }), ); } else { const defaultRole = await Roles.getInstance().getOne({ where: { name: "default", }, }); if (!defaultRole) return; await Users.getInstance().put( userSelected?.uid as string, User.hydrate({ uid: userSelected?.uid as string, office_role: undefined, role: defaultRole, }), ); } getUser(); setIsAdminModalOpened(false); }, [userSelected, adminModalType, getUser]); /** Functions for the super admin modal */ const openSuperAdminModal = () => { setIsSuperAdminModalOpened(true); }; const closeSuperAdminModal = useCallback(() => { setIsSuperAdminModalOpened(false); setIsSuperAdminChecked(userSelected?.role?.name === "super-admin"); }, [userSelected]); const handleSuperAdminChanged = (checked: boolean) => { setIsSuperAdminChecked(checked); setSuperAdminModalType(checked ? "add" : "remove"); openSuperAdminModal(); }; const handleSuperAdminModalAccepted = useCallback(async () => { if (!userSelected) return; let vote = Vote.hydrate({ appointment: Appointment.hydrate({ uid: currentAppointment?.uid ?? undefined, user: User.hydrate({ uid: userSelected.uid, }), choice: superAdminModalType === "add" ? EVote.NOMINATE : EVote.DISMISS, }), }); const liveVote = await LiveVotes.getInstance().post(vote); if (liveVote.appointment.votes?.length === 3) { if (superAdminModalType === "add") { Toasts.getInstance().open({ title: `Le titre de super-administrateur a été attribué à ${userSelected.contact?.first_name} ${userSelected.contact?.last_name} `, }); } else { Toasts.getInstance().open({ title: `Le titre de super-administrateur a été retiré à ${userSelected.contact?.first_name} ${userSelected.contact?.last_name} `, }); } } await getUser(); setIsSuperAdminModalOpened(false); }, [userSelected, currentAppointment, superAdminModalType, getUser]); /** Reset switch state when userSelect change */ useEffect(() => { if (!userSelected) return; setIsSuperAdminChecked(userSelected.role?.name === "super-admin"); setIsAdminChecked(userSelected.role?.name === "admin"); }, [userSelected]); const userHasVoted = useCallback(() => { if (!currentAppointment) return false; const user = JwtService.getInstance().decodeJwt(); return currentAppointment.votes?.find((vote) => vote.voter?.uid === user?.userId) !== undefined; }, [currentAppointment]); const deleteMyVote = useCallback(async () => { if (!currentAppointment) return; const user = JwtService.getInstance().decodeJwt(); const vote = currentAppointment.votes?.find((vote) => vote.voter?.uid === user?.userId); if (!vote) return; await LiveVotes.getInstance().delete({ uid: vote.uid }); await getUser(); }, [currentAppointment, getUser]); return ( {!isLoading && (
{userSelected?.contact?.first_name + " " + userSelected?.contact?.last_name} Office {userSelected?.office_membership?.name.toLocaleUpperCase()}
Nom {userSelected?.contact?.first_name}
Prénom {userSelected?.contact?.last_name}
Numéro de téléphone {userSelected?.contact?.cell_phone_number}
Email {userSelected?.contact?.email}
Rôle au sein de son office
{userSelected?.office_role ? userSelected?.office_role?.name : "Utilisateur restreint"}
Attribuer un titre
{!isSuperAdminChecked && !currentAppointment && ( )} {currentAppointment && (
warning
{currentAppointment.votes?.length}/3
{currentAppointment.choice === EVote.NOMINATE ? `Un ou des collaborateurs souhaitent attribuer le titre de Super Admin à ce collaborateur. Il manque ${ 3 - currentAppointment.votes?.length! } vote(s) pour que le collaborateur se voit attribuer le titre.` : `Un ou des collaborateurs souhaitent retirer le titre de Super Admin à ce collaborateur. Il manque ${ 3 - currentAppointment.votes?.length! } vote(s) pour que le collaborateur se voit retirer le titre.`}
{userHasVoted() && (
)}
)}
{superAdminModalType === "add" ? "Nommer" : "Retirer"} une personne Super Administrateur nécessite 3 votes de super administrateurs existants. Souhaitez-vous attribuer un vote ?
)} {isLoading && (
)}
); }