FPSMS-frontend
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

249 rindas
9.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. import { ProdScheduleResult, ProdScheduleResultByPage, SearchProdSchedule, fetchProdSchedules } from "@/app/api/scheduling/actions";
  17. import { arrayToDateString, decimalFormatter } from "@/app/utils/formatUtil";
  18. import { isEqual, uniqBy } from "lodash";
  19. import dayjs from "dayjs";
  20. import { defaultPagingController } from "../SearchResults/SearchResults";
  21. // type RecordStructure ={
  22. // id: number,
  23. // schedulePeriod: string,
  24. // scheduleAt: string
  25. // };
  26. type Props = {
  27. type: SearchProdSchedule["type"];
  28. // initProdSchedules: ProdScheduleResultByPage;
  29. defaultInputs: SearchProdSchedule;
  30. };
  31. type SearchQuery = Partial<Omit<SearchProdSchedule, "id" | "type" | "pageSize" | "pageNum">>;
  32. type SearchParamNames = keyof SearchQuery;
  33. const RSOverview: React.FC<Props> = ({ type, defaultInputs }) => {
  34. const [filteredSchedules, setFilteredSchedules] = useState<ProdScheduleResult[]>([]);
  35. const { t } = useTranslation("scheduling");
  36. const router = useRouter();
  37. // const [filterObj, setFilterObj] = useState({});
  38. // const [tempSelectedValue, setTempSelectedValue] = useState({});
  39. const [pagingController, setPagingController] = useState(defaultPagingController)
  40. const [totalCount, setTotalCount] = useState(0)
  41. const [inputs, setInputs] = useState(defaultInputs)
  42. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  43. () => {
  44. var searchCriteria: Criterion<SearchParamNames>[] = [
  45. { label: t("Schedule Period"), label2: t("Schedule Period To"), paramName: "schedulePeriod", type: "dateRange" },
  46. { label: t("Scheduled At"), paramName: "scheduleAt", type: "date" },
  47. { label: t("Product Count"), paramName: "totalEstProdCount", type: "text" },
  48. ]
  49. return searchCriteria
  50. },
  51. [t]
  52. );
  53. // const onDetailClick = useCallback(
  54. // (item: ItemsResult) => {
  55. // router.push(`/settings/items/edit?id=${item.id}`);
  56. // },
  57. // [router]
  58. // );
  59. const onDeleteClick = useCallback(
  60. (item: ItemsResult) => { },
  61. [router]
  62. );
  63. const onDetailClick = (record: any) => {
  64. console.log("[debug] record", record);
  65. router.push(`/scheduling/rough/edit?id=${record.id}`);
  66. }
  67. const columns = useMemo<Column<ProdScheduleResult>[]>(
  68. () => [
  69. {
  70. name: "id",
  71. label: t("Details"),
  72. onClick: (record) => onDetailClick(record),
  73. buttonIcon: <EditNote />,
  74. },
  75. {
  76. name: "schedulePeriod",
  77. label: t("Demand Forecast Period"),
  78. renderCell: (params) => {
  79. return `${arrayToDateString(params.schedulePeriod)} - ${arrayToDateString(params.schedulePeriodTo)}`
  80. }
  81. },
  82. {
  83. name: "scheduleAt",
  84. label: t("Schedule At"),
  85. renderCell: (params) => {
  86. return arrayToDateString(params.scheduleAt)
  87. }
  88. },
  89. {
  90. name: "totalEstProdCount",
  91. label: t("Product Count(s)"),
  92. headerAlign: "right",
  93. align: "right",
  94. renderCell: (params) => {
  95. return decimalFormatter.format(params.totalEstProdCount)
  96. }
  97. },
  98. // {
  99. // name: "action",
  100. // label: t(""),
  101. // buttonIcon: <GridDeleteIcon />,
  102. // onClick: onDeleteClick,
  103. // },
  104. ],
  105. [filteredSchedules]
  106. );
  107. // useEffect(() => {
  108. // refetchData(filterObj);
  109. // }, [filterObj, pagingController.pageNum, pagingController.pageSize]);
  110. const refetchData = useCallback(async (query: Record<SearchParamNames, string> | SearchProdSchedule, actionType: "reset" | "search" | "paging") => {
  111. // console.log(query)
  112. const params: SearchProdSchedule = {
  113. scheduleAt: dayjs(query?.scheduleAt).isValid() ? query?.scheduleAt : undefined,
  114. schedulePeriod: dayjs(query?.schedulePeriod).isValid() ? query?.schedulePeriod : undefined,
  115. schedulePeriodTo: dayjs(query?.schedulePeriodTo).isValid() ? query?.schedulePeriodTo : undefined,
  116. totalEstProdCount: query?.totalEstProdCount ? Number(query?.totalEstProdCount) : undefined,
  117. type: "rough",
  118. pageNum: pagingController.pageNum - 1,
  119. pageSize: pagingController.pageSize
  120. }
  121. const response = await fetchProdSchedules(params)
  122. // console.log(response)
  123. if (response) {
  124. setTotalCount(response.total)
  125. switch (actionType) {
  126. case "reset":
  127. case "search":
  128. setFilteredSchedules(() => response.records)
  129. break;
  130. case "paging":
  131. setFilteredSchedules((fs) => uniqBy([...fs, ...response.records], "id"))
  132. break;
  133. }
  134. }
  135. }, [pagingController, setPagingController])
  136. useEffect(() => {
  137. refetchData(inputs, "paging")
  138. }, [pagingController])
  139. // const refetchData = async (filterObj: SearchQuery) => {
  140. // const authHeader = axiosInstance.defaults.headers['Authorization'];
  141. // if (!authHeader) {
  142. // return; // Exit the function if the token is not set
  143. // }
  144. // const params ={
  145. // pageNum: pagingController.pageNum,
  146. // pageSize: pagingController.pageSize,
  147. // ...filterObj,
  148. // ...tempSelectedValue,
  149. // }
  150. // try {
  151. // // const response = await axiosInstance.get<ItemsResult[]>(`${NEXT_PUBLIC_API_URL}/items/getRecordByPage`, {
  152. // // params,
  153. // // paramsSerializer: (params) => {
  154. // // return Qs.stringify(params, { arrayFormat: 'repeat' });
  155. // // },
  156. // // });
  157. // //setFilteredSchedules(response.data.records);
  158. // // setFilteredSchedules([
  159. // // {
  160. // // id: 1,
  161. // // scheduledPeriod: "2025-05-11 to 2025-05-17",
  162. // // scheduledAt: "2025-05-07",
  163. // // productCount: 13,
  164. // // },
  165. // // {
  166. // // id: 2,
  167. // // scheduledPeriod: "2025-05-18 to 2025-05-24",
  168. // // scheduledAt: "2025-05-14",
  169. // // productCount: 15,
  170. // // },
  171. // // {
  172. // // id: 3,
  173. // // scheduledPeriod: "2025-05-25 to 2025-05-31",
  174. // // scheduledAt: "2025-05-21",
  175. // // productCount: 13,
  176. // // },
  177. // // ])
  178. // setPagingController({
  179. // ...pagingController,
  180. // // totalCount: response.data.total
  181. // })
  182. // // return response; // Return the data from the response
  183. // } catch (error) {
  184. // console.error('Error fetching items:', error);
  185. // throw error; // Rethrow the error for further handling
  186. // }
  187. // };
  188. const onReset = useCallback(() => {
  189. // setFilteredSchedules(items ?? []);
  190. // setFilterObj({});
  191. // setTempSelectedValue({});
  192. refetchData(inputs, "reset");
  193. }, []);
  194. return (
  195. <>
  196. <SearchBox
  197. criteria={searchCriteria}
  198. onSearch={(query) => {
  199. // resetPagingController()
  200. setInputs(() => (
  201. {
  202. scheduleAt: query?.scheduleAt,
  203. schedulePeriod: query?.schedulePeriod,
  204. schedulePeriodTo: query?.schedulePeriodTo,
  205. totalEstProdCount: Number(query?.totalEstProdCount)
  206. }
  207. ))
  208. refetchData(query, "search")
  209. // setFilterObj({
  210. // ...query
  211. // })
  212. }}
  213. onReset={onReset}
  214. />
  215. <SearchResults<ProdScheduleResult>
  216. items={filteredSchedules}
  217. columns={columns}
  218. setPagingController={setPagingController}
  219. pagingController={pagingController}
  220. totalCount={totalCount}
  221. // isAutoPaging={false}
  222. />
  223. </>
  224. );
  225. };
  226. export default RSOverview;