FPSMS-frontend
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

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