您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

92 行
3.0 KiB

  1. "use client";
  2. import { Claim, ClaimSearchForm } from "@/app/api/claims";
  3. import React, { useCallback, useMemo, useState } from "react";
  4. import SearchBox, { Criterion } from "../SearchBox/index";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults/index";
  7. import EditNote from "@mui/icons-material/EditNote";
  8. import { dateInRange } from "@/app/utils/commonUtil";
  9. import { claimStatusCombo, expenseTypeCombo } from "@/app/utils/comboUtil";
  10. import { convertDateArrayToString, convertDateToString } from "@/app/utils/formatUtil";
  11. interface Props {
  12. claims: Claim[];
  13. }
  14. type SearchQuery = Partial<Omit<ClaimSearchForm, "id">>;
  15. type SearchParamNames = keyof SearchQuery;
  16. const ClaimSearch: React.FC<Props> = ({ claims }) => {
  17. const { t } = useTranslation();
  18. console.log(claims)
  19. // If claim searching is done on the server-side, then no need for this.
  20. const [filteredClaims, setFilteredClaims] = useState(claims);
  21. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  22. () => [
  23. { label: t("Creation Date From"), label2: t("Creation Date To"), paramName: "created", type: "dateRange" },
  24. // { label: t("Related Project Name"), paramName: "name", type: "text" },
  25. {
  26. label: t("Expense Type"),
  27. paramName: "type",
  28. type: "select",
  29. options: expenseTypeCombo,
  30. },
  31. {
  32. label: t("Status"),
  33. paramName: "status",
  34. type: "select",
  35. options: claimStatusCombo,
  36. },
  37. ],
  38. [t],
  39. );
  40. const onClaimClick = useCallback((claim: Claim) => {
  41. console.log(claim);
  42. }, []);
  43. const columns = useMemo<Column<Claim>[]>(
  44. () => [
  45. // {
  46. // name: "action",
  47. // label: t("Actions"),
  48. // onClick: onClaimClick,
  49. // buttonIcon: <EditNote />,
  50. // },
  51. { name: "created", label: t("Creation Date"), type: "date" },
  52. { name: "code", label: t("Claim Code") },
  53. // { name: "project", label: t("Related Project Name") },
  54. { name: "amount", label: t("Amount"), type: "money" },
  55. { name: "type", label: t("Expense Type"), needTranslation: true },
  56. { name: "status", label: t("Status"), needTranslation: true },
  57. { name: "remarks", label: t("Remarks") },
  58. ],
  59. [t, onClaimClick],
  60. );
  61. return (
  62. <>
  63. <SearchBox
  64. criteria={searchCriteria}
  65. onSearch={(query) => {
  66. setFilteredClaims(
  67. claims.filter(
  68. (claim) =>
  69. dateInRange(convertDateArrayToString(claim.created, "YYYY-MM-DD")!!, query.created, query.createdTo ?? undefined) &&
  70. // claim.project.name.toLowerCase().includes(query.name.toLowerCase()) &&
  71. (claim.type.toLowerCase().includes(query.type.toLowerCase()) || query.type.toLowerCase() === "all") &&
  72. (claim.status.toLowerCase().includes(query.status.toLowerCase()) || query.status.toLowerCase() === "all")
  73. ),
  74. );
  75. }}
  76. />
  77. <SearchResults<Claim> items={filteredClaims} columns={columns} />
  78. </>
  79. );
  80. };
  81. export default ClaimSearch;