You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

91 lines
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. // If claim searching is done on the server-side, then no need for this.
  19. const [filteredClaims, setFilteredClaims] = useState(claims);
  20. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  21. () => [
  22. { label: t("Creation Date From"), label2: t("Creation Date To"), paramName: "created", type: "dateRange" },
  23. // { label: t("Related Project Name"), paramName: "name", type: "text" },
  24. {
  25. label: t("Expense Type"),
  26. paramName: "type",
  27. type: "select",
  28. options: expenseTypeCombo,
  29. },
  30. {
  31. label: t("Status"),
  32. paramName: "status",
  33. type: "select",
  34. options: claimStatusCombo,
  35. },
  36. ],
  37. [t],
  38. );
  39. const onClaimClick = useCallback((claim: Claim) => {
  40. console.log(claim);
  41. }, []);
  42. const columns = useMemo<Column<Claim>[]>(
  43. () => [
  44. // {
  45. // name: "action",
  46. // label: t("Actions"),
  47. // onClick: onClaimClick,
  48. // buttonIcon: <EditNote />,
  49. // },
  50. { name: "created", label: t("Creation Date"), type: "date" },
  51. { name: "code", label: t("Claim Code") },
  52. // { name: "project", label: t("Related Project Name") },
  53. { name: "amount", label: t("Amount (HKD)") },
  54. { name: "type", label: t("Expense Type"), needTranslation: true },
  55. { name: "status", label: t("Status"), needTranslation: true },
  56. { name: "remarks", label: t("Remarks") },
  57. ],
  58. [t, onClaimClick],
  59. );
  60. return (
  61. <>
  62. <SearchBox
  63. criteria={searchCriteria}
  64. onSearch={(query) => {
  65. setFilteredClaims(
  66. claims.filter(
  67. (claim) =>
  68. dateInRange(convertDateArrayToString(claim.created, "YYYY-MM-DD")!!, query.created, query.createdTo ?? undefined) &&
  69. // claim.project.name.toLowerCase().includes(query.name.toLowerCase()) &&
  70. (claim.type.toLowerCase().includes(query.type.toLowerCase()) || query.type.toLowerCase() === "all") &&
  71. (claim.status.toLowerCase().includes(query.status.toLowerCase()) || query.status.toLowerCase() === "all")
  72. ),
  73. );
  74. }}
  75. />
  76. <SearchResults<Claim> items={filteredClaims} columns={columns} />
  77. </>
  78. );
  79. };
  80. export default ClaimSearch;