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.
 
 
 

143 regels
4.4 KiB

  1. "use client"
  2. import { SearchJoResult, SearchJoResultRequest, fetchJos } from "@/app/api/jo/actions";
  3. import React, { useCallback, useEffect, useMemo, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import { Criterion } from "../SearchBox";
  6. import SearchResults, { Column, defaultPagingController } from "../SearchResults/SearchResults";
  7. import { EditNote } from "@mui/icons-material";
  8. import { decimalFormatter } from "@/app/utils/formatUtil";
  9. import { uniqBy, upperFirst } from "lodash";
  10. import SearchBox from "../SearchBox/SearchBox";
  11. interface Props {
  12. defaultInputs: SearchJoResultRequest
  13. }
  14. type SearchQuery = Partial<Omit<SearchJoResult, "id">>;
  15. type SearchParamNames = keyof SearchQuery;
  16. const JoSearch: React.FC<Props> = ({ defaultInputs }) => {
  17. const { t } = useTranslation("jo");
  18. const [filteredJos, setFilteredJos] = useState<SearchJoResult[]>([]);
  19. const [inputs, setInputs] = useState(defaultInputs);
  20. const [pagingController, setPagingController] = useState(
  21. defaultPagingController
  22. )
  23. const [totalCount, setTotalCount] = useState(0)
  24. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(() => [
  25. { label: t("Code"), paramName: "code", type: "text" },
  26. { label: t("Name"), paramName: "name", type: "text" },
  27. ], [t])
  28. const columns = useMemo<Column<SearchJoResult>[]>(
  29. () => [
  30. {
  31. name: "id",
  32. label: t("Details"),
  33. onClick: (record) => onDetailClick(record),
  34. buttonIcon: <EditNote />,
  35. },
  36. {
  37. name: "code",
  38. label: t("Code")
  39. },
  40. {
  41. name: "name",
  42. label: t("Name"),
  43. },
  44. {
  45. name: "reqQty",
  46. label: t("Req. Qty"),
  47. align: "right",
  48. headerAlign: "right",
  49. renderCell: (row) => {
  50. return decimalFormatter.format(row.reqQty)
  51. }
  52. },
  53. {
  54. name: "uom",
  55. label: t("UoM"),
  56. align: "left",
  57. headerAlign: "left",
  58. renderCell: (row) => {
  59. return t(row.uom)
  60. }
  61. },
  62. {
  63. name: "status",
  64. label: t("Status"),
  65. renderCell: (row) => {
  66. return t(upperFirst(row.status))
  67. }
  68. }
  69. ], []
  70. )
  71. const refetchData = useCallback(async (
  72. query: Record<SearchParamNames, string> | SearchJoResultRequest,
  73. actionType: "reset" | "search" | "paging",
  74. ) => {
  75. const params: SearchJoResultRequest = {
  76. code: query.code,
  77. name: query.name,
  78. pageNum: pagingController.pageNum - 1,
  79. pageSize: pagingController.pageSize,
  80. }
  81. const response = await fetchJos(params)
  82. if (response) {
  83. setTotalCount(response.total);
  84. switch (actionType) {
  85. case "reset":
  86. case "search":
  87. setFilteredJos(() => response.records);
  88. break;
  89. case "paging":
  90. setFilteredJos((fs) =>
  91. uniqBy([...fs, ...response.records], "id"),
  92. );
  93. break;
  94. }
  95. }
  96. }, [pagingController, setPagingController])
  97. useEffect(() => {
  98. refetchData(inputs, "paging");
  99. }, [pagingController]);
  100. const onDetailClick = useCallback((record: SearchJoResult) => {
  101. }, [])
  102. const onSearch = useCallback((query: Record<SearchParamNames, string>) => {
  103. setInputs(() => ({
  104. code: query.code,
  105. name: query.name
  106. }))
  107. refetchData(query, "search");
  108. }, [])
  109. const onReset = useCallback(() => {
  110. refetchData(defaultInputs, "paging");
  111. }, [])
  112. return <>
  113. <SearchBox
  114. criteria={searchCriteria}
  115. onSearch={onSearch}
  116. onReset={onReset}
  117. />
  118. <SearchResults<SearchJoResult>
  119. items={filteredJos}
  120. columns={columns}
  121. setPagingController={setPagingController}
  122. pagingController={pagingController}
  123. totalCount={totalCount}
  124. // isAutoPaging={false}
  125. />
  126. </>
  127. }
  128. export default JoSearch;