FPSMS-frontend
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

148 wiersze
4.3 KiB

  1. "use client";
  2. import { useCallback, useEffect, useMemo, useState } from "react";
  3. import SearchBox, { Criterion } from "../SearchBox";
  4. import { ItemsResult, ItemsResultResponse } from "@/app/api/settings/item";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults";
  7. import { EditNote } from "@mui/icons-material";
  8. import { useRouter, useSearchParams } from "next/navigation";
  9. import { GridDeleteIcon } from "@mui/x-data-grid";
  10. import { TypeEnum } from "@/app/utils/typeEnum";
  11. import axios from "axios";
  12. import { BASE_API_URL, NEXT_PUBLIC_API_URL } from "@/config/api";
  13. import axiosInstance from "@/app/(main)/axios/axiosInstance";
  14. type Props = {
  15. items: ItemsResult[];
  16. };
  17. type SearchQuery = Partial<Omit<ItemsResult, "id">>;
  18. type SearchParamNames = keyof SearchQuery;
  19. const ItemsSearch: React.FC<Props> = ({ items }) => {
  20. const [filteredItems, setFilteredItems] = useState<ItemsResult[]>(items);
  21. const { t } = useTranslation("items");
  22. const router = useRouter();
  23. const [filterObj, setFilterObj] = useState({});
  24. const [pagingController, setPagingController] = useState({
  25. pageNum: 1,
  26. pageSize: 10,
  27. // totalCount: 0,
  28. });
  29. const [totalCount, setTotalCount] = useState(0)
  30. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(() => {
  31. var searchCriteria: Criterion<SearchParamNames>[] = [
  32. { label: t("Code"), paramName: "code", type: "text" },
  33. { label: t("Name"), paramName: "name", type: "text" },
  34. ];
  35. return searchCriteria;
  36. }, [t, items]);
  37. const onDetailClick = useCallback(
  38. (item: ItemsResult) => {
  39. router.push(`/settings/items/edit?id=${item.id}`);
  40. },
  41. [router]
  42. );
  43. const onDeleteClick = useCallback((item: ItemsResult) => {}, [router]);
  44. const columns = useMemo<Column<ItemsResult>[]>(
  45. () => [
  46. {
  47. name: "id",
  48. label: t("Details"),
  49. onClick: onDetailClick,
  50. buttonIcon: <EditNote />,
  51. },
  52. {
  53. name: "code",
  54. label: t("Code"),
  55. },
  56. {
  57. name: "name",
  58. label: t("Name"),
  59. },
  60. {
  61. name: "action",
  62. label: t(""),
  63. buttonIcon: <GridDeleteIcon />,
  64. onClick: onDeleteClick,
  65. },
  66. ],
  67. [filteredItems]
  68. );
  69. const refetchData = useCallback(
  70. async (filterObj: SearchQuery) => {
  71. const authHeader = axiosInstance.defaults.headers["Authorization"];
  72. if (!authHeader) {
  73. return; // Exit the function if the token is not set
  74. }
  75. const params = {
  76. pageNum: pagingController.pageNum,
  77. pageSize: pagingController.pageSize,
  78. ...filterObj,
  79. };
  80. try {
  81. const response = await axiosInstance.get<ItemsResultResponse>(
  82. `${NEXT_PUBLIC_API_URL}/items/getRecordByPage`,
  83. { params }
  84. );
  85. console.log(response);
  86. if (response.status == 200) {
  87. setFilteredItems(response.data.records);
  88. setTotalCount(response.data.total)
  89. return response; // Return the data from the response
  90. } else {
  91. throw "400";
  92. }
  93. } catch (error) {
  94. console.error("Error fetching items:", error);
  95. throw error; // Rethrow the error for further handling
  96. }
  97. },
  98. [axiosInstance, pagingController.pageNum, pagingController.pageSize]
  99. );
  100. useEffect(() => {
  101. refetchData(filterObj);
  102. }, [filterObj, pagingController.pageNum, pagingController.pageSize]);
  103. const onReset = useCallback(() => {
  104. setFilteredItems(items);
  105. }, [items]);
  106. return (
  107. <>
  108. <SearchBox
  109. criteria={searchCriteria}
  110. onSearch={(query) => {
  111. // setFilteredItems(
  112. // items.filter((pm) => {
  113. // return (
  114. // pm.code.toLowerCase().includes(query.code.toLowerCase()) &&
  115. // pm.name.toLowerCase().includes(query.name.toLowerCase())
  116. // );
  117. // })
  118. // );
  119. setFilterObj({
  120. ...query,
  121. });
  122. }}
  123. onReset={onReset}
  124. />
  125. <SearchResults<ItemsResult>
  126. items={filteredItems}
  127. columns={columns}
  128. setPagingController={setPagingController}
  129. pagingController={pagingController}
  130. totalCount={totalCount}
  131. isAutoPaging={false}
  132. />
  133. </>
  134. );
  135. };
  136. export default ItemsSearch;