Skip to content

Commit 8ad5917

Browse files
[PRMP-860] Refactor upload components to link doc types
1 parent 37ddf23 commit 8ad5917

File tree

17 files changed

+542
-180
lines changed

17 files changed

+542
-180
lines changed

app/src/components/blocks/_documentUpload/documentSelectOrderStage/DocumentSelectOrderStage.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ const DocumentSelectOrderStage = ({
166166

167167
if (docToRemove.position) {
168168
updatedDocList = updatedDocList.map((doc) => {
169+
if (doc.docType !== docToRemove.docType) {
170+
return doc;
171+
}
172+
169173
if (doc.position && +doc.position > +docToRemove.position!) {
170174
doc.position = +doc.position - 1;
171175
}
@@ -185,7 +189,12 @@ const DocumentSelectOrderStage = ({
185189
position: fieldValues[documentPositionKey(doc.id)]!,
186190
}));
187191

188-
setDocuments(updatedDocuments);
192+
setDocuments((previousState) => {
193+
return previousState.map((doc) => {
194+
const updatedDoc = updatedDocuments.find((d) => d.id === doc.id);
195+
return updatedDoc ? updatedDoc : doc;
196+
});
197+
});
189198
};
190199

191200
const submitDocuments = (): void => {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.document-select-stage {
2+
.action-buttons {
3+
display: flex;
4+
align-items: center;
5+
6+
.continue-button {
7+
margin-bottom: 0;
8+
}
9+
}
10+
}

app/src/components/blocks/_documentUpload/documentSelectStage/DocumentSelectStage.tsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Button, Fieldset, Table, TextInput, WarningCallout } from 'nhsuk-react-components';
22
import { getDocument } from 'pdfjs-dist';
3-
import { JSX, RefObject, useEffect, useRef, useState } from 'react';
3+
import { Dispatch, JSX, RefObject, SetStateAction, useEffect, useRef, useState } from 'react';
44
import { v4 as uuidv4 } from 'uuid';
55
import useTitle from '../../../../helpers/hooks/useTitle';
66
import {
@@ -25,13 +25,16 @@ import { ErrorMessageListItem } from '../../../../types/pages/genericPageErrors'
2525
import { getJourney, useEnhancedNavigate } from '../../../../helpers/utils/urlManipulations';
2626
import { DOCUMENT_TYPE, DOCUMENT_TYPE_CONFIG } from '../../../../helpers/utils/documentType';
2727
import rejectedFileTypes from '../../../../config/rejectedFileTypes.json';
28+
import { Link } from 'react-router';
2829

2930
export type Props = {
3031
setDocuments: SetUploadDocuments;
3132
documents: Array<UploadDocument>;
3233
documentType: DOCUMENT_TYPE;
3334
filesErrorRef: RefObject<boolean>;
3435
documentConfig: DOCUMENT_TYPE_CONFIG;
36+
goToNextDocType?: () => void;
37+
showSkiplink?: boolean;
3538
};
3639

3740
type UploadFilesError = ErrorMessageListItem<UPLOAD_FILE_ERROR_TYPE>;
@@ -42,13 +45,16 @@ const DocumentSelectStage = ({
4245
documentType,
4346
filesErrorRef,
4447
documentConfig,
48+
goToNextDocType,
49+
showSkiplink,
4550
}: Props): JSX.Element => {
4651
const fileInputRef = useRef<HTMLInputElement | null>(null);
4752
const [noFilesSelected, setNoFilesSelected] = useState<boolean>(false);
4853
const scrollToRef = useRef<HTMLDivElement>(null);
4954
const fileInputAreaRef = useRef<HTMLFieldSetElement>(null);
5055
const [lastErrorsLength, setLastErrorsLength] = useState(0);
5156
const [tooManyFilesAdded, setTooManyFilesAdded] = useState<boolean>(false);
57+
const hasNextDocType = useRef<boolean>(showSkiplink);
5258
const journey = getJourney();
5359

5460
const navigate = useEnhancedNavigate();
@@ -196,34 +202,59 @@ const DocumentSelectStage = ({
196202

197203
setNoFilesSelected(sortedDocs.length === 0);
198204

199-
setDocuments(sortedDocs);
205+
setDocuments((previousState) => {
206+
const docs = previousState.filter((doc) => doc.docType !== documentType);
207+
return [...docs, ...sortedDocs];
208+
});
200209
};
201210

202211
const validateDocuments = (): boolean => {
203212
setNoFilesSelected(documents.length === 0);
204213

205214
documents?.forEach((doc) => (doc.validated = true));
206215

207-
setDocuments([...documents]);
216+
setDocuments((previousState) => {
217+
previousState.forEach((doc) => {
218+
if (doc.docType === documentType) {
219+
doc.validated = true;
220+
}
221+
})
222+
return [...previousState];
223+
});
208224

209225
return (
210226
documents.length > 0 &&
211227
documents.every((doc) => doc.state !== DOCUMENT_UPLOAD_STATE.FAILED)
212228
);
213229
};
214230

231+
const navigateToNextStep = (): void => {
232+
if (documentConfig.stitched) {
233+
navigate.withParams(routeChildren.DOCUMENT_UPLOAD_SELECT_ORDER);
234+
return;
235+
}
236+
237+
navigate.withParams(routeChildren.DOCUMENT_UPLOAD_CONFIRMATION);
238+
};
239+
215240
const continueClicked = (): void => {
216241
if (!validateDocuments()) {
217242
scrollToRef.current?.scrollIntoView({ behavior: 'smooth' });
218243
return;
219244
}
220245

221-
if (documentConfig.stitched) {
222-
navigate.withParams(routeChildren.DOCUMENT_UPLOAD_SELECT_ORDER);
223-
return;
224-
}
246+
skipClicked();
247+
};
225248

226-
navigate.withParams(routeChildren.DOCUMENT_UPLOAD_CONFIRMATION);
249+
const skipClicked = (): void => {
250+
if (hasNextDocType.current) {
251+
hasNextDocType.current = false;
252+
goToNextDocType!();
253+
window.scrollTo(0, 0);
254+
}
255+
else {
256+
navigateToNextStep();
257+
}
227258
};
228259

229260
const DocumentRow = (document: UploadDocument, index: number): JSX.Element => {
@@ -299,7 +330,7 @@ const DocumentSelectStage = ({
299330
};
300331

301332
return (
302-
<>
333+
<div className='document-select-stage'>
303334
<BackButton toLocation={routes.VERIFY_PATIENT} dataTestid="back-button" />
304335

305336
{(errorDocs().length > 0 || noFilesSelected || tooManyFilesAdded) && (
@@ -461,17 +492,24 @@ const DocumentSelectStage = ({
461492
)}
462493
</>
463494
)}
464-
<div className="lloydgeorge_upload-submission">
495+
<div className="action-buttons">
465496
<Button
466497
type="button"
467498
id="continue-button"
468499
data-testid="continue-button"
469500
onClick={continueClicked}
501+
className='continue-button mr-4'
470502
>
471503
Continue
472504
</Button>
505+
{showSkiplink && goToNextDocType && documents.length === 0 &&
506+
<LinkButton
507+
onClick={skipClicked}>
508+
{documentConfig.content.skipDocumentLinkText}
509+
</LinkButton>
510+
}
473511
</div>
474-
</>
512+
</div>
475513
);
476514
};
477515

0 commit comments

Comments
 (0)