"use client"; import React, { useMemo } from "react"; import SearchBox, { Criterion } from "../SearchBox"; import { useTranslation } from "react-i18next"; import { ProjectResult } from "@/app/api/projects"; import { ProjectPandLReportFilter } from "@/app/api/reports"; import { fetchProjectPandLReport } from "@/app/api/reports/actions"; import { downloadFile } from "@/app/utils/commonUtil"; import { dateTypeCombo } from "@/app/utils/comboUtil"; import { FormHelperText } from "@mui/material"; import { errorDialog, errorDialogWithContent } from "../Swal/CustomAlerts"; interface Props { projects: ProjectResult[]; } type SearchQuery = Partial>; type SearchParamNames = keyof SearchQuery; const GenerateProjectPandLReport: React.FC = ({ projects }) => { const { t } = useTranslation("report"); const projectCombo = projects.map(project => `${project.code} - ${project.name}`) const searchCriteria: Criterion[] = useMemo( () => [ { label: t("Project *"), paramName: "project", type: "select", options: projectCombo, needAll: false}, { label: t("Start Month *"), label2: t("End Month *"), paramName: "startMonth", type: "dateRange", needMonth: true }, ], [t], ); return ( <> { if (query.project.length > 0 && query.project.toLocaleLowerCase() !== "all") { const projectIndex = projectCombo.findIndex(project => project === query.project) console.log(projects[projectIndex].id, query.startMonth, query.startMonthTo) if(projects[projectIndex].id != null && query.startMonth != "" && query.startMonthTo != undefined){ const response = await fetchProjectPandLReport({ projectId: projects[projectIndex].id, startMonth: query.startMonth, endMonth: query.startMonthTo }) if (response) { downloadFile(new Uint8Array(response.blobValue), response.filename!!) } }else{ errorDialogWithContent(t("Download Fail"), t(`Please check the required field`), t) .then(() => { window.location.reload() }) } } }} formType={"download"} /> ); }; export default GenerateProjectPandLReport;