11import { Button , Fieldset , Table , TextInput , WarningCallout } from 'nhsuk-react-components' ;
22import { getDocument } from 'pdfjs-dist' ;
3- import { JSX , RefObject , useEffect , useRef , useState } from 'react' ;
3+ import { Dispatch , JSX , RefObject , SetStateAction , useEffect , useRef , useState } from 'react' ;
44import { v4 as uuidv4 } from 'uuid' ;
55import useTitle from '../../../../helpers/hooks/useTitle' ;
66import {
@@ -25,13 +25,16 @@ import { ErrorMessageListItem } from '../../../../types/pages/genericPageErrors'
2525import { getJourney , useEnhancedNavigate } from '../../../../helpers/utils/urlManipulations' ;
2626import { DOCUMENT_TYPE , DOCUMENT_TYPE_CONFIG } from '../../../../helpers/utils/documentType' ;
2727import rejectedFileTypes from '../../../../config/rejectedFileTypes.json' ;
28+ import { Link } from 'react-router' ;
2829
2930export 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
3740type 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