|
| 1 | +import { |
| 2 | + useEffect, |
| 3 | + useMemo, |
| 4 | + useState, |
| 5 | +} from 'react'; |
| 6 | +import { DownloadLineIcon } from '@ifrc-go/icons'; |
| 7 | +import { |
| 8 | + Message, |
| 9 | + Modal, |
| 10 | +} from '@ifrc-go/ui'; |
| 11 | +import { useTranslation } from '@ifrc-go/ui/hooks'; |
| 12 | +import { |
| 13 | + isDefined, |
| 14 | + isNotDefined, |
| 15 | +} from '@togglecorp/fujs'; |
| 16 | + |
| 17 | +import Link from '#components/Link'; |
| 18 | +import { type components } from '#generated/types'; |
| 19 | +import useAlert from '#hooks/useAlert'; |
| 20 | +import { EAP_TYPE_SIMPLIFIED } from '#utils/constants'; |
| 21 | +import { |
| 22 | + type GoApiBody, |
| 23 | + useLazyRequest, |
| 24 | + useRequest, |
| 25 | +} from '#utils/restRequest'; |
| 26 | + |
| 27 | +import i18n from './i18n.json'; |
| 28 | +import styles from './styles.module.css'; |
| 29 | + |
| 30 | +type EapType = components['schemas']['EapEapTypeEnumKey']; |
| 31 | +type ExportStatusEnum = components<'read'>['schemas']['ExportStatusEnum']; |
| 32 | + |
| 33 | +type ExportBody = GoApiBody<'/api/v2/pdf-export/', 'POST'>; |
| 34 | + |
| 35 | +const EXPORT_STATUS_PENDING = 0 satisfies ExportStatusEnum; |
| 36 | +const EXPORT_STATUS_COMPLETED = 1 satisfies ExportStatusEnum; |
| 37 | +const EXPORT_STATUS_ERRORED = 2 satisfies ExportStatusEnum; |
| 38 | + |
| 39 | +interface Props { |
| 40 | + eapId: number; |
| 41 | + eapType: EapType; |
| 42 | + onClose: () => void; |
| 43 | +} |
| 44 | + |
| 45 | +function EapExportModal(props: Props) { |
| 46 | + const { |
| 47 | + eapId, |
| 48 | + eapType, |
| 49 | + onClose, |
| 50 | + } = props; |
| 51 | + |
| 52 | + const strings = useTranslation(i18n); |
| 53 | + const alert = useAlert(); |
| 54 | + |
| 55 | + const [exportId, setExportId] = useState<number | undefined>(); |
| 56 | + |
| 57 | + const exportTriggerBody = useMemo<ExportBody>( |
| 58 | + () => ({ |
| 59 | + export_id: eapId, |
| 60 | + export_type: eapType === EAP_TYPE_SIMPLIFIED ? 'simplified-eap' : 'full-eap', |
| 61 | + selector: '#pdf-preview-ready', |
| 62 | + is_pga: false, |
| 63 | + per_country: undefined, |
| 64 | + }), |
| 65 | + [eapId, eapType], |
| 66 | + ); |
| 67 | + |
| 68 | + const { |
| 69 | + pending: exportPending, |
| 70 | + error: exportError, |
| 71 | + trigger: triggerExport, |
| 72 | + } = useLazyRequest({ |
| 73 | + method: 'POST', |
| 74 | + useCurrentLanguageForMutation: true, |
| 75 | + url: '/api/v2/pdf-export/', |
| 76 | + body: exportTriggerBody, |
| 77 | + onSuccess: (response) => { |
| 78 | + if (isDefined(response.id)) { |
| 79 | + setExportId(response.id); |
| 80 | + } |
| 81 | + }, |
| 82 | + onFailure: () => { |
| 83 | + alert.show( |
| 84 | + strings.failureToExportMessage, |
| 85 | + { variant: 'danger' }, |
| 86 | + ); |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + triggerExport(null); |
| 92 | + }, [triggerExport]); |
| 93 | + |
| 94 | + const { |
| 95 | + pending: exportStatusPending, |
| 96 | + response: exportStatusResponse, |
| 97 | + error: exportStatusError, |
| 98 | + } = useRequest({ |
| 99 | + skip: isNotDefined(exportId), |
| 100 | + url: '/api/v2/pdf-export/{id}/', |
| 101 | + // FIXME: typings should be fixed in the server |
| 102 | + pathVariables: isDefined(exportId) ? ({ id: String(exportId) }) : undefined, |
| 103 | + shouldPoll: (poll) => { |
| 104 | + if (poll?.errored || poll?.value?.status !== EXPORT_STATUS_PENDING) { |
| 105 | + return -1; |
| 106 | + } |
| 107 | + |
| 108 | + return 5000; |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + const exportStatus = useMemo(() => { |
| 113 | + if (exportPending) { |
| 114 | + return 'PREPARE'; |
| 115 | + } |
| 116 | + |
| 117 | + if (exportStatusPending || exportStatusResponse?.status === EXPORT_STATUS_PENDING) { |
| 118 | + return 'WAITING'; |
| 119 | + } |
| 120 | + |
| 121 | + if (isDefined(exportStatusError) |
| 122 | + || isDefined(exportError) |
| 123 | + || (isDefined(exportStatusResponse) |
| 124 | + && exportStatusResponse.status === EXPORT_STATUS_ERRORED) |
| 125 | + ) { |
| 126 | + return 'FAILED'; |
| 127 | + } |
| 128 | + |
| 129 | + if (isDefined(exportStatusResponse) |
| 130 | + && isDefined(exportStatusResponse.status === EXPORT_STATUS_COMPLETED) |
| 131 | + && isDefined(exportStatusResponse.pdf_file) |
| 132 | + ) { |
| 133 | + return 'SUCCESS'; |
| 134 | + } |
| 135 | + |
| 136 | + return 'NOT_STARTED'; |
| 137 | + }, [ |
| 138 | + exportPending, |
| 139 | + exportStatusError, |
| 140 | + exportError, |
| 141 | + exportStatusPending, |
| 142 | + exportStatusResponse, |
| 143 | + ]); |
| 144 | + |
| 145 | + return ( |
| 146 | + <Modal |
| 147 | + heading={strings.exportTitle} |
| 148 | + onClose={onClose} |
| 149 | + className={styles.drefExportModal} |
| 150 | + > |
| 151 | + {exportStatus === 'PREPARE' && ( |
| 152 | + <Message |
| 153 | + pending |
| 154 | + title={strings.preparingExport} |
| 155 | + /> |
| 156 | + )} |
| 157 | + {exportStatus === 'WAITING' && ( |
| 158 | + <Message |
| 159 | + pending |
| 160 | + title={strings.waitingExport} |
| 161 | + /> |
| 162 | + )} |
| 163 | + {exportStatus === 'FAILED' && ( |
| 164 | + <Message |
| 165 | + title={strings.exportFailed} |
| 166 | + description={exportError?.value.messageForNotification |
| 167 | + ?? exportStatusError?.value.messageForNotification} |
| 168 | + /> |
| 169 | + )} |
| 170 | + {exportStatus === 'SUCCESS' && ( |
| 171 | + <Message |
| 172 | + title={strings.exportSuccessfully} |
| 173 | + description={strings.downloadLinkDescription} |
| 174 | + actions={( |
| 175 | + <Link |
| 176 | + colorVariant="primary" |
| 177 | + styleVariant="outline" |
| 178 | + href={exportStatusResponse?.pdf_file} |
| 179 | + before={<DownloadLineIcon className={styles.icon} />} |
| 180 | + external |
| 181 | + > |
| 182 | + {strings.downloadLinkLabel} |
| 183 | + </Link> |
| 184 | + )} |
| 185 | + /> |
| 186 | + )} |
| 187 | + </Modal> |
| 188 | + ); |
| 189 | +} |
| 190 | + |
| 191 | +export default EapExportModal; |
0 commit comments