From 406ebeb2ccfcf6849157ef24926f595db2b4a26e Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 25 Jul 2023 16:50:16 +0200 Subject: [PATCH] :sparkles: Admin office role add/remove --- .../CollaboratorInformations/index.tsx | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx index f2c5a7ac..ae31697a 100644 --- a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx +++ b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx @@ -26,7 +26,7 @@ export default function CollaboratorInformations(props: IProps) { const [roleModalOpened, setRoleModalOpened] = useState(false); const [adminModalOpened, setAdminModalOpened] = useState(false); - + const [adminRoleType, setAdminRoleType] = useState<"add" | "remove">("add"); const [selectedOption, setSelectedOption] = useState(null); const handleRoleChange = useCallback((option: IOption) => { @@ -56,25 +56,31 @@ export default function CollaboratorInformations(props: IProps) { }, [selectedOption, userSelected]); const changeAdmin = useCallback(async () => { - const adminRole = await Roles.getInstance().getOne({ - where: { - name: "admin", - }, - }); + if (adminRoleType === "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, - }), - ); + if (!adminRole) return; + await Users.getInstance().put( + userSelected?.uid as string, + User.hydrate({ + uid: userSelected?.uid as string, + office_role: undefined, + role: adminRole, + }), + ); + } else { + // retirer rôle admin + } setRoleModalOpened(false); - }, [userSelected]); + }, [adminRoleType, userSelected]); - const openAdminModal = useCallback(() => { + const openAdminModal = useCallback((e: React.ChangeEvent) => { + if (e.target.checked) setAdminRoleType("add"); + else setAdminRoleType("remove"); setAdminModalOpened(true); }, []); @@ -189,7 +195,7 @@ export default function CollaboratorInformations(props: IProps) { cancelText={"Annuler"}>
- Attributer le rôle de {selectedOption?.label} à{" "} + Attribuer le rôle de {selectedOption?.label} à{" "} {userSelected?.contact?.first_name} {userSelected?.contact?.last_name} ?
@@ -204,7 +210,8 @@ export default function CollaboratorInformations(props: IProps) { cancelText={"Annuler"}>
- Attributer le rôle d'administrateur à {userSelected?.contact?.first_name} {userSelected?.contact?.last_name} ? + {adminRoleType === "add" ? "Attribuer" : "Retirer"} le rôle d'administrateur à{" "} + {userSelected?.contact?.first_name} {userSelected?.contact?.last_name} ?