From 224025c3fb99e31bdfdd899d481e0ab10dfbcea0 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Mon, 25 Mar 2024 14:36:05 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(app-desk)=20add=20hook=20use?= =?UTF-8?q?WhoAmI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hook useWhoAmI is a custom hook that gives informations about the current member. --- .../members/components/ModalDelete.tsx | 14 +++--------- .../features/members/components/ModalRole.tsx | 15 ++----------- .../src/features/members/hooks/useWhoAmI.tsx | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 src/frontend/apps/desk/src/features/members/hooks/useWhoAmI.tsx diff --git a/src/frontend/apps/desk/src/features/members/components/ModalDelete.tsx b/src/frontend/apps/desk/src/features/members/components/ModalDelete.tsx index c058c14..bd8954a 100644 --- a/src/frontend/apps/desk/src/features/members/components/ModalDelete.tsx +++ b/src/frontend/apps/desk/src/features/members/components/ModalDelete.tsx @@ -11,11 +11,11 @@ import { useRouter } from 'next/navigation'; import IconUser from '@/assets/icons/icon-user.svg'; import { Box, Text, TextErrors } from '@/components'; import { useCunninghamTheme } from '@/cunningham'; -import { useAuthStore } from '@/features/auth'; import { Role, Team } from '@/features/teams/'; import { useDeleteTeamAccess } from '../api/useDeleteTeamAccess'; import IconRemoveMember from '../assets/icon-remove-member.svg'; +import { useWhoAmI } from '../hooks/useWhoAmI'; import { Access } from '../types'; interface ModalDeleteProps { @@ -26,12 +26,12 @@ interface ModalDeleteProps { } export const ModalDelete = ({ access, onClose, team }: ModalDeleteProps) => { - const { userData } = useAuthStore(); const { toast } = useToastProvider(); const { colorsTokens } = useCunninghamTheme(); const router = useRouter(); - const isMyself = userData?.id === access.user.id; + const { isMyself, isLastOwner, isOtherOwner } = useWhoAmI(access); + const isNotAllowed = isOtherOwner || isLastOwner; const { mutate: removeTeamAccess, @@ -53,14 +53,6 @@ export const ModalDelete = ({ access, onClose, team }: ModalDeleteProps) => { }, }); - const rolesAllowed = access.abilities.set_role_to; - const isLastOwner = - !rolesAllowed.length && access.role === Role.OWNER && isMyself; - - const isOtherOwner = access.role === Role.OWNER && userData?.id && !isMyself; - - const isNotAllowed = isOtherOwner || isLastOwner; - return ( { const { t } = useTranslation(); const [localRole, setLocalRole] = useState(access.role); - const { userData } = useAuthStore(); const { toast } = useToastProvider(); const { mutate: updateTeamAccess, @@ -46,17 +45,7 @@ export const ModalRole = ({ onClose(); }, }); - - const rolesAllowed = access.abilities.set_role_to; - const isLastOwner = - !rolesAllowed.length && - access.role === Role.OWNER && - userData?.id === access.user.id; - - const isOtherOwner = - access.role === Role.OWNER && - userData?.id && - userData.id !== access.user.id; + const { isLastOwner, isOtherOwner } = useWhoAmI(access); const isNotAllowed = isOtherOwner || isLastOwner; diff --git a/src/frontend/apps/desk/src/features/members/hooks/useWhoAmI.tsx b/src/frontend/apps/desk/src/features/members/hooks/useWhoAmI.tsx new file mode 100644 index 0000000..612b245 --- /dev/null +++ b/src/frontend/apps/desk/src/features/members/hooks/useWhoAmI.tsx @@ -0,0 +1,22 @@ +import { useAuthStore } from '@/features/auth'; +import { Role } from '@/features/teams'; + +import { Access } from '../types'; + +export const useWhoAmI = (access: Access) => { + const { userData } = useAuthStore(); + + const isMyself = userData?.id === access.user.id; + const rolesAllowed = access.abilities.set_role_to; + + const isLastOwner = + !rolesAllowed.length && access.role === Role.OWNER && isMyself; + + const isOtherOwner = access.role === Role.OWNER && userData?.id && !isMyself; + + return { + isLastOwner, + isOtherOwner, + isMyself, + }; +};