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

QrModal.tsx 7.1 KiB

6ヶ月前
6ヶ月前
6ヶ月前
3ヶ月前
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ヶ月前
6ヶ月前
6ヶ月前
3ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
6ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use client";
  2. import {
  3. Box,
  4. Button,
  5. Grid,
  6. Modal,
  7. ModalProps,
  8. Stack,
  9. Typography,
  10. } from "@mui/material";
  11. import { useCallback, useEffect, useMemo, useState } from "react";
  12. import ReactQrCodeScanner, {
  13. ScannerConfig,
  14. } from "../ReactQrCodeScanner/ReactQrCodeScanner";
  15. import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
  16. import {
  17. fetchStockInLineInfo,
  18. ModalFormInput,
  19. StockInLineEntry,
  20. updateStockInLine,
  21. } from "@/app/api/po/actions";
  22. import PutAwayForm from "./PutAwayForm";
  23. import { StockInLine } from "@/app/api/po";
  24. import { WarehouseResult } from "@/app/api/warehouse";
  25. import { QrCodeInfo } from "@/app/api/qrcode";
  26. import { Check } from "@mui/icons-material";
  27. import { useTranslation } from "react-i18next";
  28. import { useSearchParams } from "next/navigation";
  29. import { useQrCodeScannerContext } from "../QrCodeScannerProvider/QrCodeScannerProvider";
  30. interface Props extends Omit<ModalProps, "children"> {
  31. warehouse: WarehouseResult[];
  32. }
  33. const style = {
  34. position: "absolute",
  35. top: "50%",
  36. left: "50%",
  37. transform: "translate(-50%, -50%)",
  38. bgcolor: "background.paper",
  39. pt: 5,
  40. px: 5,
  41. pb: 10,
  42. width: "auto",
  43. };
  44. const QrModal: React.FC<Props> = ({ open, onClose, warehouse }) => {
  45. const { t } = useTranslation();
  46. const [serverError, setServerError] = useState("");
  47. const params = useSearchParams();
  48. const formProps = useForm<ModalFormInput>({
  49. defaultValues: {
  50. status: "received",
  51. // acceptedQty
  52. // ...itemDetail,
  53. },
  54. });
  55. const errors = formProps.formState.errors;
  56. const closeHandler = useCallback<NonNullable<ModalProps["onClose"]>>(
  57. (...args) => {
  58. onClose?.(...args);
  59. setItemDetail(undefined);
  60. setStockInLineId(undefined);
  61. // reset();
  62. },
  63. [onClose],
  64. );
  65. const [stockInLineId, setStockInLineId] = useState<number>();
  66. const scannerConfig = useMemo<ScannerConfig>(
  67. () => ({
  68. onUpdate: (err, result) => {
  69. if (result) {
  70. const data: QrCodeInfo = JSON.parse(result.getText());
  71. console.log(data);
  72. if (data.stockInLineId) {
  73. console.log("still got in");
  74. console.log(data.stockInLineId);
  75. setStockInLineId(data.stockInLineId);
  76. }
  77. } else return;
  78. },
  79. }),
  80. [],
  81. );
  82. // QR Code Scanner
  83. const scanner = useQrCodeScannerContext();
  84. useEffect(() => {
  85. if (open && !scanner.isScanning) {
  86. scanner.startScan();
  87. } else if (!open && scanner.isScanning) {
  88. scanner.stopScan();
  89. }
  90. }, [open]);
  91. useEffect(() => {
  92. if (scanner.values.length > 0 && !Boolean(itemDetail)) {
  93. console.log(scanner.values[0]);
  94. const data: QrCodeInfo = JSON.parse(scanner.values[0]);
  95. console.log(data);
  96. if (data.stockInLineId) {
  97. console.log("still got in");
  98. console.log(data.stockInLineId);
  99. setStockInLineId(data.stockInLineId);
  100. }
  101. scanner.resetScan();
  102. }
  103. }, [scanner.values]);
  104. const [itemDetail, setItemDetail] = useState<StockInLine>();
  105. const [disabledSubmit, setDisabledSubmit] = useState(false);
  106. const [unavailableText, setUnavailableText] = useState<string | undefined>(
  107. undefined,
  108. );
  109. const fetchStockInLine = useCallback(
  110. async (stockInLineId: number) => {
  111. setUnavailableText(undefined);
  112. const res = await fetchStockInLineInfo(stockInLineId);
  113. if (res.status.toLowerCase() === "received") {
  114. console.log(res.acceptedQty);
  115. formProps.setValue("acceptedQty", res.acceptedQty);
  116. setDisabledSubmit(false);
  117. setItemDetail(res);
  118. } else if (res.status.toLowerCase() === "completed") {
  119. setDisabledSubmit(true);
  120. } else {
  121. //
  122. setUnavailableText("Item Not Available");
  123. setDisabledSubmit(true);
  124. }
  125. },
  126. [formProps, itemDetail, fetchStockInLineInfo],
  127. );
  128. useEffect(() => {
  129. if (stockInLineId) fetchStockInLine(stockInLineId);
  130. }, [stockInLineId]);
  131. const onSubmit = useCallback<SubmitHandler<ModalFormInput>>(
  132. async (data, event) => {
  133. const hasErrors = false;
  134. console.log("errors");
  135. console.log(errors);
  136. console.log("data");
  137. console.log(data);
  138. console.log("itemDetail");
  139. console.log(itemDetail);
  140. try {
  141. // add checking
  142. // const qty = data.sampleRate
  143. //////////////////////// modify this mess later //////////////////////
  144. const args = {
  145. id: itemDetail?.id,
  146. purchaseOrderId: parseInt(params.get("id")!),
  147. purchaseOrderLineId: itemDetail?.purchaseOrderLineId,
  148. itemId: itemDetail?.itemId,
  149. acceptedQty: data.acceptedQty,
  150. warehouseId: data.warehouseId,
  151. status: data.status,
  152. // ...data,
  153. // productionDate: productionDate,
  154. } as StockInLineEntry & ModalFormInput;
  155. //////////////////////////////////////////////////////////////////////
  156. console.log(args);
  157. // return
  158. if (hasErrors) {
  159. setServerError(t("An error has occurred. Please try again later."));
  160. return false;
  161. }
  162. // return;
  163. const res = await updateStockInLine(args);
  164. if (Boolean(res.id)) {
  165. // update entries
  166. console.log(res);
  167. // add loading
  168. closeHandler({}, "backdropClick");
  169. }
  170. console.log(res);
  171. // if (res)
  172. } catch (e) {
  173. // server error
  174. setServerError(t("An error has occurred. Please try again later."));
  175. console.log(e);
  176. }
  177. },
  178. [t, itemDetail],
  179. );
  180. return (
  181. <FormProvider {...formProps}>
  182. <Modal open={open} onClose={closeHandler}>
  183. <Box
  184. sx={style}
  185. component="form"
  186. onSubmit={formProps.handleSubmit(onSubmit)}
  187. >
  188. <Grid container xs={12}>
  189. <Grid item xs={12}>
  190. {itemDetail != undefined ? (
  191. unavailableText != undefined ? (
  192. <Typography variant="h4" marginInlineEnd={2}>
  193. {unavailableText}
  194. </Typography>
  195. ) : (
  196. <>
  197. <PutAwayForm
  198. itemDetail={itemDetail}
  199. warehouse={warehouse}
  200. disabled={false}
  201. />
  202. <Stack direction="row" justifyContent="flex-end" gap={1}>
  203. <Button
  204. name="submit"
  205. variant="contained"
  206. startIcon={<Check />}
  207. type="submit"
  208. disabled={disabledSubmit}
  209. >
  210. {t("submit")}
  211. </Button>
  212. </Stack>
  213. </>
  214. )
  215. ) : (
  216. // <ReactQrCodeScanner scannerConfig={scannerConfig} />
  217. <Typography variant="h4">
  218. {t(
  219. "Will start binding procedure after scanning item qr code.",
  220. )}
  221. </Typography>
  222. )}
  223. </Grid>
  224. </Grid>
  225. </Box>
  226. </Modal>
  227. </FormProvider>
  228. );
  229. };
  230. export default QrModal;