From 56a689c46d2fd66a5102827a860a6d799ba1f54f Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 26 Jul 2023 13:30:22 +0200 Subject: [PATCH] :sparkles: Switch and popups working on users page --- .../DesignSystem/Switch/classes.module.scss | 35 +++++ .../Components/DesignSystem/Switch/index.tsx | 33 +++++ .../CollaboratorInformations/index.tsx | 27 ++-- .../Layouts/Users/UserInformations/index.tsx | 134 ++++++++++-------- 4 files changed, 155 insertions(+), 74 deletions(-) create mode 100644 src/front/Components/DesignSystem/Switch/classes.module.scss create mode 100644 src/front/Components/DesignSystem/Switch/index.tsx diff --git a/src/front/Components/DesignSystem/Switch/classes.module.scss b/src/front/Components/DesignSystem/Switch/classes.module.scss new file mode 100644 index 00000000..9639f5cc --- /dev/null +++ b/src/front/Components/DesignSystem/Switch/classes.module.scss @@ -0,0 +1,35 @@ +.root { + display: flex; + gap: 16px; + .switch-container { + position: relative; + width: 46px; + height: 24px; + background-color: var(--grey-medium); + border-radius: 64px; + + transition: all 200ms ease-in-out; + &[data-checked="true"] { + background-color: var(--turquoise-flash); + + .round { + left: 24px; + } + } + + .checkbox { + display: none; + } + + .round { + transition: all 200ms ease-in-out; + width: 20px; + height: 20px; + border-radius: 100px; + position: absolute; + top: 2px; + left: 2px; + background: white; + } + } +} diff --git a/src/front/Components/DesignSystem/Switch/index.tsx b/src/front/Components/DesignSystem/Switch/index.tsx new file mode 100644 index 00000000..3b911bb7 --- /dev/null +++ b/src/front/Components/DesignSystem/Switch/index.tsx @@ -0,0 +1,33 @@ +import { useCallback, useEffect, useState } from "react"; + +import classes from "./classes.module.scss"; +import Typography, { ITypo, ITypoColor } from "../Typography"; + +type IProps = { + onChange?: (checked: boolean) => void; + checked?: boolean; + label: string; +}; +export default function Switch({ onChange, checked, label }: IProps) { + const [isChecked, setIsChecked] = useState(checked ? checked : false); + + useEffect(() => { + setIsChecked(checked ? checked : false); + }, [checked]); + + const handleChange = useCallback(() => { + setIsChecked(!isChecked); + onChange?.(!isChecked); + }, [isChecked, onChange]); + + return ( +
+
+
+
+ + {label} + +
+ ); +} diff --git a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx index 617838d6..f5730d3c 100644 --- a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx +++ b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx @@ -3,9 +3,9 @@ import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; import Roles from "@Front/Api/LeCoffreApi/Admin/Roles/Roles"; import Users from "@Front/Api/LeCoffreApi/Admin/Users/Users"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; -import CheckBox from "@Front/Components/DesignSystem/CheckBox"; import SelectField, { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; +import Switch from "@Front/Components/DesignSystem/Switch"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultCollaboratorDashboard from "@Front/Components/LayoutTemplates/DefaultCollaboratorDashboard"; import Module from "@Front/Config/Module"; @@ -29,6 +29,13 @@ export default function CollaboratorInformations(props: IProps) { const [adminRoleType, setAdminRoleType] = useState<"add" | "remove">("add"); const [selectedOption, setSelectedOption] = useState(null); + const [isAdminChecked, setIsAdminChecked] = useState(false); + + useEffect(() => { + if (!userSelected) return; + setIsAdminChecked(userSelected.role?.name === "admin" && !userSelected.office_role); + }, [userSelected]); + const handleRoleChange = useCallback((option: IOption) => { setSelectedOption(option); setRoleModalOpened(true); @@ -82,15 +89,17 @@ export default function CollaboratorInformations(props: IProps) { } }, [adminRoleType, userSelected]); - const openAdminModal = useCallback((e: React.ChangeEvent) => { - if (e.target.checked) setAdminRoleType("add"); + const openAdminModal = useCallback((checked: boolean) => { + setIsAdminChecked(checked); + if (checked) setAdminRoleType("add"); else setAdminRoleType("remove"); setAdminModalOpened(true); }, []); const closeAdminModal = useCallback(() => { + setIsAdminChecked(userSelected?.role?.name === "admin" && !userSelected.office_role); setAdminModalOpened(false); - }, []); + }, [userSelected]); useEffect(() => { async function getUser() { @@ -177,15 +186,7 @@ export default function CollaboratorInformations(props: IProps) {
{userSelected?.role?.name !== "super-admin" && (
- +
)} diff --git a/src/front/Components/Layouts/Users/UserInformations/index.tsx b/src/front/Components/Layouts/Users/UserInformations/index.tsx index 22ebaf28..0348d001 100644 --- a/src/front/Components/Layouts/Users/UserInformations/index.tsx +++ b/src/front/Components/Layouts/Users/UserInformations/index.tsx @@ -1,15 +1,15 @@ import WarningIcon from "@Assets/images/warning.png"; import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users"; -import CheckBox from "@Front/Components/DesignSystem/CheckBox"; import SelectField, { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; +import Switch from "@Front/Components/DesignSystem/Switch"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultUserDashboard from "@Front/Components/LayoutTemplates/DefaultUserDashboard"; import User from "le-coffre-resources/dist/Notary"; import Image from "next/image"; import { useRouter } from "next/router"; -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import classes from "./classes.module.scss"; @@ -21,42 +21,74 @@ export default function UserInformations(props: IProps) { const [userSelected, setUserSelected] = useState(null); const [availableRoles, setAvailableRoles] = useState([]); - const [addSuperAdminModalOpened, setAddSuperAdminModalOpened] = useState(false); - const [removeSuperAdminModalOpened, setRemoveSuperAdminModalOpened] = useState(false); + const [isSuperAdminModalOpened, setIsSuperAdminModalOpened] = useState(false); + const [superAdminModalType, setSuperAdminModalType] = useState<"add" | "remove">("add"); + const [adminModalType, setAdminModalType] = useState<"add" | "remove">("add"); - const openAddSuperAdminModal = () => { - setAddSuperAdminModalOpened(true); + const [isSuperAdminChecked, setIsSuperAdminChecked] = useState(false); + const [isAdminChecked, setIsAdminChecked] = useState(false); + const [isAdminModalOpened, setIsAdminModalOpened] = useState(false); + + /** Functions for the admin modal */ + const openAdminModal = () => { + setIsAdminModalOpened(true); }; - const closeAddSuperAdminModal = () => { - setAddSuperAdminModalOpened(false); + const closeAdminModal = useCallback(() => { + setIsAdminModalOpened(false); + setIsAdminChecked(userSelected?.role?.name === "admin" && !userSelected.office_role); + }, [userSelected]); + + const handleAdminChanged = (checked: boolean) => { + setIsAdminChecked(checked); + setAdminModalType(checked ? "add" : "remove"); + openAdminModal(); }; - const openRemoveSuperAdminModal = () => { - setRemoveSuperAdminModalOpened(true); - }; - - const closeRemoveSuperAdminModal = () => { - setRemoveSuperAdminModalOpened(false); - }; - - const handleCheckboxAdminChanged = (e: React.ChangeEvent) => { - const checked = e.target.checked; - if (checked) { - openAddSuperAdminModal(); + const handleAdminModalAccepted = useCallback(async () => { + if (!userSelected) return; + if (adminModalType === "add") { + // add super admin } else { - openRemoveSuperAdminModal(); + // remove super admin } + setIsAdminModalOpened(false); + }, [userSelected, adminModalType]); + + /** Functions for the super admin modal */ + const openSuperAdminModal = () => { + setIsSuperAdminModalOpened(true); }; - const addSuperAdmin = async () => { - closeAddSuperAdminModal(); + const closeSuperAdminModal = useCallback(() => { + setIsSuperAdminModalOpened(false); + setIsSuperAdminChecked(userSelected?.role?.name === "super-admin" && !userSelected.office_role); + }, [userSelected]); + + const handleSuperAdminChanged = (checked: boolean) => { + setIsSuperAdminChecked(checked); + setSuperAdminModalType(checked ? "add" : "remove"); + openSuperAdminModal(); }; - const removeSuperAdmin = async () => { - closeRemoveSuperAdminModal(); - }; + const handleSuperAdminModalAccepted = useCallback(async () => { + if (!userSelected) return; + if (superAdminModalType === "add") { + // add super admin + } else { + // remove super admin + } + setIsSuperAdminModalOpened(false); + }, [userSelected, superAdminModalType]); + /** Reset switch state when userSelect change */ + useEffect(() => { + if (!userSelected) return; + 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; @@ -137,23 +169,8 @@ export default function UserInformations(props: IProps) { Attribuer un titre
- - + +
warning @@ -174,38 +191,33 @@ export default function UserInformations(props: IProps) {
- Nommer une personne Super Administrateur nécessite 3 votes de super administrateurs existants. Souhaitez-vous - attribuer un vote ? + {superAdminModalType === "add" ? "Nommer" : "Retirer"} une personne Super Administrateur nécessite 3 votes de + super administrateurs existants. Souhaitez-vous attribuer un vote ?
-
- - Retirer un collaborateur du rôle de Super Administrateur nécessite 3 votes de super administrateurs existants. - Souhaitez-vous attribuer un vote ? - -
+