From c83438ec36f263b8d5cd45c0e43a206a70a9b538 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 30 Dec 2024 16:34:30 +0100 Subject: [PATCH] role --- backend/src/controllers/user.ts | 10 +++++++--- frontend/src/actions/userActions.ts | 16 ++++++++++++++++ .../src/components/socketio/SocketService.ts | 3 ++- frontend/src/components/users/EditRole.tsx | 3 +++ frontend/src/trans.nl.ts | 3 ++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/backend/src/controllers/user.ts b/backend/src/controllers/user.ts index 78d7c250..e8f3a1cd 100644 --- a/backend/src/controllers/user.ts +++ b/backend/src/controllers/user.ts @@ -146,11 +146,15 @@ export const saveRole = async (req: ConfacRequest, res: Response) => { role.audit = updateAudit(role.audit, req.user); const {value: originalRole} = await collection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: role}, {returnOriginal: true}); await saveAudit(req, 'role', originalRole, role); - return res.send({_id, ...role}); + + const responseRole = {_id, ...role}; + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.ROLES, _id, responseRole); + return res.send(responseRole); } role.audit = createAudit(req.user); const inserted = await collection.insertOne(role); - const [createdClient] = inserted.ops; - return res.send(createdClient); + const [createdRole] = inserted.ops; + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.ROLES, createdRole._id, createdRole); + return res.send(createdRole); }; diff --git a/frontend/src/actions/userActions.ts b/frontend/src/actions/userActions.ts index 8c2f2591..09635155 100644 --- a/frontend/src/actions/userActions.ts +++ b/frontend/src/actions/userActions.ts @@ -82,3 +82,19 @@ export function saveRole(role: RoleModel, callback?: (savedRole: RoleModel) => v .then(() => dispatch(busyToggle.off())); }; } + +export function handleRoleSocketEvents(eventType: string, eventPayload: EntityEventPayload){ + return (dispatch: Dispatch) => { + dispatch(busyToggle()); + switch(eventType){ + case SocketEventTypes.EntityUpdated: + case SocketEventTypes.EntityCreated: + dispatch({ + type: ACTION_TYPES.ROLE_UPDATE, + role: eventPayload.entity, + }); break; + default: throw new Error(`${eventType} not supported for role.`); + } + dispatch(busyToggle.off()); +} +} diff --git a/frontend/src/components/socketio/SocketService.ts b/frontend/src/components/socketio/SocketService.ts index 5ffc2d79..1c2b163a 100644 --- a/frontend/src/components/socketio/SocketService.ts +++ b/frontend/src/components/socketio/SocketService.ts @@ -6,7 +6,7 @@ import { SocketEventTypes } from "./SocketEventTypes"; import { EntityEventPayload } from "./EntityEventPayload"; import { t } from "../utils"; import { toast } from "react-toastify"; -import { handleUserSocketEvents } from "../../actions/userActions"; +import { handleRoleSocketEvents, handleUserSocketEvents } from "../../actions/userActions"; function createSocketService () { // TODO nicolas read server url from frontend config !!! @@ -43,6 +43,7 @@ function createSocketService () { case 'consultants': dispatch(handleConsultantSocketEvents(eventType, eventPayload)); break; case 'clients': dispatch(handleClientSocketEvents(eventType, eventPayload)); break; case 'users': dispatch(handleUserSocketEvents(eventType, eventPayload)); break; + case 'roles': dispatch(handleRoleSocketEvents(eventType, eventPayload)); break; default: throw new Error(`${eventPayload.entityType} event for entity type not supported.`); }; }); diff --git a/frontend/src/components/users/EditRole.tsx b/frontend/src/components/users/EditRole.tsx index 3c1686c9..66ac1b9c 100644 --- a/frontend/src/components/users/EditRole.tsx +++ b/frontend/src/components/users/EditRole.tsx @@ -14,6 +14,7 @@ import {useDocumentTitle} from '../hooks/useDocumentTitle'; import {saveRole} from '../../actions/userActions'; import {Audit} from '../admin/audit/Audit'; import {useParams} from 'react-router-dom'; +import useEntityChangedToast from '../hooks/useEntityChangedToast'; export const EditRole = () => { @@ -23,6 +24,8 @@ export const EditRole = () => { const model = useSelector((state: ConfacState) => state.user.roles.find(c => c.name === params.id)); const [role, setRole] = useState(model || getNewRole()); + useEntityChangedToast(role._id); + const docTitle = role._id ? 'roleEdit' : 'roleNew'; useDocumentTitle(docTitle, {name: role.name}); diff --git a/frontend/src/trans.nl.ts b/frontend/src/trans.nl.ts index 091ec01e..495e092b 100644 --- a/frontend/src/trans.nl.ts +++ b/frontend/src/trans.nl.ts @@ -682,7 +682,8 @@ export const trans = { projects: 'Project', consultants: 'Consultant', clients: 'Klant', - users: 'Gebruiker' + users: 'Gebruiker', + roles: 'Rol' }, operation: { entityUpdated: '{entityType} werd aangepast door {user}',