Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

120 строки
3.6 KiB

  1. "use client";
  2. import { ProjectCategory, 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. import uniq from "lodash/uniq";
  9. import { useRouter } from "next/navigation";
  10. import { MAINTAIN_PROJECT } from "@/middleware";
  11. interface Props {
  12. projects: ProjectResult[];
  13. projectCategories: ProjectCategory[];
  14. abilities: string[]
  15. }
  16. type SearchQuery = Partial<Omit<ProjectResult, "id">>;
  17. type SearchParamNames = keyof SearchQuery;
  18. const ProjectSearch: React.FC<Props> = ({ projects, projectCategories, abilities }) => {
  19. const router = useRouter();
  20. const { t } = useTranslation("projects");
  21. const [filteredProjects, setFilteredProjects] = useState(projects);
  22. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  23. () => [
  24. { label: t("Project code"), paramName: "code", type: "text" },
  25. { label: t("Project name"), paramName: "name", type: "text" },
  26. {
  27. label: t("Client name"),
  28. paramName: "client",
  29. type: "select",
  30. options: uniq(projects.map((project) => project.client)),
  31. },
  32. {
  33. label: t("Project category"),
  34. paramName: "category",
  35. type: "select",
  36. options: projectCategories.map((category) => category.name),
  37. },
  38. {
  39. label: t("Team"),
  40. paramName: "team",
  41. type: "select",
  42. options: uniq(projects.map((project) => project.team)),
  43. },
  44. {
  45. label: t("Status"),
  46. paramName: "status",
  47. type: "select",
  48. options: uniq(projects.map((project) => project.status)),
  49. },
  50. ],
  51. [t, projectCategories, projects],
  52. );
  53. const onReset = useCallback(() => {
  54. setFilteredProjects(projects);
  55. }, [projects]);
  56. const onProjectClick = useCallback(
  57. (project: ProjectResult) => {
  58. if (Boolean(project.mainProject)) {
  59. router.push(`/projects/editSub?id=${project.id}`);
  60. } else router.push(`/projects/edit?id=${project.id}`);
  61. },
  62. [router],
  63. );
  64. const columns = useMemo<Column<ProjectResult>[]>(
  65. () => [
  66. {
  67. name: "id",
  68. label: t("Details"),
  69. onClick: onProjectClick,
  70. buttonIcon: <EditNote />,
  71. disabled: !abilities.includes(MAINTAIN_PROJECT),
  72. },
  73. { name: "code", label: t("Project Code") },
  74. { name: "name", label: t("Project Name") },
  75. { name: "category", label: t("Project Category") },
  76. { name: "team", label: t("Team") },
  77. { name: "client", label: t("Client") },
  78. { name: "status", label: t("Status") },
  79. ],
  80. [t, onProjectClick],
  81. );
  82. return (
  83. <>
  84. <SearchBox
  85. criteria={searchCriteria}
  86. onSearch={(query) => {
  87. setFilteredProjects(
  88. projects.filter(
  89. (p) =>
  90. p.code.toLowerCase().includes(query.code.toLowerCase()) &&
  91. p.name.toLowerCase().includes(query.name.toLowerCase()) &&
  92. (query.client === "All" || p.client === query.client) &&
  93. (query.category === "All" || p.category === query.category) &&
  94. (query.team === "All" || p.team === query.team) &&
  95. (query.status === "All" || p.status === query.status),
  96. ),
  97. );
  98. }}
  99. onReset={onReset}
  100. />
  101. <SearchResults<ProjectResult>
  102. items={filteredProjects}
  103. columns={columns}
  104. />
  105. </>
  106. );
  107. };
  108. export default ProjectSearch;