diff --git a/packages/cards/src/pages/CardDetail/CardDetail.tsx b/packages/cards/src/pages/CardDetail/CardDetail.tsx index 97cdb72..0ab813e 100644 --- a/packages/cards/src/pages/CardDetail/CardDetail.tsx +++ b/packages/cards/src/pages/CardDetail/CardDetail.tsx @@ -4,7 +4,12 @@ import { useDispatch, useSelector } from 'react-redux'; import { getCardDetails } from '@modules/cards/store/features/cards/slice'; import { AppDispatch } from '@modules/cards/store/store'; import { userCardsDetailsSelector } from '@modules/cards/store/features/cards/selectors'; -import { USER_ROLE, onChangeUserRole } from 'shared'; +import { + USER_ROLE, + EnumRole, + onChangeUserRole, + stopListeningToUserRoleChange, +} from 'shared'; export const CardDetail = () => { const dispatch: AppDispatch = useDispatch(); @@ -40,6 +45,7 @@ export const CardDetail = () => { useEffect(() => { onChangeUserRole(setRole); + return stopListeningToUserRoleChange; }, []); if (!cardDetails) { @@ -48,7 +54,7 @@ export const CardDetail = () => { const getActions = () => { switch (role) { - case 'ADMIN': + case EnumRole.admin: return [ , ]; + case EnumRole.manager: + return [ + , + ]; default: return []; } @@ -66,7 +78,7 @@ export const CardDetail = () => { <>

PAN: {cardDetails.pan}

Expiry: {cardDetails.expiry}

@@ -85,10 +97,13 @@ export const CardDetail = () => { )} /> +

+ *For demonstration events from the host, change the user role. +

setIsModalVisible(false)} > diff --git a/packages/cards/src/pages/CardsList/CardsList.tsx b/packages/cards/src/pages/CardsList/CardsList.tsx index 993a296..973449b 100644 --- a/packages/cards/src/pages/CardsList/CardsList.tsx +++ b/packages/cards/src/pages/CardsList/CardsList.tsx @@ -18,13 +18,7 @@ const columns: ColumnsType = [ dataIndex: 'name', key: 'name', render: (value, record) => { - console.log(record, 'record'); - return ( - - ); + return ; }, }, { diff --git a/packages/shared/src/events/userRole.ts b/packages/shared/src/events/userRole.ts index a60ba12..0083fbb 100644 --- a/packages/shared/src/events/userRole.ts +++ b/packages/shared/src/events/userRole.ts @@ -1,16 +1,29 @@ import { Channels } from '@/events/const/channels'; import { EnumRole } from '@/types'; +let eventHandler: ((event: Event) => void) | null = null; + export const onChangeUserRole = (cb: (role: EnumRole) => void): void => { - window.addEventListener(Channels.changeUserRole, (event: Event) => { + eventHandler = (event: Event) => { const customEvent = event as CustomEvent<{ role: EnumRole }>; if (customEvent.detail) { + console.log(`On ${Channels.changeUserRole} - ${customEvent.detail.role}`); cb(customEvent.detail.role); } - }); + }; + + window.addEventListener(Channels.changeUserRole, eventHandler); +}; + +export const stopListeningToUserRoleChange = (): void => { + if (eventHandler) { + window.removeEventListener(Channels.changeUserRole, eventHandler); + eventHandler = null; + } }; export const emitChangeUserRole = (newRole: EnumRole): void => { + console.log(`Emit ${Channels.changeUserRole} - ${newRole}`); const event = new CustomEvent(Channels.changeUserRole, { detail: { role: newRole }, }); diff --git a/packages/shared/src/types/index.ts b/packages/shared/src/types/index.ts index 93d78f8..9c81a10 100644 --- a/packages/shared/src/types/index.ts +++ b/packages/shared/src/types/index.ts @@ -1,7 +1,7 @@ export enum EnumRole { admin = 'ADMIN', user = 'USER', - manager = 'MANGER', + manager = 'MANAGER', } export type TypeUser = { @@ -16,9 +16,7 @@ declare global { interface Window { host: { common: { - user: { - role: EnumRole; - }; + user: TypeUser; }; }; }