Skip to content

Commit f36ec76

Browse files
[PRMP-1493] Implement restriction post and complete page
1 parent a1709af commit f36ec76

File tree

6 files changed

+193
-92
lines changed

6 files changed

+193
-92
lines changed

app/src/components/blocks/_userPatientRestrictions/userPatientRestrictionsAddConfirmStage/UserPatientRestrictionsAddConfirmStage.tsx

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,63 @@
1-
import { Link } from 'react-router-dom';
1+
import { Link, useNavigate } from 'react-router-dom';
22
import useTitle from '../../../../helpers/hooks/useTitle';
33
import { UserInformation } from '../../../../types/generic/userPatientRestriction';
44
import BackButton from '../../../generic/backButton/BackButton';
55
import PatientSummary from '../../../generic/patientSummary/PatientSummary';
66
import StaffMemberDetails from '../../../generic/staffMemberDetails/StaffMemberDetails';
7-
import { routeChildren } from '../../../../types/generic/routes';
7+
import { routeChildren, routes } from '../../../../types/generic/routes';
88
import { Button } from 'nhsuk-react-components';
9+
import postUserPatientRestriction from '../../../../helpers/requests/userPatientRestrictions/createUserPatientRestriction';
10+
import usePatient from '../../../../helpers/hooks/usePatient';
11+
import useBaseAPIUrl from '../../../../helpers/hooks/useBaseAPIUrl';
12+
import useBaseAPIHeaders from '../../../../helpers/hooks/useBaseAPIHeaders';
13+
import { AxiosError } from 'axios';
14+
import { isMock } from '../../../../helpers/utils/isLocal';
15+
import { errorToParams } from '../../../../helpers/utils/errorToParams';
16+
import { useState } from 'react';
17+
import SpinnerButton from '../../../generic/spinnerButton/SpinnerButton';
918

1019
type Props = {
1120
userInformation: UserInformation;
1221
};
1322

1423
const UserPatientRestrictionsAddConfirmStage = ({ userInformation }: Props): React.JSX.Element => {
24+
const navigate = useNavigate();
25+
const patient = usePatient();
26+
const baseAPIUrl = useBaseAPIUrl();
27+
const baseAPIHeaders = useBaseAPIHeaders();
28+
1529
const pageTitle = 'Check the details of the restriction';
1630
useTitle({ pageTitle });
1731

18-
const addRestriction = (): void => {};
32+
const [creatingRestriction, setCreatingRestriction] = useState(false);
33+
34+
const addRestriction = async (): Promise<void> => {
35+
setCreatingRestriction(true);
36+
37+
try {
38+
await postUserPatientRestriction({
39+
nhsNumber: patient!.nhsNumber,
40+
smartcardId: userInformation.smartcardId,
41+
baseAPIUrl,
42+
baseAPIHeaders,
43+
});
44+
45+
handleSuccess();
46+
} catch (e) {
47+
const error = e as AxiosError;
48+
if (isMock(error)) {
49+
handleSuccess();
50+
} else if (error.response?.status === 403) {
51+
navigate(routes.SESSION_EXPIRED);
52+
} else {
53+
navigate(routes.SERVER_ERROR + errorToParams(error));
54+
}
55+
}
56+
};
57+
58+
const handleSuccess = (): void => {
59+
navigate(routeChildren.USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE);
60+
};
1961

2062
return (
2163
<>
@@ -34,14 +76,18 @@ const UserPatientRestrictionsAddConfirmStage = ({ userInformation }: Props): Rea
3476

3577
<StaffMemberDetails userInformation={userInformation} />
3678

37-
<div className="action-button-group">
38-
<Button data-testid="continue-button" onClick={addRestriction}>
39-
Continue to add this restriction
40-
</Button>
41-
<Link className="ml-4" to={routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CANCEL}>
42-
Cancel without adding a restriction
43-
</Link>
44-
</div>
79+
{creatingRestriction ? (
80+
<SpinnerButton id="creating-restriction-spinner" status="Processing..." />
81+
) : (
82+
<div className="action-button-group">
83+
<Button data-testid="continue-button" onClick={addRestriction}>
84+
Continue to add this restriction
85+
</Button>
86+
<Link className="ml-4" to={routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CANCEL}>
87+
Cancel without adding a restriction
88+
</Link>
89+
</div>
90+
)}
4591
</>
4692
);
4793
};

app/src/components/blocks/_userPatientRestrictions/userPatientRestrictionsRemoveConfirmStage/UserPatientRestrictionsRemoveConfirmStage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const UserPatientRestrictionsRemoveConfirmStage = ({ restriction }: Props): Reac
5151
}
5252
}
5353

54-
navigate(routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_COMPLETE);
54+
navigate(routeChildren.USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE);
5555
};
5656

5757
return (
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import axios, { AxiosError } from 'axios';
2+
import { AuthHeaders } from '../../../types/blocks/authHeaders';
3+
import { endpoints } from '../../../types/generic/endpoints';
4+
5+
type CreateUserPatientRestrictionArgs = {
6+
smartcardId: string;
7+
nhsNumber: string;
8+
baseAPIUrl: string;
9+
baseAPIHeaders: AuthHeaders;
10+
};
11+
12+
const postUserPatientRestriction = async ({
13+
smartcardId,
14+
nhsNumber,
15+
baseAPIUrl,
16+
baseAPIHeaders,
17+
}: CreateUserPatientRestrictionArgs): Promise<void> => {
18+
try {
19+
const url = baseAPIUrl + endpoints.USER_PATIENT_RESTRICTIONS;
20+
await axios.post(url, {
21+
headers: {
22+
...baseAPIHeaders,
23+
},
24+
data: {
25+
smartcardId,
26+
nhsNumber,
27+
},
28+
});
29+
} catch (e) {
30+
const error = e as AxiosError;
31+
throw error;
32+
}
33+
};
34+
35+
export default postUserPatientRestriction;

app/src/pages/userPatientRestrictionsPage/UserPatientRestrictionsPage.tsx

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import UserPatientRestrictionsSearchStaffStage from '../../components/blocks/_us
1515
import UserPatientRestrictionsVerifyStaffStage from '../../components/blocks/_userPatientRestrictions/userPatientRestrictionsVerifyStaffStage/UserPatientRestrictionsVerifyStaffStage';
1616
import UserPatientRestrictionsAddConfirmStage from '../../components/blocks/_userPatientRestrictions/userPatientRestrictionsAddConfirmStage/UserPatientRestrictionsAddConfirmStage';
1717
import UserPatientRestrictionsAddCancelStage from '../../components/blocks/_userPatientRestrictions/userPatientRestrictionsAddCancelStage/UserPatientRestrictionsAddCancelStage';
18+
import PatientGuard from '../../router/guards/patientGuard/PatientGuard';
1819

1920
const UserPatientRestrictionsPage = (): React.JSX.Element => {
2021
const {
@@ -45,96 +46,115 @@ const UserPatientRestrictionsPage = (): React.JSX.Element => {
4546
return (
4647
<>
4748
<Routes>
49+
{/* routes that don't need patient context */}
50+
<Route path="*" element={<div>Page not found</div>} />
4851
<Route index element={<UserPatientRestrictionsIndex setSubRoute={setSubRoute} />} />
4952

5053
<Route
5154
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_LIST)}
5255
element={<UserPatientRestrictionsListStage setSubRoute={setSubRoute} />}
5356
/>
5457

55-
<Route
56-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_PATIENT)}
57-
element={
58-
<UserPatientRestrictionsVerifyPatientStage
59-
confirmClicked={confirmVerifyPatientDetails}
60-
route={subRoute!}
61-
/>
62-
}
63-
/>
64-
65-
<Route
66-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_VIEW)}
67-
element={
68-
<UserPatientRestrictionsViewStage
69-
setSubRoute={setSubRoute}
70-
onRemoveRestriction={onRemoveRestriction}
71-
/>
72-
}
73-
/>
74-
75-
<Route
76-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_CONFIRM)}
77-
element={
78-
<UserPatientRestrictionsRemoveConfirmStage
79-
restriction={restrictionToRemove!}
80-
/>
81-
}
82-
/>
83-
84-
<Route
85-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_COMPLETE)}
86-
element={<UserPatientRestrictionsCompleteStage route={subRoute!} />}
87-
/>
88-
8958
<Route
9059
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_SEARCH_PATIENT)}
9160
element={<UserPatientRestrictionsSearchPatientStage />}
9261
/>
62+
{/* routes that don't need patient context */}
9363

64+
{/* routes that need patient context */}
9465
<Route
95-
path={getLastURLPath(
96-
routeChildren.USER_PATIENT_RESTRICTIONS_EXISTING_RESTRICTIONS,
97-
)}
9866
element={
99-
<UserPatientRestrictionsExistingStage
100-
existingRestrictions={existingRestrictions}
101-
setExistingRestrictions={setExistingRestrictions}
102-
/>
67+
<PatientGuard navigationPath={routes.USER_PATIENT_RESTRICTIONS}>
68+
<Outlet />
69+
</PatientGuard>
10370
}
104-
/>
105-
106-
<Route
107-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_SEARCH_STAFF)}
108-
element={
109-
<UserPatientRestrictionsSearchStaffStage
110-
existingRestrictions={existingRestrictions}
111-
setUserInformation={setUserInformation}
112-
/>
113-
}
114-
/>
115-
116-
<Route
117-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_STAFF)}
118-
element={
119-
<UserPatientRestrictionsVerifyStaffStage
120-
userInformation={userInformation!}
121-
/>
122-
}
123-
/>
124-
125-
<Route
126-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CONFIRM)}
127-
element={
128-
<UserPatientRestrictionsAddConfirmStage
129-
userInformation={userInformation!}
130-
/>
131-
}
132-
/>
133-
134-
<Route
135-
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CANCEL)}
136-
element={<UserPatientRestrictionsAddCancelStage />}
137-
/>
71+
>
72+
<Route
73+
path={getLastURLPath(
74+
routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_PATIENT,
75+
)}
76+
element={
77+
<UserPatientRestrictionsVerifyPatientStage
78+
confirmClicked={confirmVerifyPatientDetails}
79+
route={subRoute!}
80+
/>
81+
}
82+
/>
83+
84+
<Route
85+
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_VIEW)}
86+
element={
87+
<UserPatientRestrictionsViewStage
88+
setSubRoute={setSubRoute}
89+
onRemoveRestriction={onRemoveRestriction}
90+
/>
91+
}
92+
/>
93+
94+
<Route
95+
path={getLastURLPath(
96+
routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_CONFIRM,
97+
)}
98+
element={
99+
<UserPatientRestrictionsRemoveConfirmStage
100+
restriction={restrictionToRemove!}
101+
/>
102+
}
103+
/>
104+
105+
<Route
106+
path={getLastURLPath(
107+
routeChildren.USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE,
108+
)}
109+
element={<UserPatientRestrictionsCompleteStage route={subRoute!} />}
110+
/>
111+
112+
<Route
113+
path={getLastURLPath(
114+
routeChildren.USER_PATIENT_RESTRICTIONS_EXISTING_RESTRICTIONS,
115+
)}
116+
element={
117+
<UserPatientRestrictionsExistingStage
118+
existingRestrictions={existingRestrictions}
119+
setExistingRestrictions={setExistingRestrictions}
120+
/>
121+
}
122+
/>
123+
124+
<Route
125+
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_SEARCH_STAFF)}
126+
element={
127+
<UserPatientRestrictionsSearchStaffStage
128+
existingRestrictions={existingRestrictions}
129+
setUserInformation={setUserInformation}
130+
/>
131+
}
132+
/>
133+
134+
<Route
135+
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_STAFF)}
136+
element={
137+
<UserPatientRestrictionsVerifyStaffStage
138+
userInformation={userInformation!}
139+
/>
140+
}
141+
/>
142+
143+
<Route
144+
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CONFIRM)}
145+
element={
146+
<UserPatientRestrictionsAddConfirmStage
147+
userInformation={userInformation!}
148+
/>
149+
}
150+
/>
151+
152+
<Route
153+
path={getLastURLPath(routeChildren.USER_PATIENT_RESTRICTIONS_ADD_CANCEL)}
154+
element={<UserPatientRestrictionsAddCancelStage />}
155+
/>
156+
</Route>
157+
{/* routes that need patient context */}
138158
</Routes>
139159

140160
<Outlet />

app/src/router/AppRouter.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,23 +265,23 @@ export const childRoutes = [
265265
parent: DOCUMENT_REASSIGN_PAGES,
266266
},
267267
{
268-
route: routeChildren.USER_PATIENT_RESTRICTIONS_VIEW,
268+
route: routeChildren.USER_PATIENT_RESTRICTIONS_LIST,
269269
parent: USER_PATIENT_RESTRICTIONS,
270270
},
271271
{
272-
route: routeChildren.USER_PATIENT_RESTRICTIONS_LIST,
272+
route: routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_PATIENT,
273273
parent: USER_PATIENT_RESTRICTIONS,
274274
},
275275
{
276-
route: routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_PATIENT,
276+
route: routeChildren.USER_PATIENT_RESTRICTIONS_VIEW,
277277
parent: USER_PATIENT_RESTRICTIONS,
278278
},
279279
{
280280
route: routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_CONFIRM,
281281
parent: USER_PATIENT_RESTRICTIONS,
282282
},
283283
{
284-
route: routeChildren.USER_PATIENT_RESTRICTIONS_REMOVE_COMPLETE,
284+
route: routeChildren.USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE,
285285
parent: USER_PATIENT_RESTRICTIONS,
286286
},
287287
{

app/src/types/generic/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export enum routeChildren {
114114
USER_PATIENT_RESTRICTIONS_VERIFY_STAFF = '/user-patient-restrictions/verify-staff',
115115
USER_PATIENT_RESTRICTIONS_RESTRICTED = '/user-patient-restrictions/restricted',
116116
USER_PATIENT_RESTRICTIONS_REMOVE_CONFIRM = '/user-patient-restrictions/remove-confirm',
117-
USER_PATIENT_RESTRICTIONS_REMOVE_COMPLETE = '/user-patient-restrictions/remove-complete',
118117
USER_PATIENT_RESTRICTIONS_ADD_CANCEL = '/user-patient-restrictions/add-cancel',
118+
USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE = '/user-patient-restrictions/action-complete',
119119
}
120120

121121
export const navigateUrlParam = (

0 commit comments

Comments
 (0)