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.
 
 

203 rivejä
6.3 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 SearchResults, { Column } from "../SearchResults";
  6. import { EditNote } from "@mui/icons-material";
  7. import { useRouter, useSearchParams } from "next/navigation";
  8. import { GridDeleteIcon } from "@mui/x-data-grid";
  9. import { TypeEnum } from "@/app/utils/typeEnum";
  10. import axios from "axios";
  11. import {BASE_API_URL, NEXT_PUBLIC_API_URL} from "@/config/api";
  12. import { useTranslation } from "react-i18next";
  13. import axiosInstance from "@/app/(main)/axios/axiosInstance";
  14. import Qs from 'qs';
  15. import EditableSearchResults from "@/components/SearchResults/EditableSearchResults"; // Make sure to import Qs
  16. // may need move to "index" or "actions"
  17. type RecordStructure = {
  18. id: number,
  19. scheduledPeriod: string,
  20. scheduledAt: string,
  21. productCount: number,
  22. };
  23. type Props = {
  24. records: RecordStructure[];
  25. };
  26. type SearchQuery = Partial<Omit<RecordStructure, "id">>;
  27. type SearchParamNames = keyof SearchQuery;
  28. const DSOverview: React.FC<Props> = ({ records }) => {
  29. const [filteredItems, setFilteredItems] = useState<RecordStructure[]>(records ?? []);
  30. const { t } = useTranslation("items");
  31. const router = useRouter();
  32. const [filterObj, setFilterObj] = useState({});
  33. const [tempSelectedValue, setTempSelectedValue] = useState({});
  34. const [pagingController, setPagingController] = useState({
  35. pageNum: 1,
  36. pageSize: 10,
  37. totalCount: 0,
  38. })
  39. const [mode, redirPath] = useMemo(() => {
  40. // var typeId = TypeEnum.CONSUMABLE_ID
  41. let title = "";
  42. const mode = "Search";
  43. let redirPath = "";
  44. title = "Product";
  45. redirPath = "/scheduling/detail";
  46. return [mode, redirPath];
  47. }, []);
  48. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  49. () =>
  50. [
  51. { label: t("Schedule Period"), paramName: "scheduledPeriod", type: "dateRange" },
  52. { label: t("Scheduled At"), paramName: "scheduledAt", type: "dateRange" },
  53. { label: t("Product Count"), paramName: "productCount", type: "text" },
  54. ]
  55. ,
  56. [t, records]
  57. );
  58. // const onDetailClick = useCallback(
  59. // (item: ItemsResult) => {
  60. // router.push(`/settings/items/edit?id=${item.id}`);
  61. // },
  62. // [router]
  63. // );
  64. const onDeleteClick = useCallback(
  65. (item: ItemsResult) => {},
  66. [router]
  67. );
  68. const onDetailClick = (record: any) => {
  69. console.log("[debug] record", record);
  70. router.push(`/scheduling/detail/edit?id=${record.id}`);
  71. }
  72. const columns = useMemo<Column<RecordStructure>[]>(
  73. () => [
  74. {
  75. name: "id",
  76. label: t("Details"),
  77. onClick: (record)=>onDetailClick(record),
  78. buttonIcon: <EditNote />,
  79. },
  80. {
  81. name: "scheduledPeriod",
  82. label: "Demand Forecast Period",
  83. },
  84. {
  85. name: "scheduledAt",
  86. label: t("Scheduled At"),
  87. },
  88. {
  89. name: "productCount",
  90. label: t("Product Count(s)"),
  91. },
  92. // {
  93. // name: "action",
  94. // label: t(""),
  95. // buttonIcon: <GridDeleteIcon />,
  96. // onClick: onDeleteClick,
  97. // },
  98. ],
  99. [filteredItems]
  100. );
  101. useEffect(() => {
  102. refetchData(filterObj);
  103. }, [filterObj, pagingController.pageNum, pagingController.pageSize]);
  104. const refetchData = async (filterObj: SearchQuery | null) => {
  105. const authHeader = axiosInstance.defaults.headers['Authorization'];
  106. if (!authHeader) {
  107. return; // Exit the function if the token is not set
  108. }
  109. const params ={
  110. pageNum: pagingController.pageNum,
  111. pageSize: pagingController.pageSize,
  112. ...filterObj,
  113. ...tempSelectedValue,
  114. }
  115. try {
  116. const response = await axiosInstance.get<ItemsResult[]>(`${NEXT_PUBLIC_API_URL}/items/getRecordByPage`, {
  117. params,
  118. paramsSerializer: (params) => {
  119. return Qs.stringify(params, { arrayFormat: 'repeat' });
  120. },
  121. });
  122. //setFilteredItems(response.data.records);
  123. setFilteredItems([
  124. {
  125. id: 1,
  126. scheduledPeriod: "2025-05-11 to 2025-05-17",
  127. scheduledAt: "2025-05-07",
  128. productCount: 13,
  129. },
  130. {
  131. id: 2,
  132. scheduledPeriod: "2025-05-18 to 2025-05-24",
  133. scheduledAt: "2025-05-14",
  134. productCount: 15,
  135. },
  136. {
  137. id: 3,
  138. scheduledPeriod: "2025-05-25 to 2025-05-31",
  139. scheduledAt: "2025-05-21",
  140. productCount: 13,
  141. },
  142. ])
  143. setPagingController({
  144. ...pagingController,
  145. totalCount: response.data.total
  146. })
  147. return response; // Return the data from the response
  148. } catch (error) {
  149. console.error('Error fetching items:', error);
  150. throw error; // Rethrow the error for further handling
  151. }
  152. };
  153. const onReset = useCallback(() => {
  154. //setFilteredItems(items ?? []);
  155. setFilterObj({});
  156. setTempSelectedValue({});
  157. refetchData(null);
  158. }, [records]);
  159. return (
  160. <>
  161. <SearchBox
  162. criteria={searchCriteria}
  163. onSearch={(query) => {
  164. setFilterObj({
  165. ...query
  166. })
  167. }}
  168. onReset={onReset}
  169. />
  170. <SearchResults<RecordStructure>
  171. items={filteredItems}
  172. columns={columns}
  173. setPagingController={setPagingController}
  174. pagingController={pagingController}
  175. isAutoPaging={false}
  176. // hasCollapse={false}
  177. />
  178. </>
  179. );
  180. };
  181. export default DSOverview;