-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.tsx
More file actions
142 lines (129 loc) · 4.14 KB
/
index.tsx
File metadata and controls
142 lines (129 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
useMemo,
useState,
} from 'react';
import {
Message,
Modal,
} from '@ifrc-go/ui';
import { useTranslation } from '@ifrc-go/ui/hooks';
import {
isDefined,
isNotDefined,
} from '@togglecorp/fujs';
import Link from '#components/Link';
import { type components } from '#generated/types';
import {
type GoApiBody,
useRequest,
} from '#utils/restRequest';
import i18n from './i18n.json';
type ExportStatusEnum = components<'read'>['schemas']['ExportStatusEnum'];
type ExportBody = GoApiBody<'/api/v2/pdf-export/', 'POST'>;
const EXPORT_STATUS_PENDING = 0 satisfies ExportStatusEnum;
const EXPORT_STATUS_COMPLETED = 1 satisfies ExportStatusEnum;
const EXPORT_STATUS_ERRORED = 2 satisfies ExportStatusEnum;
interface Props {
perId: string;
countryId: string;
onCancel: () => void;
}
function PerExportModal(props: Props) {
const {
perId,
countryId,
onCancel,
} = props;
const strings = useTranslation(i18n);
const [exportId, setExportId] = useState<number | undefined>();
const exportTriggerBody = useMemo(
() => ({
export_id: Number(perId),
export_type: 'per' as const,
per_country: Number(countryId),
is_pga: undefined,
version: undefined,
diff: undefined,
} satisfies ExportBody),
[perId, countryId],
);
const {
pending: pendingExportTrigger,
error: exportTriggerError,
} = useRequest({
skip: isDefined(exportId) || isNotDefined(perId),
method: 'POST',
useCurrentLanguageForMutation: true,
url: '/api/v2/pdf-export/',
body: exportTriggerBody,
onSuccess: (response) => {
if (isDefined(response.id)) {
setExportId(response.id);
}
},
});
const {
pending: pendingExportStatus,
response: exportStatusResponse,
error: exportStatusError,
} = useRequest({
skip: isNotDefined(exportId),
url: '/api/v2/pdf-export/{id}/',
// FIXME: typings should be fixed in the server
pathVariables: isDefined(exportId) ? ({ id: String(exportId) }) : undefined,
shouldPoll: (poll) => {
if (poll?.errored || poll?.value?.status !== EXPORT_STATUS_PENDING) {
return -1;
}
return 5000;
},
});
return (
<Modal
heading={strings.perExportTitle}
onClose={onCancel}
>
{pendingExportTrigger && (
<Message
pending
title={strings.perPreparingExport}
/>
)}
{(pendingExportStatus || exportStatusResponse?.status === EXPORT_STATUS_PENDING) && (
<Message
pending
title={strings.perWaitingExport}
/>
)}
{(exportStatusResponse?.status === EXPORT_STATUS_ERRORED
|| isDefined(exportTriggerError)
|| isDefined(exportStatusError)
) && (
<Message
title={strings.perExportFailed}
description={exportTriggerError?.value.messageForNotification
?? exportStatusError?.value.messageForNotification}
/>
)}
{isDefined(exportStatusResponse)
&& exportStatusResponse.status === EXPORT_STATUS_COMPLETED
&& isDefined(exportStatusResponse.pdf_file) && (
<Message
title={strings.perExportSuccessfully}
description={strings.perClickDownloadLink}
actions={(
<Link
colorVariant="primary"
styleVariant="outline"
href={exportStatusResponse?.pdf_file}
external
>
{strings.perDownloadPDF}
</Link>
)}
/>
)}
</Modal>
);
}
export default PerExportModal;