Skip to content

Commit f8db4de

Browse files
frozenheliumAdityaKhatri
authored andcommitted
refactor(dref-export-modal): update pga state management logic
1 parent 5a6e8cb commit f8db4de

File tree

5 files changed

+95
-155
lines changed

5 files changed

+95
-155
lines changed

app/src/components/domain/DrefExportModal/i18n.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
"drefPreparingExport": "Preparing for export...",
66
"drefWaitingExport": "Waiting for the export to complete...",
77
"drefExportFailed": "Export failed",
8-
"drefExportSuccessfully": "Export completed successfully",
8+
"drefExportSuccessfully": "Export completed successfully!",
99
"drefClickDownloadLink": "Click on the download link below!",
1010
"drefDownloadPDF": "Download PDF",
11+
"includePgaLabel": "Include PGA",
12+
"startExportLabel": "Start Export",
13+
"configureExportLabel": "Configure export",
1114
"drefDownloadPDFWithPGA": "Download PDF with PGA",
1215
"drefDownloadPDFwithoutPGA": "Download PDF without PGA",
1316
"drefFailureToExportMessage":"Failed to export PDF."
1417
}
15-
}
18+
}

app/src/components/domain/DrefExportModal/index.tsx

Lines changed: 78 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
useCallback,
32
useEffect,
43
useMemo,
54
useState,
65
} from 'react';
6+
import { DownloadLineIcon } from '@ifrc-go/icons';
77
import {
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
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
.download-button {
2-
display: flex;
3-
align-items: center;
4-
flex-direction: column;
5-
padding: var(--go-ui-spacing-md);
6-
}
1+
.dref-export-modal {
2+
.icon {
3+
font-size: var(--go-ui-height-icon-multiplier);
4+
}
5+
}

app/src/views/DrefApplicationForm/Overview/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,9 @@ function Overview(props: Props) {
421421
/>
422422
)}
423423
</InputSection>
424-
{(
425-
value?.disaster_category === DISASTER_CATEGORY_ORANGE
426-
|| value?.disaster_category === DISASTER_CATEGORY_RED)
427-
&& value?.type_of_dref !== TYPE_IMMINENT && (
424+
{(value?.disaster_category === DISASTER_CATEGORY_ORANGE
425+
|| value?.disaster_category === DISASTER_CATEGORY_RED
426+
) && value?.type_of_dref !== TYPE_IMMINENT && (
428427
<InputSection title={strings.drefFormUploadCrisisDocument}>
429428
<GoSingleFileInput
430429
name="disaster_category_analysis"

app/src/views/DrefFinalReportForm/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,15 @@ export function Component() {
258258
}),
259259
),
260260
// Process proposed actions with injected client IDs, defaulting if missing
261-
proposed_action: isDefined(proposed_action)
262-
&& proposed_action.length > 1 ? proposed_action?.map(
261+
proposed_action: isDefined(proposed_action) && proposed_action.length > 1
262+
? proposed_action?.map(
263263
(action) => ({
264264
...injectClientId(action),
265265
activities: action.activities?.map(injectClientId),
266266
}),
267267
// NOTE: Sort and display early actions before early response
268-
).sort((a, b) => a.proposed_type - b.proposed_type) : [
268+
).sort((a, b) => a.proposed_type - b.proposed_type)
269+
: [
269270
{
270271
client_id: randomString(),
271272
proposed_type: EARLY_ACTION,

0 commit comments

Comments
 (0)