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, + }; +};