FPSMS-frontend
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

205 行
6.1 KiB

  1. "use client";
  2. import React, { useCallback, useEffect, useMemo, useState } from "react";
  3. import SearchBox, { Criterion } from "../SearchBox";
  4. import { ItemsResult } from "@/app/api/settings/item";
  5. import { EditNote } from "@mui/icons-material";
  6. import { useRouter, useSearchParams } from "next/navigation";
  7. import { GridDeleteIcon } from "@mui/x-data-grid";
  8. import { TypeEnum } from "@/app/utils/typeEnum";
  9. import axios from "axios";
  10. import { BASE_API_URL, NEXT_PUBLIC_API_URL } from "@/config/api";
  11. import { useTranslation } from "react-i18next";
  12. import axiosInstance from "@/app/(main)/axios/axiosInstance";
  13. import Qs from "qs";
  14. import EditableSearchResults, { Column } from "@/components/SearchResults/EditableSearchResults"; // Make sure to import Qs
  15. import { ItemsResultResponse } from "@/app/api/settings/item";
  16. type Props = {
  17. items: ItemsResult[];
  18. };
  19. type SearchQuery = Partial<Omit<ItemsResult, "id">>;
  20. type SearchParamNames = keyof SearchQuery;
  21. const RSSOverview: React.FC<Props> = ({ items }) => {
  22. const [filteredItems, setFilteredItems] = useState<ItemsResult[]>(
  23. items ?? [],
  24. );
  25. const { t } = useTranslation();
  26. const router = useRouter();
  27. const [filterObj, setFilterObj] = useState({});
  28. const [tempSelectedValue, setTempSelectedValue] = useState({});
  29. const [pagingController, setPagingController] = useState({
  30. pageNum: 1,
  31. pageSize: 10,
  32. totalCount: 0,
  33. });
  34. const [mode, redirPath] = useMemo(() => {
  35. // var typeId = TypeEnum.CONSUMABLE_ID
  36. let title = "";
  37. const mode = "Search";
  38. let redirPath = "";
  39. title = "Product";
  40. redirPath = "/settings/rss";
  41. return [mode, redirPath];
  42. }, []);
  43. const handleSelectionChange = (selectedValues: string[]) => {
  44. setTempSelectedValue({
  45. ...tempSelectedValue,
  46. excludeDate: selectedValues,
  47. });
  48. };
  49. const dayOptions = [
  50. { label: t("Mon"), value: 1 },
  51. { label: t("Tue"), value: 2 },
  52. { label: t("Wed"), value: 3 },
  53. { label: t("Thu"), value: 4 },
  54. { label: t("Fri"), value: 5 },
  55. { label: t("Sat"), value: 6 },
  56. { label: t("Sun"), value: 7 },
  57. ];
  58. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(() => {
  59. const searchCriteria: Criterion<SearchParamNames>[] = [
  60. { label: t("Finished Goods Name"), paramName: "name", type: "text" },
  61. {
  62. label: t("Exclude Date"),
  63. paramName: "shelfLife",
  64. type: "select",
  65. options: ["qcChecks"],
  66. // selectedValues: filterObj,
  67. handleSelectionChange: handleSelectionChange,
  68. },
  69. ];
  70. return searchCriteria;
  71. }, [t, items]);
  72. // const onDetailClick = useCallback(
  73. // (item: ItemsResult) => {
  74. // router.push(`/settings/items/edit?id=${item.id}`);
  75. // },
  76. // [router]
  77. // );
  78. const handleEditClick = useCallback((item: ItemsResult) => {}, [router]);
  79. const onDeleteClick = useCallback((item: ItemsResult) => {}, [router]);
  80. const columns = useMemo<Column<ItemsResult>[]>(
  81. () => [
  82. // {
  83. // name: "id",
  84. // label: t("Details"),
  85. // onClick: ()=>{},
  86. // buttonIcon: <EditNote />,
  87. // },
  88. // {
  89. // name: "name",
  90. // label: "Finished Goods Name",
  91. // // type: "input",
  92. // },
  93. // {
  94. // name: "excludeDate",
  95. // label: t("Exclude Date"),
  96. // type: "multi-select",
  97. // options: dayOptions,
  98. // renderCell: (item: ItemsResult) => {
  99. // console.log("[debug] render item", item);
  100. // const selectedValues = (item.excludeDate as number[]) ?? []; // Assuming excludeDate is an array of numbers
  101. // const selectedLabels = dayOptions
  102. // .filter((option) => selectedValues.includes(option.value))
  103. // .map((option) => option.label)
  104. // .join(", "); // Join labels into a string
  105. // return (
  106. // <span onDoubleClick={() => handleEditClick(item.id as number)}>
  107. // {selectedLabels || "None"}{" "}
  108. // {/* Display "None" if no days are selected */}
  109. // </span>
  110. // );
  111. // },
  112. // {
  113. // name: "action",
  114. // label: t(""),
  115. // buttonIcon: <GridDeleteIcon />,
  116. // onClick: onDeleteClick,
  117. // },
  118. ],
  119. [filteredItems],
  120. );
  121. useEffect(() => {
  122. refetchData(filterObj);
  123. }, [filterObj, pagingController.pageNum, pagingController.pageSize]);
  124. const refetchData = async (filterObj: SearchQuery) => {
  125. const authHeader = axiosInstance.defaults.headers["Authorization"];
  126. if (!authHeader) {
  127. return; // Exit the function if the token is not set
  128. }
  129. const params = {
  130. pageNum: pagingController.pageNum,
  131. pageSize: pagingController.pageSize,
  132. ...filterObj,
  133. ...tempSelectedValue,
  134. };
  135. try {
  136. const response = await axiosInstance.get<ItemsResultResponse>(
  137. `${NEXT_PUBLIC_API_URL}/items/getRecordByPage`,
  138. {
  139. params,
  140. paramsSerializer: (params) => {
  141. return Qs.stringify(params, { arrayFormat: "repeat" });
  142. },
  143. },
  144. );
  145. setFilteredItems(response.data.records);
  146. setPagingController({
  147. ...pagingController,
  148. totalCount: response.data.total,
  149. });
  150. return response; // Return the data from the response
  151. } catch (error) {
  152. console.error("Error fetching items:", error);
  153. throw error; // Rethrow the error for further handling
  154. }
  155. };
  156. const onReset = useCallback(() => {
  157. //setFilteredItems(items ?? []);
  158. setFilterObj({});
  159. setTempSelectedValue({});
  160. refetchData(filterObj);
  161. }, [items, filterObj]);
  162. return (
  163. <>
  164. <SearchBox
  165. criteria={searchCriteria}
  166. onSearch={(query) => {
  167. setFilterObj({
  168. ...query,
  169. });
  170. }}
  171. onReset={onReset}
  172. />
  173. <EditableSearchResults<ItemsResult>
  174. index={1} // bug
  175. isEdit
  176. isEditable
  177. items={filteredItems}
  178. columns={columns}
  179. setPagingController={setPagingController}
  180. pagingController={pagingController}
  181. isAutoPaging={false}
  182. hasCollapse={false}
  183. />
  184. </>
  185. );
  186. };
  187. export default RSSOverview;