Skip to content

Commit

Permalink
add shared modules and event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
u4aew committed Dec 28, 2023
1 parent 73674c6 commit 7c97e79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
23 changes: 19 additions & 4 deletions packages/cards/src/pages/CardDetail/CardDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -40,6 +45,7 @@ export const CardDetail = () => {

useEffect(() => {
onChangeUserRole(setRole);
return stopListeningToUserRoleChange;
}, []);

if (!cardDetails) {
Expand All @@ -48,7 +54,7 @@ export const CardDetail = () => {

const getActions = () => {
switch (role) {
case 'ADMIN':
case EnumRole.admin:
return [
<Button key="edit" type="primary" onClick={showEditModal}>
Edit
Expand All @@ -57,6 +63,12 @@ export const CardDetail = () => {
Delete
</Button>,
];
case EnumRole.manager:
return [
<Button key="edit" type="primary" onClick={showEditModal}>
Edit
</Button>,
];
default:
return [];
}
Expand All @@ -66,7 +78,7 @@ export const CardDetail = () => {
<>
<Card
actions={getActions()}
title={`Card Details - ${cardDetails.cardHolderName}`}
title={`Card Details - ${cardDetails.cardHolderName} `}
>
<p>PAN: {cardDetails.pan}</p>
<p>Expiry: {cardDetails.expiry}</p>
Expand All @@ -85,10 +97,13 @@ export const CardDetail = () => {
</List.Item>
)}
/>
<p>
<b>*For demonstration events from the host, change the user role.</b>
</p>
</Card>
<Modal
title="Edit transactions"
visible={isModalVisible}
open={isModalVisible}
onOk={handleEdit}
onCancel={() => setIsModalVisible(false)}
>
Expand Down
8 changes: 1 addition & 7 deletions packages/cards/src/pages/CardsList/CardsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ const columns: ColumnsType<DataType> = [
dataIndex: 'name',
key: 'name',
render: (value, record) => {
console.log(record, 'record');
return (
<PreviewCard
pan={record.pan}
name={record.name}
/>
);
return <PreviewCard pan={record.pan} name={record.name} />;
},
},
{
Expand Down
17 changes: 15 additions & 2 deletions packages/shared/src/events/userRole.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
Expand Down
6 changes: 2 additions & 4 deletions packages/shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export enum EnumRole {
admin = 'ADMIN',
user = 'USER',
manager = 'MANGER',
manager = 'MANAGER',
}

export type TypeUser = {
Expand All @@ -16,9 +16,7 @@ declare global {
interface Window {
host: {
common: {
user: {
role: EnumRole;
};
user: TypeUser;
};
};
}
Expand Down

0 comments on commit 7c97e79

Please sign in to comment.