@@ -6,7 +6,7 @@ import { useCallback, useState } from "react";
66
77import { useReadOnlyGuard } from "~/modules/auth" ;
88import { NewTabNotice } from "~/modules/layout/shared/NewTabNotice" ;
9- import { FileUpload , useFileUploadForm } from "~/modules/shared" ;
9+ import { FileUpload , getDsfrModal , useFileUploadForm } from "~/modules/shared" ;
1010import { api } from "~/trpc/react" ;
1111
1212import { CseStepIndicator } from "./components/CseStepIndicator" ;
@@ -29,6 +29,7 @@ export function Step2Upload({
2929 const router = useRouter ( ) ;
3030 const utils = api . useUtils ( ) ;
3131 const [ deletingFileId , setDeletingFileId ] = useState < string | null > ( null ) ;
32+ const [ finalizeError , setFinalizeError ] = useState < string | null > ( null ) ;
3233 const readOnlyGuard = useReadOnlyGuard ( ) ;
3334
3435 const refreshFileList = useCallback ( ( ) => {
@@ -44,6 +45,21 @@ export function Step2Upload({
4445 onError : ( ) => setDeletingFileId ( null ) ,
4546 } ) ;
4647
48+ const finalizeMutation = api . cseOpinion . finalize . useMutation ( ) ;
49+
50+ const finalizeAndRedirect = useCallback ( async ( ) => {
51+ try {
52+ await finalizeMutation . mutateAsync ( ) ;
53+ router . push ( "/avis-cse/confirmation" ) ;
54+ } catch ( error ) {
55+ setFinalizeError (
56+ error instanceof Error
57+ ? error . message
58+ : "Erreur lors de la validation du dépôt." ,
59+ ) ;
60+ }
61+ } , [ finalizeMutation , router ] ) ;
62+
4763 const {
4864 closeModal,
4965 handleConfirm,
@@ -56,14 +72,44 @@ export function Step2Upload({
5672 } = useFileUploadForm ( {
5773 flowType : "cse_opinion" ,
5874 onUploaded : refreshFileList ,
59- onAllUploaded : ( ) => router . push ( "/avis-cse/confirmation" ) ,
75+ onAllUploaded : ( ) => {
76+ void finalizeAndRedirect ( ) ;
77+ } ,
6078 } ) ;
6179
80+ const skipUploadSubmit = useCallback (
81+ ( event : React . FormEvent ) => {
82+ event . preventDefault ( ) ;
83+ setFinalizeError ( null ) ;
84+ const dialog = modalRef . current ;
85+ if ( ! dialog ) return ;
86+ const modal = getDsfrModal ( dialog ) ;
87+ if ( modal ) {
88+ modal . disclose ( ) ;
89+ } else {
90+ dialog . showModal ( ) ;
91+ }
92+ } ,
93+ [ modalRef ] ,
94+ ) ;
95+
96+ const hasExistingFiles = existingFiles . length > 0 ;
97+ const hasSelectedFiles = selectedFiles . length > 0 ;
98+ const formSubmit =
99+ ! hasSelectedFiles && hasExistingFiles ? skipUploadSubmit : handleSubmit ;
100+ const confirmAction = hasSelectedFiles
101+ ? handleConfirm
102+ : ( ) => {
103+ closeModal ( ) ;
104+ void finalizeAndRedirect ( ) ;
105+ } ;
106+
62107 const remainingSlots = MAX_CSE_FILES - existingFiles . length ;
108+ const isSubmitting = isPending || finalizeMutation . isPending ;
63109
64110 return (
65111 < >
66- < form onSubmit = { handleSubmit } >
112+ < form onSubmit = { formSubmit } >
67113 < div className = "fr-grid-row fr-grid-row--middle fr-mb-3w" >
68114 < div className = "fr-col" >
69115 < h1 className = "fr-h4 fr-mb-0" >
@@ -118,6 +164,12 @@ export function Step2Upload({
118164 />
119165 </ div >
120166
167+ { finalizeError && (
168+ < p className = "fr-error-text fr-mt-2w" role = "alert" >
169+ { finalizeError }
170+ </ p >
171+ ) }
172+
121173 < div className = { `fr-mt-4w ${ formStyles . actions } ` } >
122174 < Link
123175 className = "fr-btn fr-btn--tertiary fr-icon-arrow-left-line fr-btn--icon-left"
@@ -129,10 +181,10 @@ export function Step2Upload({
129181 < button
130182 { ...readOnlyGuard . buttonProps }
131183 className = "fr-btn fr-icon-arrow-right-line fr-btn--icon-right"
132- disabled = { isPending || readOnlyGuard . isReadOnly }
184+ disabled = { isSubmitting || readOnlyGuard . isReadOnly }
133185 type = "submit"
134186 >
135- { isPending ? "Envoi en cours\u2026" : "Soumettre" }
187+ { isSubmitting ? "Envoi en cours\u2026" : "Soumettre" }
136188 </ button >
137189 { readOnlyGuard . tooltip }
138190 </ span >
@@ -143,7 +195,7 @@ export function Step2Upload({
143195 declarationYear = { declarationYear }
144196 modalRef = { modalRef }
145197 onClose = { closeModal }
146- onSubmit = { handleConfirm }
198+ onSubmit = { confirmAction }
147199 />
148200 </ >
149201 ) ;
0 commit comments