♻️(app-desk) add hook useWhoAmI

The hook useWhoAmI is a custom hook that gives informations
about the current member.
This commit is contained in:
Anthony LC
2024-03-25 14:36:05 +01:00
committed by Anthony LC
parent 724bbe550c
commit 224025c3fb
3 changed files with 27 additions and 24 deletions

View File

@@ -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 (
<Modal
isOpen

View File

@@ -9,10 +9,10 @@ import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Box, Text, TextErrors } from '@/components';
import { useAuthStore } from '@/features/auth';
import { Role } from '@/features/teams';
import { useUpdateTeamAccess } from '../api/useUpdateTeamAccess';
import { useWhoAmI } from '../hooks/useWhoAmI';
import { Access } from '../types';
import { ChooseRole } from './ChooseRole';
@@ -32,7 +32,6 @@ export const ModalRole = ({
}: ModalRoleProps) => {
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;

View File

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