|
| 1 | +// Copyright (c) 2014 - 2023 UNICEF. All rights reserved. |
| 2 | + |
| 3 | +import PropTypes from "prop-types"; |
| 4 | +import { useDispatch } from "react-redux"; |
| 5 | +import { object, string, date } from "yup"; |
| 6 | +import { useEffect } from "react"; |
| 7 | + |
| 8 | +import { useI18n } from "../../../../i18n"; |
| 9 | +import ActionDialog from "../../../../action-dialog"; |
| 10 | +import Form from "../../../../form"; |
| 11 | +import { formatFileName } from "../../../../record-actions/exports/utils"; |
| 12 | +import { toServerDateFormat, useMemoizedSelector } from "../../../../../libs"; |
| 13 | +import { clearExportedUsageReport, fetchUsageExport } from "../action-creators"; |
| 14 | +import { getUsageReportExport } from "../selectors"; |
| 15 | + |
| 16 | +import { NAME, FORM_ID, EXPORTED_URL } from "./constants"; |
| 17 | +import { form } from "./form"; |
| 18 | +import css from "./style.css"; |
| 19 | + |
| 20 | +function Component({ close, open, pending, setPending }) { |
| 21 | + const i18n = useI18n(); |
| 22 | + const exportedUsageReport = useMemoizedSelector(state => getUsageReportExport(state)); |
| 23 | + const dispatch = useDispatch(); |
| 24 | + const dialogPending = typeof pending === "object" ? pending.get("pending") : pending; |
| 25 | + |
| 26 | + const fieldDisplayName = { |
| 27 | + fromDate: i18n.t("key_performance_indicators.date_range_dialog.from"), |
| 28 | + toDate: i18n.t("key_performance_indicators.date_range_dialog.to"), |
| 29 | + file_name: i18n.t("usage_report.file_name") |
| 30 | + }; |
| 31 | + |
| 32 | + const validationSchema = object({ |
| 33 | + fromDate: date() |
| 34 | + .nullable() |
| 35 | + .required(i18n.t("fields.required_field", { field: fieldDisplayName.fromDate })) |
| 36 | + .typeError(i18n.t("usage_report.invalid_date_format", { field: fieldDisplayName.fromDate })), |
| 37 | + toDate: date() |
| 38 | + .nullable() |
| 39 | + .required(i18n.t("fields.required_field", { field: fieldDisplayName.toDate })) |
| 40 | + .typeError(i18n.t("usage_report.invalid_date_format", { field: fieldDisplayName.toDate })) |
| 41 | + .test({ |
| 42 | + name: "is-after", |
| 43 | + message: i18n.t("usage_report.invalid_date_range", { |
| 44 | + from: fieldDisplayName.fromDate, |
| 45 | + to: fieldDisplayName.toDate |
| 46 | + }), |
| 47 | + test: (value, context) => { |
| 48 | + const { fromDate } = context.parent; |
| 49 | + |
| 50 | + return !fromDate || !value || value > fromDate; |
| 51 | + } |
| 52 | + }), |
| 53 | + file_name: string().required(i18n.t("fields.required_field", { field: fieldDisplayName.file_name })) |
| 54 | + }); |
| 55 | + |
| 56 | + // Form submission |
| 57 | + const onSubmit = getData => { |
| 58 | + const fileName = formatFileName(getData.file_name, "xlsx"); |
| 59 | + const defaultBody = { |
| 60 | + export_format: "xlsx", |
| 61 | + record_type: "usage_report", |
| 62 | + file_name: fileName, |
| 63 | + selected_from_date: toServerDateFormat(getData.fromDate), |
| 64 | + selected_to_date: toServerDateFormat(getData.toDate) |
| 65 | + }; |
| 66 | + const data = { ...defaultBody }; |
| 67 | + |
| 68 | + setPending(true); |
| 69 | + dispatch(fetchUsageExport(data, i18n.t("exports.exported"))); |
| 70 | + }; |
| 71 | + |
| 72 | + const handleCustomClose = () => close(); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (!exportedUsageReport.isEmpty() && exportedUsageReport.get(EXPORTED_URL)) { |
| 76 | + window.open(exportedUsageReport.get(EXPORTED_URL)); |
| 77 | + dispatch(clearExportedUsageReport()); |
| 78 | + } |
| 79 | + }, [exportedUsageReport.get(EXPORTED_URL, "")]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <ActionDialog |
| 83 | + open={open} |
| 84 | + confirmButtonProps={{ |
| 85 | + form: FORM_ID, |
| 86 | + type: "submit" |
| 87 | + }} |
| 88 | + cancelHandler={handleCustomClose} |
| 89 | + dialogTitle={i18n.t("usage_report.label")} |
| 90 | + confirmButtonLabel={i18n.t("buttons.export")} |
| 91 | + pending={dialogPending} |
| 92 | + omitCloseAfterSuccess |
| 93 | + > |
| 94 | + <Form |
| 95 | + useCancelPrompt |
| 96 | + formID={FORM_ID} |
| 97 | + mode="new" |
| 98 | + formSections={form(fieldDisplayName)} |
| 99 | + onSubmit={onSubmit} |
| 100 | + formClassName={`${css["usage-reports-form"]}`} |
| 101 | + validations={validationSchema} |
| 102 | + /> |
| 103 | + </ActionDialog> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +Component.displayName = NAME; |
| 108 | + |
| 109 | +Component.propTypes = { |
| 110 | + close: PropTypes.func, |
| 111 | + open: PropTypes.bool, |
| 112 | + pending: PropTypes.bool, |
| 113 | + setPending: PropTypes.func |
| 114 | +}; |
| 115 | + |
| 116 | +export default Component; |
0 commit comments