Skip to content

Commit f4ee049

Browse files
moving to load participants when dialog opens
1 parent 9e122b8 commit f4ee049

File tree

7 files changed

+59
-61
lines changed

7 files changed

+59
-61
lines changed

src/api/routers/managementRouter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express, { Response } from 'express';
22
import { z } from 'zod';
33

4+
import { GetUserParticipants } from '../../web/services/participant';
45
import { isSuperUserCheck } from '../middleware/userRoleMiddleware';
56
import { GetUserAuditTrail } from '../services/auditTrailService';
67
import { getAllUsersList, getUserById, updateUserLock } from '../services/managementService';
@@ -17,6 +18,12 @@ const handleGetUserAuditTrail = async (req: UserParticipantRequest, res: Respons
1718
return res.status(200).json(auditTrail ?? []);
1819
};
1920

21+
const handleGetUserParticipants = async (req: UserParticipantRequest, res: Response) => {
22+
const { userId } = z.object({ userId: z.coerce.number() }).parse(req.params);
23+
const participants = await GetUserParticipants(userId);
24+
return res.status(200).json(participants ?? []);
25+
};
26+
2027
const handleChangeUserLock = async (req: ParticipantRequest, res: Response) => {
2128
const { userId } = z.object({ userId: z.coerce.number() }).parse(req.params);
2229
const { isLocked } = z.object({ isLocked: z.boolean() }).parse(req.body);
@@ -34,6 +41,7 @@ export function createManagementRouter() {
3441

3542
managementRouter.get('/users', isSuperUserCheck, handleGetAllUsers);
3643
managementRouter.get('/:userId/auditTrail', isSuperUserCheck, handleGetUserAuditTrail);
44+
managementRouter.get('/:userId/participants', isSuperUserCheck, handleGetUserParticipants);
3745
managementRouter.patch('/:userId/changeLock', isSuperUserCheck, handleChangeUserLock);
3846

3947
return managementRouter;

src/api/services/participantsService.ts

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export const getAllParticipants = async (): Promise<Participant[]> => {
8181
.withGraphFetched('[apiRoles, approver, types, users]');
8282
};
8383

84+
export const getUserParticipants = async (userId: number): Promise<Participant[]> => {
85+
return Participant.query().withGraphFetched('userToParticipantRoles').where('userId', userId);
86+
};
87+
8488
export const getParticipantsBySiteIds = async (siteIds: number[]) => {
8589
return Participant.query().whereIn('siteId', siteIds).withGraphFetched('types');
8690
};

src/web/components/UserManagement/UserManagementItem.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as Switch from '@radix-ui/react-switch';
22
import { useState } from 'react';
33

4-
import { ParticipantDTO } from '../../../api/entities/Participant';
54
import { UserDTO } from '../../../api/entities/User';
65
import UserAuditTrailDialog from './UserAuditTrailDialog';
76
import UserParticipantsDialog from './UserParticipantsDialog';
@@ -10,15 +9,10 @@ import './UserManagementItem.scss';
109

1110
type UserManagementItemProps = Readonly<{
1211
user: UserDTO;
13-
userParticipants: ParticipantDTO[];
1412
onChangeUserLock: (userId: number, isLocked: boolean) => Promise<void>;
1513
}>;
1614

17-
export function UserManagementItem({
18-
user,
19-
userParticipants,
20-
onChangeUserLock,
21-
}: UserManagementItemProps) {
15+
export function UserManagementItem({ user, onChangeUserLock }: UserManagementItemProps) {
2216
const [showUserParticipantsDialog, setShowUserParticipantsDialog] = useState<boolean>(false);
2317
const [showUserAuditTrailDialog, setShowUserAuditTrailDialog] = useState<boolean>(false);
2418

@@ -46,11 +40,7 @@ export function UserManagementItem({
4640
View Participants
4741
</button>
4842
{showUserParticipantsDialog && (
49-
<UserParticipantsDialog
50-
user={user}
51-
userParticipants={userParticipants}
52-
onOpenChange={onUserParticipantsDialogChange}
53-
/>
43+
<UserParticipantsDialog user={user} onOpenChange={onUserParticipantsDialogChange} />
5444
)}
5545

5646
<button type='button' className='viewable-button' onClick={onUserAuditTrailDialogChange}>

src/web/components/UserManagement/UserManagementTable.tsx

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
22
import { useState } from 'react';
33

4-
import { ParticipantDTO } from '../../../api/entities/Participant';
54
import { UserDTO } from '../../../api/entities/User';
65
import { SortableProvider, useSortable } from '../../contexts/SortableTableProvider';
76
import { PagingTool } from '../Core/Paging/PagingTool';
@@ -14,7 +13,6 @@ import './UserManagementTable.scss';
1413

1514
type UserManagementTableProps = Readonly<{
1615
users: UserDTO[];
17-
allParticipants: ParticipantDTO[];
1816
onChangeUserLock: (userId: number, isLocked: boolean) => Promise<void>;
1917
}>;
2018

@@ -26,11 +24,7 @@ function NoUsers() {
2624
);
2725
}
2826

29-
function UserManagementTableContent({
30-
users,
31-
allParticipants,
32-
onChangeUserLock,
33-
}: UserManagementTableProps) {
27+
function UserManagementTableContent({ users, onChangeUserLock }: UserManagementTableProps) {
3428
const initialRowsPerPage = 25;
3529
const initialPageNumber = 1;
3630

@@ -79,12 +73,6 @@ function UserManagementTableContent({
7973

8074
const pagedRows = getPagedUsers(sortedUsers);
8175

82-
const getUserParticipants = (user: UserDTO) => {
83-
return allParticipants.filter((p) =>
84-
user.userToParticipantRoles?.find((role) => role.participantId === p.id)
85-
);
86-
};
87-
8876
return (
8977
<div className='users-table-container'>
9078
<div className='users-table-header'>
@@ -116,12 +104,7 @@ function UserManagementTableContent({
116104

117105
<tbody>
118106
{pagedRows.map((user) => (
119-
<UserManagementItem
120-
key={user.id}
121-
user={user}
122-
userParticipants={getUserParticipants(user)}
123-
onChangeUserLock={onChangeUserLock}
124-
/>
107+
<UserManagementItem key={user.id} user={user} onChangeUserLock={onChangeUserLock} />
125108
))}
126109
</tbody>
127110
</table>

src/web/components/UserManagement/UserParticipantsDialog.tsx

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
1+
import { useEffect, useState } from 'react';
2+
13
import { ParticipantDTO } from '../../../api/entities/Participant';
24
import { UserDTO } from '../../../api/entities/User';
35
import { UserRoleId } from '../../../api/entities/UserRole';
6+
import { GetUserParticipants } from '../../services/participant';
47
import { Dialog } from '../Core/Dialog/Dialog';
8+
import { Loading } from '../Core/Loading/Loading';
59
import UserParticipantsTable from './UserPartcipantsTable';
610

711
type UserParticipantsDialogProps = Readonly<{
812
user: UserDTO;
9-
userParticipants: ParticipantDTO[];
1013
onOpenChange: () => void;
1114
}>;
1215

13-
function UserParticipantsDialog({
14-
user,
15-
userParticipants,
16-
onOpenChange,
17-
}: UserParticipantsDialogProps) {
16+
function UserParticipantsDialog({ user, onOpenChange }: UserParticipantsDialogProps) {
17+
const [userParticipants, setUserParticipants] = useState<ParticipantDTO[]>();
18+
const [isLoading, setIsLoading] = useState<boolean>(true);
19+
20+
// useEffect(() => {
21+
// const getParticipants = async () => {
22+
// const participants = await GetUserParticipants(user.id);
23+
// setUserParticipants(participants);
24+
// setIsLoading(false);
25+
// };
26+
// getParticipants();
27+
// }, [user]);
28+
1829
return (
1930
<Dialog
2031
title={`Participants List for ${user.firstName} ${user.lastName}`}
2132
onOpenChange={onOpenChange}
2233
closeButtonText='Cancel'
2334
>
24-
{user.userToParticipantRoles?.find((role) => role.userRoleId === UserRoleId.UID2Support) ? (
25-
<div>This user has the UID2 support role and has admin access to all participants.</div>
35+
{isLoading ? (
36+
<Loading message='Loading audit trail...' />
2637
) : (
2738
<div>
28-
<UserParticipantsTable user={user} userParticipants={userParticipants} />
39+
{user.userToParticipantRoles?.find(
40+
(role) => role.userRoleId === UserRoleId.UID2Support
41+
) ? (
42+
<div>This user has the UID2 support role and has admin access to all participants.</div>
43+
) : (
44+
<div>
45+
<UserParticipantsTable user={user} userParticipants={userParticipants ?? []} />
46+
</div>
47+
)}
2948
</div>
3049
)}
3150
</Dialog>

src/web/screens/manageUsers.tsx

+4-21
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import { defer, useLoaderData } from 'react-router-typesafe';
55
import { Loading } from '../components/Core/Loading/Loading';
66
import { ScreenContentContainer } from '../components/Core/ScreenContentContainer/ScreenContentContainer';
77
import UserManagementTable from '../components/UserManagement/UserManagementTable';
8-
// import { GetAuditTrail } from '../services/auditTrailService';
9-
import { GetAllParticipants } from '../services/participant';
108
import { ChangeUserLock, GetAllUsers } from '../services/userAccount';
11-
import { AwaitTypesafe, resolveAll } from '../utils/AwaitTypesafe';
9+
import { AwaitTypesafe } from '../utils/AwaitTypesafe';
1210
import { RouteErrorBoundary } from '../utils/RouteErrorBoundary';
1311
import { PortalRoute } from './routeUtils';
1412

1513
const loader = () => {
1614
const userList = GetAllUsers();
17-
const participantsList = GetAllParticipants();
18-
// const auditTrail = GetAuditTrail();
19-
return defer({ userList, participantsList });
15+
return defer({ userList });
2016
};
2117

2218
function ManageUsers() {
@@ -36,21 +32,8 @@ function ManageUsers() {
3632
</p>
3733
<ScreenContentContainer>
3834
<Suspense fallback={<Loading message='Loading users...' />}>
39-
<AwaitTypesafe
40-
resolve={resolveAll({
41-
users: data.userList,
42-
participants: data.participantsList,
43-
// auditTrail: data.auditTrail,
44-
})}
45-
>
46-
{(loadedData) => (
47-
<UserManagementTable
48-
users={loadedData.users}
49-
allParticipants={loadedData.participants}
50-
// auditTrail={loadedData.auditTrail}
51-
onChangeUserLock={onChangeUserLock}
52-
/>
53-
)}
35+
<AwaitTypesafe resolve={data.userList}>
36+
{(users) => <UserManagementTable users={users} onChangeUserLock={onChangeUserLock} />}
5437
</AwaitTypesafe>
5538
</Suspense>
5639
</ScreenContentContainer>

src/web/services/participant.ts

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ export async function GetAllParticipants() {
4444
}
4545
}
4646

47+
export async function GetUserParticipants(userId: number) {
48+
try {
49+
const result = await axios.get<ParticipantDTO[]>(`/manage/${userId}/participants`, {
50+
validateStatus: (status) => status === 200,
51+
});
52+
return result.data;
53+
} catch (e: unknown) {
54+
throw backendError(e, 'Could not load user participants');
55+
}
56+
}
57+
4758
export async function GetSignedParticipants() {
4859
try {
4960
const result = await axios.get<SignedParticipantDTO[]>(`/participants/signed`, {

0 commit comments

Comments
 (0)