FPSMS-frontend
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.
 
 

101 lines
2.8 KiB

  1. "use client";
  2. import { ProjectResult } from "@/app/api/projects";
  3. import React, { useCallback, useMemo, useState } from "react";
  4. import SearchBox, { Criterion } from "../SearchBox";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults";
  7. import EditNote from "@mui/icons-material/EditNote";
  8. interface Props {
  9. projects: ProjectResult[];
  10. }
  11. type SearchQuery = Partial<Omit<ProjectResult, "id">>;
  12. type SearchParamNames = keyof SearchQuery;
  13. const ProjectSearch: React.FC<Props> = ({ projects }) => {
  14. const { t } = useTranslation("projects");
  15. // If project searching is done on the server-side, then no need for this.
  16. const [filteredProjects, setFilteredProjects] = useState(projects);
  17. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  18. () => [
  19. { label: t("Project code"), paramName: "code", type: "text" },
  20. { label: t("Project name"), paramName: "name", type: "text" },
  21. {
  22. label: t("Client name"),
  23. paramName: "client",
  24. type: "select",
  25. options: ["Client A", "Client B", "Client C"],
  26. },
  27. {
  28. label: t("Project category"),
  29. paramName: "category",
  30. type: "select",
  31. options: ["Confirmed Project", "Project to be bidded"],
  32. },
  33. {
  34. label: t("Team"),
  35. paramName: "team",
  36. type: "select",
  37. options: ["TW", "WY"],
  38. },
  39. ],
  40. [t],
  41. );
  42. const onReset = useCallback(() => {
  43. setFilteredProjects(projects);
  44. }, [projects]);
  45. const onProjectClick = useCallback((project: ProjectResult) => {
  46. console.log(project);
  47. }, []);
  48. const columns = useMemo<Column<ProjectResult>[]>(
  49. () => [
  50. {
  51. name: "id",
  52. label: t("Details"),
  53. onClick: onProjectClick,
  54. buttonIcon: <EditNote />,
  55. },
  56. { name: "code", label: t("Project Code") },
  57. { name: "name", label: t("Project Name") },
  58. { name: "category", label: t("Project Category") },
  59. { name: "team", label: t("Team") },
  60. { name: "client", label: t("Client") },
  61. ],
  62. [t, onProjectClick],
  63. );
  64. return (
  65. <>
  66. <SearchBox
  67. criteria={searchCriteria}
  68. onSearch={(query) => {
  69. setFilteredProjects(
  70. projects.filter(
  71. (p) =>
  72. p.code.toLowerCase().includes(query.code.toLowerCase()) &&
  73. p.name.toLowerCase().includes(query.name.toLowerCase()) &&
  74. (query.client === "All" || p.client === query.client) &&
  75. (query.category === "All" || p.category === query.category) &&
  76. (query.team === "All" || p.team === query.team),
  77. ),
  78. );
  79. }}
  80. onReset={onReset}
  81. />
  82. <SearchResults<ProjectResult>
  83. items={filteredProjects}
  84. columns={columns}
  85. />
  86. </>
  87. );
  88. };
  89. export default ProjectSearch;