FPSMS-frontend
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PoSearch.tsx 6.2 KiB

6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
5ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use client";
  2. import { PoResult } from "@/app/api/po";
  3. import { useCallback, useEffect, useMemo, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import { useRouter, useSearchParams } from "next/navigation";
  6. import SearchBox, { Criterion } from "../SearchBox";
  7. import SearchResults, { Column } from "../SearchResults";
  8. import { EditNote } from "@mui/icons-material";
  9. import { Button, Grid, Tab, Tabs, TabsProps, Typography } from "@mui/material";
  10. import QrModal from "../PoDetail/QrModal";
  11. import { WarehouseResult } from "@/app/api/warehouse";
  12. import NotificationIcon from "@mui/icons-material/NotificationImportant";
  13. import { useSession } from "next-auth/react";
  14. import { defaultPagingController } from "../SearchResults/SearchResults";
  15. import { fetchPoListClient, testing } from "@/app/api/po/actions";
  16. import dayjs from "dayjs";
  17. import { OUTPUT_DATE_FORMAT } from "@/app/utils/formatUtil";
  18. import arraySupport from "dayjs/plugin/arraySupport";
  19. dayjs.extend(arraySupport);
  20. type Props = {
  21. po: PoResult[];
  22. warehouse: WarehouseResult[];
  23. totalCount: number;
  24. };
  25. type SearchQuery = Partial<Omit<PoResult, "id">>;
  26. type SearchParamNames = keyof SearchQuery;
  27. // cal offset (pageSize)
  28. // cal limit (pageSize)
  29. const PoSearch: React.FC<Props> = ({
  30. po,
  31. warehouse,
  32. totalCount: initTotalCount,
  33. }) => {
  34. const [filteredPo, setFilteredPo] = useState<PoResult[]>(po);
  35. const [filterArgs, setFilterArgs] = useState<Record<string, any>>({});
  36. const { t } = useTranslation("purchaseOrder");
  37. const router = useRouter();
  38. const [pagingController, setPagingController] = useState(
  39. defaultPagingController
  40. );
  41. const [totalCount, setTotalCount] = useState(initTotalCount);
  42. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(() => {
  43. var searchCriteria: Criterion<SearchParamNames>[] = [
  44. { label: t("Code"), paramName: "code", type: "text" },
  45. {
  46. label: t("Status"),
  47. paramName: "status",
  48. type: "select-labelled",
  49. options: [
  50. {label: t(`pending`), value: `pending`},
  51. {label: t(`receiving`), value: `receiving`},
  52. {label: t(`completed`), value: `completed`},
  53. ]
  54. },
  55. {
  56. label: t("Escalated"),
  57. paramName: "escalated",
  58. type: "select",
  59. options: [t("Escalated"), t("NotEscalated")],
  60. },
  61. ];
  62. return searchCriteria;
  63. }, [t, po]);
  64. const onDetailClick = useCallback(
  65. (po: PoResult) => {
  66. router.push(`/po/edit?id=${po.id}`);
  67. },
  68. [router]
  69. );
  70. const onDeleteClick = useCallback((po: PoResult) => {}, [router]);
  71. const columns = useMemo<Column<PoResult>[]>(
  72. () => [
  73. {
  74. name: "id",
  75. label: t("Details"),
  76. onClick: onDetailClick,
  77. buttonIcon: <EditNote />,
  78. },
  79. {
  80. name: "code",
  81. label: t("Code"),
  82. },
  83. {
  84. name: "orderDate",
  85. label: t("OrderDate"),
  86. renderCell: (params) => {
  87. return dayjs(params.orderDate)
  88. // .add(-1, "month")
  89. .format(OUTPUT_DATE_FORMAT);
  90. },
  91. },
  92. {
  93. name: "supplier",
  94. label: t("Supplier"),
  95. },
  96. {
  97. name: "status",
  98. label: t("Status"),
  99. renderCell: (params) => {
  100. return t(`${params.status.toLowerCase()}`);
  101. },
  102. },
  103. {
  104. name: "escalated",
  105. label: t("Escalated"),
  106. renderCell: (params) => {
  107. console.log(params.escalated)
  108. return params.escalated ? (
  109. <NotificationIcon color="warning" />
  110. ) : undefined;
  111. },
  112. },
  113. ],
  114. [filteredPo]
  115. );
  116. const onReset = useCallback(() => {
  117. setFilteredPo(po);
  118. }, [po]);
  119. const [isOpenScanner, setOpenScanner] = useState(false);
  120. const onOpenScanner = useCallback(() => {
  121. setOpenScanner(true);
  122. }, []);
  123. const onCloseScanner = useCallback(() => {
  124. setOpenScanner(false);
  125. }, []);
  126. const newPageFetch = useCallback(
  127. async (
  128. pagingController: Record<string, number>,
  129. filterArgs: Record<string, number>
  130. ) => {
  131. console.log(pagingController);
  132. console.log(filterArgs);
  133. const params = {
  134. ...pagingController,
  135. ...filterArgs,
  136. };
  137. const res = await fetchPoListClient(params);
  138. // const res = await testing(params);
  139. if (res) {
  140. console.log(res);
  141. setFilteredPo(res.records);
  142. setTotalCount(res.total);
  143. }
  144. },
  145. [fetchPoListClient]
  146. );
  147. useEffect(() => {
  148. newPageFetch(pagingController, filterArgs);
  149. }, [newPageFetch, pagingController, filterArgs]);
  150. return (
  151. <>
  152. <Grid container>
  153. <Grid item xs={8}>
  154. <Typography variant="h4" marginInlineEnd={2}>
  155. {t("Purchase Order")}
  156. </Typography>
  157. </Grid>
  158. <Grid item xs={4} display="flex" justifyContent="end" alignItems="end">
  159. <QrModal
  160. open={isOpenScanner}
  161. onClose={onCloseScanner}
  162. warehouse={warehouse}
  163. />
  164. <Button onClick={onOpenScanner}>{t("bind")}</Button>
  165. </Grid>
  166. </Grid>
  167. <>
  168. <SearchBox
  169. criteria={searchCriteria}
  170. onSearch={(query) => {
  171. console.log(query)
  172. setFilterArgs({
  173. code: query.code,
  174. status: query.status === "All" ? "" : query.status,
  175. escalated: query.escalated === 'All' ? undefined : query.escalated === t("Escalated")
  176. });
  177. // setFilteredPo((prev) =>
  178. // po.filter((p) => {
  179. // return (
  180. // p.code.toLowerCase().includes(query.code.toLowerCase()) &&
  181. // (query.status === "All" || t(`${p.status.toLowerCase()}`) === query.status) &&
  182. // (query.escalated === "All" || (p.escalated === Boolean((query.escalated) === t("Escalated"))))
  183. // )
  184. // })
  185. // );
  186. }}
  187. onReset={onReset}
  188. />
  189. <SearchResults<PoResult>
  190. items={filteredPo}
  191. columns={columns}
  192. pagingController={pagingController}
  193. setPagingController={setPagingController}
  194. totalCount={totalCount}
  195. isAutoPaging={false}
  196. />
  197. </>
  198. </>
  199. );
  200. };
  201. export default PoSearch;