Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

87 righe
2.6 KiB

  1. "use client";
  2. import React, { useMemo } from "react";
  3. import SearchBox, { Criterion } from "../SearchBox";
  4. import { useTranslation } from "react-i18next";
  5. import { ProjectResult } from "@/app/api/projects";
  6. import {
  7. fetchLastModifiedReport,
  8. } from "@/app/api/reports/actions";
  9. import { downloadFile } from "@/app/utils/commonUtil";
  10. import { LastModifiedReportFilter } from "@/app/api/reports";
  11. import dayjs from "dayjs";
  12. import { getPublicHolidaysForNYears } from "@/app/utils/holidayUtils";
  13. interface Props {
  14. projects: ProjectResult[];
  15. companyHolidays: String[];
  16. }
  17. type SearchQuery = Partial<Omit<LastModifiedReportFilter, "id">>;
  18. type SearchParamNames = keyof SearchQuery;
  19. const GenerateLastModifiedReport: React.FC<Props> = ({ projects, companyHolidays }) => {
  20. const { t } = useTranslation("report");
  21. const projectCombo = projects.map((project) => ({label: `${project.code} - ${project.name}`, value: project.id}))
  22. console.log(companyHolidays)
  23. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  24. () => [
  25. {
  26. label: t("Date"),
  27. paramName: "date",
  28. type: "date",
  29. },
  30. {
  31. label: t("Range"),
  32. paramName: "dayRange",
  33. type: "autocomplete",
  34. options: [
  35. {label: t("30 Days"), value: 30},
  36. {label: t("60 Days"), value: 60},
  37. {label: t("90 Days"), value: 90},
  38. ],
  39. needAll: false
  40. }
  41. ],
  42. [t]
  43. );
  44. return (
  45. <>
  46. <SearchBox
  47. formType={"download"}
  48. criteria={searchCriteria}
  49. onSearch={async (query: any) => {
  50. console.log(query);
  51. const yearNeeded = parseInt(dayjs(query.date).format("YYYY"))
  52. const holidayList: String[] = [...getPublicHolidaysForNYears(1, yearNeeded).map((item) => dayjs(item.date).format("DD/MM/YYYY")), ...companyHolidays]
  53. const uniqueHoliday = holidayList.filter((value, index, arr) => index === arr.indexOf(value));
  54. let postData = {
  55. dateString: dayjs().format("YYYY-MM-DD").toString(),
  56. holidays: uniqueHoliday,
  57. dayRange: query.dayRange
  58. };
  59. console.log(query.date.length > 0)
  60. if (query.date.length > 0) {
  61. postData.dateString = query.date
  62. }
  63. console.log(postData)
  64. const response = await fetchLastModifiedReport(postData);
  65. if (response) {
  66. downloadFile(
  67. new Uint8Array(response.blobValue),
  68. response.filename!!
  69. );
  70. }
  71. }}
  72. />
  73. </>
  74. );
  75. };
  76. export default GenerateLastModifiedReport;