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.
 
 

98 rivejä
3.4 KiB

  1. "use client";
  2. import { ProjectResult } from "@/app/api/projects";
  3. import React, { useMemo, useState, useCallback, useEffect } from "react";
  4. import SearchBox, { Criterion } from "../SearchBox";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults";
  7. import { ClientProjectResult} from "@/app/api/clientprojects";
  8. import { fetchAllClientSubsidiaryProjects} from "@/app/api/clientprojects/actions";
  9. import VisibilityIcon from '@mui/icons-material/Visibility';
  10. import { useRouter, useSearchParams } from "next/navigation";
  11. import ProgressByClient from "@/components/ProgressByClient";
  12. interface Props {
  13. clientProjects: ClientProjectResult[];
  14. }
  15. type SearchQuery = Partial<Omit<ClientProjectResult, "id">>;
  16. type SearchParamNames = keyof SearchQuery;
  17. const ProgressByClientSearch: React.FC<Props> = ({ clientProjects }) => {
  18. const router = useRouter();
  19. const { t } = useTranslation("projects");
  20. const searchParams = useSearchParams()
  21. // If project searching is done on the server-side, then no need for this.
  22. const [filteredProjects, setFilteredProjects] = useState(clientProjects);
  23. const [clientSubsidiaryProjectResult, setClientSubsidiaryProjectResult]:any[] = useState([]);
  24. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  25. () => [
  26. { label: "Client Code", paramName: "customerCode", type: "text" },
  27. { label: "Client Name", paramName: "customerName", type: "text" },
  28. ],
  29. [t],
  30. );
  31. // const onTaskClick = useCallback((clientProjectResult: ClientProjectResult) => {
  32. // const clickResult = fetchAllClientSubsidiaryProjects(clientProjectResult.customerId, clientProjectResult.subsidiaryId)
  33. // console.log(clickResult)
  34. // setClientSubsidiaryProjectResult(clickResult)
  35. // }, []);
  36. const onTaskClick = useCallback(async (clientProjectResult: ClientProjectResult) => {
  37. try {
  38. router.push(
  39. `/dashboard/ProjectStatusByClient?customerId=${clientProjectResult.customerId}&subsidiaryId=${clientProjectResult.subsidiaryId}`
  40. );
  41. } catch (error) {
  42. console.error('Error fetching client subsidiary projects:', error);
  43. }
  44. }, []);
  45. const columns = useMemo<Column<ClientProjectResult>[]>(
  46. () => [
  47. {
  48. name: "id",
  49. label: t("Details"),
  50. onClick: onTaskClick,
  51. buttonIcon: <VisibilityIcon />,
  52. },
  53. { name: "customerCode", label: t("Client Code") },
  54. { name: "customerName", label: t("Client Name") },
  55. { name: "subsidiaryCode", label: t("Subsidiary Code") },
  56. { name: "subsidiaryName", label: t("Subisdiary") },
  57. { name: "projectNo", label: t("No. of Projects") },
  58. ],
  59. [onTaskClick, t],
  60. // [t],
  61. );
  62. return (
  63. <>
  64. <SearchBox
  65. criteria={searchCriteria}
  66. onSearch={(query) => {
  67. setFilteredProjects(
  68. clientProjects.filter(
  69. (cp) =>
  70. cp.customerCode.toLowerCase().includes(query.customerCode.toLowerCase()) &&
  71. cp.customerName.toLowerCase().includes(query.customerName.toLowerCase())
  72. ),
  73. );
  74. }}
  75. />
  76. <SearchResults<ClientProjectResult>
  77. items={filteredProjects}
  78. columns={columns}
  79. />
  80. {/* {clientSubsidiaryProjectResult.length > 0 && (
  81. <ProgressByClient clientSubsidiaryProjectResult={clientSubsidiaryProjectResult} />
  82. )} */}
  83. </>
  84. );
  85. };
  86. export default ProgressByClientSearch;