11import {
2- useCallback ,
2+ useEffect ,
33 useMemo ,
44 useState ,
55} from 'react' ;
6+ import { DownloadLineIcon } from '@ifrc-go/icons' ;
67import {
78 Button ,
89 Checkbox ,
@@ -56,33 +57,7 @@ function DrefExportModal(props: Props) {
5657 const alert = useAlert ( ) ;
5758
5859 const [ exportId , setExportId ] = useState < number | undefined > ( ) ;
59- const [ isPga , setIsPga ] = useState < boolean > ( false ) ;
60- const [ isPgaCheckboxVisible , setIsPgaCheckboxVisible ] = useState ( true ) ;
61-
62- const drefExportTriggerBody = useMemo (
63- ( ) => {
64- let type : ExportTypeEnum ;
65- if ( applicationType === 'OPS_UPDATE' ) {
66- type = 'dref-operational-updates' ;
67- } else if ( applicationType === 'FINAL_REPORT' ) {
68- type = 'dref-final-reports' ;
69- } else {
70- type = 'dref-applications' ;
71- }
72- return {
73- export_id : id ,
74- export_type : type ,
75- is_pga : isPga ,
76- selector : '#pdf-preview-ready' ,
77- per_country : undefined ,
78- } ;
79- } ,
80- [
81- id ,
82- isPga ,
83- applicationType ,
84- ] ,
85- ) ;
60+ const [ includePga , setIncludePga ] = useState < boolean > ( false ) ;
8661
8762 const exportTriggerBody = useMemo (
8863 ( ) => {
@@ -98,45 +73,23 @@ function DrefExportModal(props: Props) {
9873 return {
9974 export_id : id ,
10075 export_type : type ,
101- is_pga : isPga ,
76+ is_pga : includePga ,
10277 selector : '#pdf-preview-ready' ,
10378 per_country : undefined ,
10479 } ;
10580 } ,
10681 [
10782 id ,
108- isPga ,
83+ includePga ,
10984 applicationType ,
11085 ] ,
11186 ) ;
11287
11388 const {
114- pending : pendingDrefImminentExportTrigger ,
115- error : drefImminentExportError ,
116- trigger : drefImminentExportTrigger ,
89+ pending : exportPending ,
90+ error : exportError ,
91+ trigger : triggerExport ,
11792 } = useLazyRequest ( {
118- method : 'POST' ,
119- useCurrentLanguageForMutation : true ,
120- url : '/api/v2/pdf-export/' ,
121- body : drefExportTriggerBody ,
122- onSuccess : ( response ) => {
123- if ( isDefined ( response . id ) ) {
124- setExportId ( response . id ) ;
125- }
126- } ,
127- onFailure : ( ) => {
128- alert . show (
129- strings . drefFailureToExportMessage ,
130- { variant : 'danger' } ,
131- ) ;
132- } ,
133- } ) ;
134-
135- const {
136- pending : pendingExportTrigger ,
137- error : exportTriggerError ,
138- } = useRequest ( {
139- skip : isDefined ( exportId ) || isNotDefined ( id ) || drefType === DREF_TYPE_IMMINENT ,
14093 method : 'POST' ,
14194 useCurrentLanguageForMutation : true ,
14295 url : '/api/v2/pdf-export/' ,
@@ -154,8 +107,22 @@ function DrefExportModal(props: Props) {
154107 } ,
155108 } ) ;
156109
110+ useEffect ( ( ) => {
111+ if ( isDefined ( exportId ) || isNotDefined ( id ) ) {
112+ return ;
113+ }
114+
115+ // Don't automatically trigger the export for imminent DREF Applications
116+ // We need to allow users to configure PGA before the export
117+ if ( drefType === DREF_TYPE_IMMINENT && applicationType === 'DREF' ) {
118+ return ;
119+ }
120+
121+ triggerExport ( null ) ;
122+ } , [ exportId , id , drefType , applicationType , triggerExport ] ) ;
123+
157124 const {
158- pending : pendingExportStatus ,
125+ pending : exportStatusPending ,
159126 response : exportStatusResponse ,
160127 error : exportStatusError ,
161128 } = useRequest ( {
@@ -172,108 +139,101 @@ function DrefExportModal(props: Props) {
172139 } ,
173140 } ) ;
174141
175- const handleDrefImminent = useCallback ( ( ) => {
176- setIsPgaCheckboxVisible ( false ) ;
177- drefImminentExportTrigger ( drefExportTriggerBody ) ;
142+ const exportStatus = useMemo ( ( ) => {
143+ if ( exportPending ) {
144+ return 'PREPARE' ;
145+ }
146+
147+ if ( exportStatusPending || exportStatusResponse ?. status === EXPORT_STATUS_PENDING ) {
148+ return 'WAITING' ;
149+ }
150+
151+ if ( isDefined ( exportStatusError )
152+ || isDefined ( exportError )
153+ || ( isDefined ( exportStatusResponse )
154+ && exportStatusResponse . status === EXPORT_STATUS_ERRORED )
155+ ) {
156+ return 'FAILED' ;
157+ }
158+
159+ if ( isDefined ( exportStatusResponse )
160+ && isDefined ( exportStatusResponse . status === EXPORT_STATUS_COMPLETED )
161+ && isDefined ( exportStatusResponse . pdf_file )
162+ ) {
163+ return 'SUCCESS' ;
164+ }
165+
166+ return 'NOT_STARTED' ;
178167 } , [
179- drefExportTriggerBody ,
180- drefImminentExportTrigger ,
168+ exportPending ,
169+ exportStatusError ,
170+ exportError ,
171+ exportStatusPending ,
172+ exportStatusResponse ,
181173 ] ) ;
182174
183175 return (
184176 < Modal
185177 heading = { strings . drefExportTitle }
186178 onClose = { onCancel }
179+ className = { styles . drefExportModal }
187180 >
188- { drefType === DREF_TYPE_IMMINENT
189- && isPgaCheckboxVisible
190- && ! ( pendingExportTrigger
191- || pendingExportStatus
192- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING )
193- && (
194- < Checkbox
195- name = { undefined }
196- value = { isPga }
197- onChange = { setIsPga }
198- label = { strings . drefDownloadPDFWithPGA }
199- />
200- ) }
201- { pendingExportTrigger && pendingDrefImminentExportTrigger && (
181+ { exportStatus === 'PREPARE' && (
202182 < Message
203183 pending
204184 title = { strings . drefPreparingExport }
205185 />
206186 ) }
207- { ( pendingExportStatus
208- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING ) && (
187+ { exportStatus === 'WAITING' && (
209188 < Message
210189 pending
211190 title = { strings . drefWaitingExport }
212191 />
213192 ) }
214- { ( exportStatusResponse ?. status === EXPORT_STATUS_ERRORED
215- || isDefined ( exportTriggerError )
216- || isDefined ( exportStatusError )
217- || isDefined ( drefImminentExportError )
218- ) && (
193+ { exportStatus === 'FAILED' && (
219194 < Message
220195 title = { strings . drefExportFailed }
221- description = { exportTriggerError ?. value . messageForNotification
222- ?? exportStatusError ?. value . messageForNotification
223- ?? drefImminentExportError ?. value . messageForNotification }
196+ description = { exportError ?. value . messageForNotification
197+ ?? exportStatusError ?. value . messageForNotification }
224198 />
225199 ) }
226- { ! ( pendingExportTrigger
227- || pendingExportStatus
228- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING )
229- && drefType === DREF_TYPE_IMMINENT
230- && ! drefImminentExportError && (
231- exportStatusResponse ?. pdf_file ? (
232- < Message
233- title = { strings . drefExportSuccessfully }
234- description = { strings . drefClickDownloadLink }
235- actions = { (
236- < Link
237- variant = "secondary"
238- href = { exportStatusResponse ?. pdf_file }
239- external
240- >
241- { strings . drefDownloadPDF }
242- </ Link >
243- ) }
244- />
245- ) : ( ! exportStatusResponse && (
246- < div className = { styles . downloadButton } >
247- < Button
248- variant = "secondary"
249- name = { undefined }
250- onClick = { handleDrefImminent }
251- >
252- { isPga
253- ? strings . drefDownloadPDFWithPGA
254- : strings . drefDownloadPDFwithoutPGA }
255- </ Button >
256- </ div >
257- ) )
258- ) }
259- { isDefined ( exportStatusResponse )
260- && exportStatusResponse . status === EXPORT_STATUS_COMPLETED
261- && isDefined ( exportStatusResponse . pdf_file )
262- && drefType !== DREF_TYPE_IMMINENT && (
200+ { exportStatus === 'SUCCESS' && (
263201 < Message
264202 title = { strings . drefExportSuccessfully }
265203 description = { strings . drefClickDownloadLink }
266204 actions = { (
267205 < Link
268206 variant = "secondary"
269207 href = { exportStatusResponse ?. pdf_file }
208+ icons = { < DownloadLineIcon className = { styles . icon } /> }
270209 external
271210 >
272211 { strings . drefDownloadPDF }
273212 </ Link >
274213 ) }
275214 />
276215 ) }
216+ { exportStatus === 'NOT_STARTED' && (
217+ < Message
218+ title = { strings . configureExportLabel }
219+ description = { drefType === DREF_TYPE_IMMINENT && applicationType !== 'FINAL_REPORT' && (
220+ < Checkbox
221+ name = { undefined }
222+ value = { includePga }
223+ onChange = { setIncludePga }
224+ label = { strings . includePgaLabel }
225+ />
226+ ) }
227+ actions = { (
228+ < Button
229+ name = { null }
230+ onClick = { triggerExport }
231+ >
232+ { strings . startExportLabel }
233+ </ Button >
234+ ) }
235+ />
236+ ) }
277237 </ Modal >
278238 ) ;
279239}
0 commit comments