-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageParticipants.tsx
136 lines (124 loc) · 4.91 KB
/
manageParticipants.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Suspense, useContext, useEffect, useState } from 'react';
import { useNavigate, useRevalidator } from 'react-router-dom';
import { defer, makeLoader, useLoaderData } from 'react-router-typesafe';
import { ParticipantDTO } from '../../api/entities/Participant';
import { Loading } from '../components/Core/Loading/Loading';
import { ErrorToast, SuccessToast } from '../components/Core/Popups/Toast';
import { ScreenContentContainer } from '../components/Core/ScreenContentContainer/ScreenContentContainer';
import AddParticipantDialog from '../components/ParticipantManagement/AddParticipantDialog';
import ParticipantManagementTable from '../components/ParticipantManagement/ParticipantManagementTable';
import { CurrentUserContext } from '../contexts/CurrentUserProvider';
import { ParticipantContext } from '../contexts/ParticipantProvider';
import { GetAllEnabledApiRoles } from '../services/apiKeyService';
import {
AddParticipant,
AddParticipantForm,
GetAllParticipants,
GetUsersDefaultParticipant,
UpdateParticipant,
UpdateParticipantForm,
} from '../services/participant';
import { GetAllParticipantTypes } from '../services/participantType';
import { AwaitTypesafe, resolveAll } from '../utils/AwaitTypesafe';
import { RouteErrorBoundary } from '../utils/RouteErrorBoundary';
import { PortalRoute } from './routeUtils';
import './manageParticipants.scss';
const loader = makeLoader(() => {
return defer({
participants: GetAllParticipants(),
participantTypes: GetAllParticipantTypes(),
apiRoles: GetAllEnabledApiRoles(),
});
});
function ManageParticipants() {
const [showAddParticipantsDialog, setShowAddParticipantsDialog] = useState<boolean>(false);
const { participant, setParticipant } = useContext(ParticipantContext);
const { LoggedInUser } = useContext(CurrentUserContext);
const navigate = useNavigate();
useEffect(() => {
if (!LoggedInUser?.user?.isUid2Support && participant) {
navigate(`/participant/${participant.id}/home`);
ErrorToast(`You do not have access to this page. Rerouting back to Home.`);
}
});
const data = useLoaderData<typeof loader>();
const reloader = useRevalidator();
const onUpdateParticipant = async (
form: UpdateParticipantForm,
updatedParticipant: ParticipantDTO
) => {
await UpdateParticipant(form, updatedParticipant.id);
// if updating the current user's participant, update the ParticipantContext
if (updatedParticipant.id === participant?.id) {
const p = await GetUsersDefaultParticipant();
setParticipant(p);
}
SuccessToast('Participant updated.');
reloader.revalidate();
};
const onAddParticipant = async (form: AddParticipantForm) => {
const response = await AddParticipant(form);
reloader.revalidate();
return response;
};
const onOpenChangeAddParticipantDialog = () => {
setShowAddParticipantsDialog(!showAddParticipantsDialog);
};
return (
<div>
<div className='manage-participants-header'>
<div className='manage-participants-header-left'>
<h1>Manage Participants</h1>
<p className='heading-details'>View and manage UID2 Portal participants.</p>
</div>
<div className='manage-participants-header-right'>
<button type='button' onClick={onOpenChangeAddParticipantDialog}>
Add Participant
</button>
</div>
</div>
<ScreenContentContainer>
<Suspense fallback={<Loading />}>
<AwaitTypesafe
resolve={resolveAll({
participantTypes: data.participantTypes,
apiRoles: data.apiRoles,
})}
>
{(loadedData) => (
<>
{showAddParticipantsDialog && (
<AddParticipantDialog
apiRoles={loadedData.apiRoles}
participantTypes={loadedData.participantTypes}
onAddParticipant={onAddParticipant}
onOpenChange={onOpenChangeAddParticipantDialog}
/>
)}
<Suspense fallback={<Loading />}>
<AwaitTypesafe resolve={data.participants}>
{(participants) => (
<ParticipantManagementTable
participants={participants}
apiRoles={loadedData.apiRoles}
participantTypes={loadedData.participantTypes}
onUpdateParticipant={onUpdateParticipant}
/>
)}
</AwaitTypesafe>
</Suspense>
</>
)}
</AwaitTypesafe>
</Suspense>
</ScreenContentContainer>
</div>
);
}
export const ManageParticipantsRoute: PortalRoute = {
path: '/participant/:participantId/manageParticipants',
description: 'Manage Participants',
element: <ManageParticipants />,
errorElement: <RouteErrorBoundary />,
loader,
};