♻️(app-desk) add hook useWhoAmI
The hook useWhoAmI is a custom hook that gives informations about the current member.
This commit is contained in:
@@ -11,11 +11,11 @@ import { useRouter } from 'next/navigation';
|
|||||||
import IconUser from '@/assets/icons/icon-user.svg';
|
import IconUser from '@/assets/icons/icon-user.svg';
|
||||||
import { Box, Text, TextErrors } from '@/components';
|
import { Box, Text, TextErrors } from '@/components';
|
||||||
import { useCunninghamTheme } from '@/cunningham';
|
import { useCunninghamTheme } from '@/cunningham';
|
||||||
import { useAuthStore } from '@/features/auth';
|
|
||||||
import { Role, Team } from '@/features/teams/';
|
import { Role, Team } from '@/features/teams/';
|
||||||
|
|
||||||
import { useDeleteTeamAccess } from '../api/useDeleteTeamAccess';
|
import { useDeleteTeamAccess } from '../api/useDeleteTeamAccess';
|
||||||
import IconRemoveMember from '../assets/icon-remove-member.svg';
|
import IconRemoveMember from '../assets/icon-remove-member.svg';
|
||||||
|
import { useWhoAmI } from '../hooks/useWhoAmI';
|
||||||
import { Access } from '../types';
|
import { Access } from '../types';
|
||||||
|
|
||||||
interface ModalDeleteProps {
|
interface ModalDeleteProps {
|
||||||
@@ -26,12 +26,12 @@ interface ModalDeleteProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const ModalDelete = ({ access, onClose, team }: ModalDeleteProps) => {
|
export const ModalDelete = ({ access, onClose, team }: ModalDeleteProps) => {
|
||||||
const { userData } = useAuthStore();
|
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
const { colorsTokens } = useCunninghamTheme();
|
const { colorsTokens } = useCunninghamTheme();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const isMyself = userData?.id === access.user.id;
|
const { isMyself, isLastOwner, isOtherOwner } = useWhoAmI(access);
|
||||||
|
const isNotAllowed = isOtherOwner || isLastOwner;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
mutate: removeTeamAccess,
|
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 (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
isOpen
|
isOpen
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import { useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { Box, Text, TextErrors } from '@/components';
|
import { Box, Text, TextErrors } from '@/components';
|
||||||
import { useAuthStore } from '@/features/auth';
|
|
||||||
import { Role } from '@/features/teams';
|
import { Role } from '@/features/teams';
|
||||||
|
|
||||||
import { useUpdateTeamAccess } from '../api/useUpdateTeamAccess';
|
import { useUpdateTeamAccess } from '../api/useUpdateTeamAccess';
|
||||||
|
import { useWhoAmI } from '../hooks/useWhoAmI';
|
||||||
import { Access } from '../types';
|
import { Access } from '../types';
|
||||||
|
|
||||||
import { ChooseRole } from './ChooseRole';
|
import { ChooseRole } from './ChooseRole';
|
||||||
@@ -32,7 +32,6 @@ export const ModalRole = ({
|
|||||||
}: ModalRoleProps) => {
|
}: ModalRoleProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [localRole, setLocalRole] = useState(access.role);
|
const [localRole, setLocalRole] = useState(access.role);
|
||||||
const { userData } = useAuthStore();
|
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
const {
|
const {
|
||||||
mutate: updateTeamAccess,
|
mutate: updateTeamAccess,
|
||||||
@@ -46,17 +45,7 @@ export const ModalRole = ({
|
|||||||
onClose();
|
onClose();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const { isLastOwner, isOtherOwner } = useWhoAmI(access);
|
||||||
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 isNotAllowed = isOtherOwner || isLastOwner;
|
const isNotAllowed = isOtherOwner || isLastOwner;
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user