11import {
2- useCallback ,
32 useEffect ,
43 useMemo ,
54 useState ,
65} from 'react' ;
6+ import { DownloadLineIcon } from '@ifrc-go/icons' ;
77import {
88 Button ,
99 Checkbox ,
@@ -57,34 +57,7 @@ function DrefExportModal(props: Props) {
5757 const alert = useAlert ( ) ;
5858
5959 const [ exportId , setExportId ] = useState < number | undefined > ( ) ;
60- const [ isPga , setIsPga ] = useState < boolean > ( false ) ;
61- const imminentFinalReport = applicationType === 'FINAL_REPORT' && drefType === DREF_TYPE_IMMINENT ;
62- const [ isPgaCheckboxVisible , setIsPgaCheckboxVisible ] = useState ( ( ) => ! imminentFinalReport ) ;
63-
64- const drefExportTriggerBody = useMemo (
65- ( ) => {
66- let type : ExportTypeEnum ;
67- if ( applicationType === 'OPS_UPDATE' ) {
68- type = 'dref-operational-updates' ;
69- } else if ( applicationType === 'FINAL_REPORT' ) {
70- type = 'dref-final-reports' ;
71- } else {
72- type = 'dref-applications' ;
73- }
74- return {
75- export_id : id ,
76- export_type : type ,
77- is_pga : isPga ,
78- selector : '#pdf-preview-ready' ,
79- per_country : undefined ,
80- } ;
81- } ,
82- [
83- id ,
84- isPga ,
85- applicationType ,
86- ] ,
87- ) ;
60+ const [ includePga , setIncludePga ] = useState < boolean > ( false ) ;
8861
8962 const exportTriggerBody = useMemo (
9063 ( ) => {
@@ -100,51 +73,26 @@ function DrefExportModal(props: Props) {
10073 return {
10174 export_id : id ,
10275 export_type : type ,
103- is_pga : isPga ,
76+ is_pga : includePga ,
10477 selector : '#pdf-preview-ready' ,
10578 per_country : undefined ,
10679 } ;
10780 } ,
10881 [
10982 id ,
110- isPga ,
83+ includePga ,
11184 applicationType ,
11285 ] ,
11386 ) ;
11487
11588 const {
116- pending : pendingDrefImminentExportTrigger ,
117- error : drefImminentExportError ,
118- trigger : drefImminentExportTrigger ,
89+ pending : exportPending ,
90+ error : exportError ,
91+ trigger : triggerExport ,
11992 } = useLazyRequest ( {
12093 method : 'POST' ,
12194 useCurrentLanguageForMutation : true ,
12295 url : '/api/v2/pdf-export/' ,
123- body : drefExportTriggerBody ,
124- onSuccess : ( response ) => {
125- if ( isDefined ( response . id ) ) {
126- setExportId ( response . id ) ;
127- }
128- } ,
129- onFailure : ( ) => {
130- alert . show (
131- strings . drefFailureToExportMessage ,
132- { variant : 'danger' } ,
133- ) ;
134- } ,
135- } ) ;
136-
137- const {
138- pending : pendingExportTrigger ,
139- error : exportTriggerError ,
140- } = useRequest ( {
141- skip : isDefined ( exportId )
142- || isNotDefined ( id )
143- || drefType === DREF_TYPE_IMMINENT
144- || imminentFinalReport ,
145- method : 'POST' ,
146- useCurrentLanguageForMutation : true ,
147- url : '/api/v2/pdf-export/' ,
14896 body : exportTriggerBody ,
14997 onSuccess : ( response ) => {
15098 if ( isDefined ( response . id ) ) {
@@ -159,8 +107,22 @@ function DrefExportModal(props: Props) {
159107 } ,
160108 } ) ;
161109
110+ useEffect ( ( ) => {
111+ if ( isDefined ( exportId ) || isNotDefined ( id ) ) {
112+ return ;
113+ }
114+
115+ // Don't automatically trigger the export for imminent DREFs (except for Final Reports)
116+ // We need to allow users to configure PGA before the export
117+ if ( drefType === DREF_TYPE_IMMINENT && applicationType !== 'FINAL_REPORT' ) {
118+ return ;
119+ }
120+
121+ triggerExport ( null ) ;
122+ } , [ exportId , id , drefType , applicationType , triggerExport ] ) ;
123+
162124 const {
163- pending : pendingExportStatus ,
125+ pending : exportStatusPending ,
164126 response : exportStatusResponse ,
165127 error : exportStatusError ,
166128 } = useRequest ( {
@@ -177,125 +139,101 @@ function DrefExportModal(props: Props) {
177139 } ,
178140 } ) ;
179141
180- const handleDrefImminent = useCallback ( ( ) => {
181- setIsPgaCheckboxVisible ( false ) ;
182- drefImminentExportTrigger ( drefExportTriggerBody ) ;
183- } , [
184- drefExportTriggerBody ,
185- drefImminentExportTrigger ,
186- ] ) ;
142+ const exportStatus = useMemo ( ( ) => {
143+ if ( exportPending ) {
144+ return 'PREPARE' ;
145+ }
187146
188- useEffect ( ( ) => {
189- if (
190- imminentFinalReport
191- && ! exportId
192- && ! pendingDrefImminentExportTrigger
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 )
193162 ) {
194- drefImminentExportTrigger ( drefExportTriggerBody ) ;
163+ return 'SUCCESS' ;
195164 }
165+
166+ return 'NOT_STARTED' ;
196167 } , [
197- imminentFinalReport ,
198- exportId ,
199- pendingDrefImminentExportTrigger ,
200- drefImminentExportTrigger ,
201- drefExportTriggerBody ,
168+ exportPending ,
169+ exportStatusError ,
170+ exportError ,
171+ exportStatusPending ,
172+ exportStatusResponse ,
202173 ] ) ;
203174
204175 return (
205176 < Modal
206177 heading = { strings . drefExportTitle }
207178 onClose = { onCancel }
179+ className = { styles . drefExportModal }
208180 >
209- { drefType === DREF_TYPE_IMMINENT
210- && isPgaCheckboxVisible
211- && ! ( pendingExportTrigger
212- || pendingExportStatus
213- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING )
214- && (
215- < Checkbox
216- name = { undefined }
217- value = { isPga }
218- onChange = { setIsPga }
219- label = { strings . drefDownloadPDFWithPGA }
220- />
221- ) }
222- { pendingExportTrigger && pendingDrefImminentExportTrigger && (
181+ { exportStatus === 'PREPARE' && (
223182 < Message
224183 pending
225184 title = { strings . drefPreparingExport }
226185 />
227186 ) }
228- { ( pendingExportStatus
229- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING ) && (
187+ { exportStatus === 'WAITING' && (
230188 < Message
231189 pending
232190 title = { strings . drefWaitingExport }
233191 />
234192 ) }
235- { ( exportStatusResponse ?. status === EXPORT_STATUS_ERRORED
236- || isDefined ( exportTriggerError )
237- || isDefined ( exportStatusError )
238- || isDefined ( drefImminentExportError )
239- ) && (
193+ { exportStatus === 'FAILED' && (
240194 < Message
241195 title = { strings . drefExportFailed }
242- description = { exportTriggerError ?. value . messageForNotification
243- ?? exportStatusError ?. value . messageForNotification
244- ?? drefImminentExportError ?. value . messageForNotification }
196+ description = { exportError ?. value . messageForNotification
197+ ?? exportStatusError ?. value . messageForNotification }
245198 />
246199 ) }
247- { ! imminentFinalReport
248- && ! ( pendingExportTrigger
249- || pendingExportStatus
250- || exportStatusResponse ?. status === EXPORT_STATUS_PENDING )
251- && drefType === DREF_TYPE_IMMINENT
252- && ! drefImminentExportError && (
253- exportStatusResponse ?. pdf_file ? (
254- < Message
255- title = { strings . drefExportSuccessfully }
256- description = { strings . drefClickDownloadLink }
257- actions = { (
258- < Link
259- variant = "secondary"
260- href = { exportStatusResponse ?. pdf_file }
261- external
262- >
263- { strings . drefDownloadPDF }
264- </ Link >
265- ) }
266- />
267- ) : ( ! exportStatusResponse && (
268- < div className = { styles . downloadButton } >
269- < Button
270- variant = "secondary"
271- name = { undefined }
272- onClick = { handleDrefImminent }
273- >
274- { isPga
275- ? strings . drefDownloadPDFWithPGA
276- : strings . drefDownloadPDFwithoutPGA }
277- </ Button >
278- </ div >
279- ) )
280- ) }
281- { isDefined ( exportStatusResponse )
282- && exportStatusResponse . status === EXPORT_STATUS_COMPLETED
283- && isDefined ( exportStatusResponse . pdf_file )
284- && drefType !== DREF_TYPE_IMMINENT && (
200+ { exportStatus === 'SUCCESS' && (
285201 < Message
286202 title = { strings . drefExportSuccessfully }
287203 description = { strings . drefClickDownloadLink }
288204 actions = { (
289205 < Link
290206 variant = "secondary"
291207 href = { exportStatusResponse ?. pdf_file }
208+ icons = { < DownloadLineIcon className = { styles . icon } /> }
292209 external
293210 >
294211 { strings . drefDownloadPDF }
295212 </ Link >
296213 ) }
297214 />
298215 ) }
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+ ) }
299237 </ Modal >
300238 ) ;
301239}
0 commit comments