/* eslint-disable @typescript-eslint/no-explicit-any */ import { useEffect, useRef, useState } from "react"; import { ExportType } from "./type"; import FactoryRenderer from "../../../../Renderer"; import { ExportDataScope } from "../../type"; interface ExportPopupProps { setTogglePopup: React.Dispatch>; onExport: (callBack: () => void, exportViewModel: ExportViewModel) => void; onExportDelete: (callBack: ((args: any) => void) | null, id: string) => void; onExportList: (callBack: ((args: any) => void) | null) => void; onExportDownload: ( callBack: ((args: any) => void) | null, id: string, ) => void; exportTypes?: string[]; dataScopes?: ExportDataScope[]; } export type ExportViewModel = { exportType: string; } & ExportDataScope; enum Status { IN_PROGRESS = "InProcess", COMPLETED = "Success", FAILED = "Error", SCHEDULED = "Scheduled", } type ExportHistoryViewModel = { id: string; fileName: string; exportType: ExportType; exportDataScope: string; date: string; status: string; src: string; referenceId: string; }; /* const sampleData = [ { id: "1", fileName: "Export1", date: "2023-10-01", status: Status.COMPLETED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "2", fileName: "Export2", date: "2023-10-02", status: Status.IN_PROGRESS, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "3", fileName: "Export3", date: "2023-10-03", status: Status.FAILED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "1", fileName: "Export1", date: "2023-10-01", status: Status.COMPLETED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "2", fileName: "Export2", date: "2023-10-02", status: Status.IN_PROGRESS, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "3", fileName: "Export3", date: "2023-10-03", status: Status.FAILED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "1", fileName: "Export1", date: "2023-10-01", status: Status.COMPLETED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "2", fileName: "Export2", date: "2023-10-02", status: Status.IN_PROGRESS, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, { id: "3", fileName: "Export3", date: "2023-10-03", status: Status.FAILED, exportType: ExportType.XLS, exportDataScope: "Header List", src: "", }, ]; */ const DEFAULT_EXPORT_TYPES = Object.values(ExportType) as string[]; const DEFAULT_DATA_SCOPES: ExportDataScope[] = [ { label: "Header List", type: "header", entity: "header" }, ]; const ExportPopup = (props: ExportPopupProps) => { const exportTypes = props.exportTypes?.length ? props.exportTypes : DEFAULT_EXPORT_TYPES; const dataScopes = props.dataScopes?.length ? props.dataScopes : DEFAULT_DATA_SCOPES; const popupRef = useRef(null); const [exporHistoryList, setExporHistoryList] = useState< ExportHistoryViewModel[] >([]); useEffect(() => { props.onExportList((list: ExportHistoryViewModel[]) => { setExporHistoryList(list); }); }, []); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { const target = event.target as HTMLElement; const isInsideMyPopup = target.closest( `.tmpl-prvent-outside-click-close`, ); if ( popupRef.current && !popupRef.current.contains(event.target as Node) && !isInsideMyPopup ) { popupRef.current.classList.add("tmpl-popup-animation-reverse"); setTimeout(() => { props.setTogglePopup ? props.setTogglePopup(false) : null; }, 200); } }; document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); }; }, []); const handleExportClick = ( exportType: string, dataScope: ExportDataScope, ) => { const exportViewModel: ExportViewModel = { exportType, ...dataScope, }; props.onExport(() => { props.onExportList((list: ExportHistoryViewModel[]) => { setExporHistoryList(list); }); }, exportViewModel); }; const downloadResultHandler = (response: any, fileName: string) => { const blob = new Blob([response]); const downloadUrl = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = downloadUrl; link.download = fileName; document.body.appendChild(link); link.click(); window.URL.revokeObjectURL(downloadUrl); document.body.removeChild(link); }; const handleDownload = async (fileUrl: string, fileName: string) => { try { props.onExportDownload( (res) => downloadResultHandler(res, fileName), fileUrl, ); } catch (error) { console.error("Download failed:", error); } }; return (
{dataScopes.map((scope) => (

{scope.label}

{exportTypes.map((type) => (
handleExportClick(type, scope)} > {type}
))}
))}

Previous Exports

{exporHistoryList.map((exportHistoryViewModel) => { return (

{exportHistoryViewModel.fileName}

{exportHistoryViewModel.date}

{exportHistoryViewModel.status === Status.COMPLETED ? ( /*
); })}
); }; export default ExportPopup;