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

DoDetail.tsx 10 KiB

10ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
8ヶ月前
7ヶ月前
6ヶ月前
8ヶ月前
8ヶ月前
8ヶ月前
7ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
8ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
6ヶ月前
5ヶ月前
7ヶ月前
5ヶ月前
7ヶ月前
5ヶ月前
7ヶ月前
8ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
7ヶ月前
6ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
6ヶ月前
7ヶ月前
8ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. "use client";
  2. import type { DoDetail as DoDetailType } from "@/app/api/do/actions";
  3. import { useRouter } from "next/navigation";
  4. import { useTranslation } from "react-i18next";
  5. import useUploadContext from "../UploadProvider/useUploadContext";
  6. import { FormProvider, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form";
  7. import { useCallback, useState } from "react";
  8. import { Button, Stack, Typography, Box, Alert } from "@mui/material";
  9. import ArrowBackIcon from '@mui/icons-material/ArrowBack';
  10. import StartIcon from "@mui/icons-material/Start";
  11. import { releaseDo, startBatchReleaseAsyncSingle, assignPickOrderByStore, releaseAssignedPickOrderByStore } from "@/app/api/do/actions";
  12. import {
  13. getWorkbenchBatchReleaseProgress,
  14. startWorkbenchBatchReleaseAsyncSingleV2,
  15. } from "@/app/api/doworkbench/actions";
  16. import Swal from "sweetalert2";
  17. import DoInfoCard from "./DoInfoCard";
  18. import DoLineTable from "./DoLineTable";
  19. import { useSession } from "next-auth/react";
  20. import { SessionWithTokens } from "@/config/authConfig"; // Import the correct session type
  21. type Props = {
  22. id?: number;
  23. defaultValues: Partial<DoDetailType> | undefined;
  24. workbenchRelease?: boolean;
  25. }
  26. const DoDetail: React.FC<Props> = ({
  27. defaultValues,
  28. id,
  29. workbenchRelease = true,
  30. }) => {
  31. const { t } = useTranslation("do")
  32. const router = useRouter();
  33. const { setIsUploading } = useUploadContext();
  34. const [serverError, setServerError] = useState("");
  35. const [successMessage, setSuccessMessage] = useState("");
  36. const [isAssigning, setIsAssigning] = useState(false);
  37. const { data: session } = useSession() as { data: SessionWithTokens | null }; // Use correct session type
  38. const currentUserId = session?.id ? parseInt(session.id) : undefined; // Get user ID from session.id
  39. //console.log("🔍 DoSearch - session:", session);
  40. //console.log("🔍 DoSearch - currentUserId:", currentUserId);
  41. const formProps = useForm<DoDetailType>({
  42. defaultValues: defaultValues
  43. })
  44. const handleBack = useCallback(() => {
  45. router.replace(`/do`)
  46. }, [router])
  47. const handleRelease = useCallback(async () => {
  48. try {
  49. setIsUploading(true)
  50. setServerError("")
  51. setSuccessMessage("")
  52. if (id) {
  53. // Get current user ID from session
  54. //const currentUserId = session?.id ? parseInt(session.id) : undefined;
  55. //if (!currentUserId) {
  56. // setServerError("User session not found. Please login again.");
  57. // return;
  58. //}
  59. /*
  60. const response = await releaseDo({
  61. id: id,
  62. //userId: currentUserId // Pass user ID from session
  63. })
  64. */
  65. const response = await startWorkbenchBatchReleaseAsyncSingleV2({
  66. doId: id,
  67. userId: currentUserId ?? 0
  68. })
  69. if (response?.code === "STARTED") {
  70. setSuccessMessage(t("DO released successfully! Pick orders created."));
  71. router.refresh();
  72. } else if (response) {
  73. setServerError(
  74. response.message ??
  75. response.code ??
  76. t("An error has occurred. Please try again later."),
  77. );
  78. }
  79. }
  80. } catch (e) {
  81. setServerError(t("An error has occurred. Please try again later."));
  82. console.log(e);
  83. } finally {
  84. setIsUploading(false)
  85. }
  86. }, [id, formProps, t, setIsUploading, session]) // Add session to dependencies
  87. // UPDATE STORE-BASED ASSIGNMENT HANDLERS
  88. const handleAssignByStore = useCallback(async (storeId: string) => {
  89. try {
  90. setIsAssigning(true)
  91. setServerError("")
  92. setSuccessMessage("")
  93. // Get current user ID from session
  94. const currentUserId = session?.id ? parseInt(session.id) : undefined;
  95. if (!currentUserId) {
  96. setServerError("User session not found. Please login again.");
  97. return;
  98. }
  99. const response = await assignPickOrderByStore({
  100. storeId: storeId,
  101. assignTo: currentUserId
  102. })
  103. if (response) {
  104. setSuccessMessage(`Pick orders assigned to ${storeId} successfully!`)
  105. console.log("Assignment response:", response)
  106. }
  107. } catch (e) {
  108. setServerError(t("Failed to assign pick orders. Please try again later."));
  109. console.log(e);
  110. } finally {
  111. setIsAssigning(false)
  112. }
  113. }, [t, session]) // Add session to dependencies
  114. const handleReleaseByStore = useCallback(async (storeId: string) => {
  115. try {
  116. setIsAssigning(true)
  117. setServerError("")
  118. setSuccessMessage("")
  119. // Get current user ID from session
  120. const currentUserId = session?.id ? parseInt(session.id) : undefined;
  121. if (!currentUserId) {
  122. setServerError("User session not found. Please login again.");
  123. return;
  124. }
  125. const response = await releaseAssignedPickOrderByStore({
  126. storeId: storeId,
  127. assignTo: currentUserId
  128. })
  129. if (response) {
  130. setSuccessMessage(`Pick orders released for ${storeId} successfully!`)
  131. console.log("Release response:", response)
  132. }
  133. } catch (e) {
  134. setServerError(t("Failed to release pick orders. Please try again later."));
  135. console.log(e);
  136. } finally {
  137. setIsAssigning(false)
  138. }
  139. }, [t, session]) // Add session to dependencies
  140. const onSubmit = useCallback<SubmitHandler<DoDetailType>>(async (data, event) => {
  141. console.log(data)
  142. }, [t])
  143. const onSubmitError = useCallback<SubmitErrorHandler<DoDetailType>>((errors) => {
  144. console.log(errors)
  145. }, [t])
  146. return <>
  147. <FormProvider {...formProps}>
  148. <Stack
  149. spacing={2}
  150. component="form"
  151. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  152. >
  153. {serverError && (
  154. <Alert severity="error" sx={{ mb: 2 }}>
  155. {serverError}
  156. </Alert>
  157. )}
  158. {successMessage && (
  159. <Alert severity="success" sx={{ mb: 2 }}>
  160. {successMessage}
  161. </Alert>
  162. )}
  163. {formProps.watch("status")?.toLowerCase() === "pending" && (
  164. <Stack direction="row" justifyContent="flex-start" gap={1}>
  165. <Button
  166. variant="outlined"
  167. startIcon={<StartIcon />}
  168. onClick={handleRelease}
  169. disabled={isAssigning}
  170. >
  171. {t("Release")}
  172. </Button>
  173. </Stack>
  174. )
  175. }
  176. {/* ADD STORE-BASED ASSIGNMENT BUTTONS */}
  177. {/*
  178. {
  179. formProps.watch("status")?.toLowerCase() === "released" && (
  180. <Box sx={{ mb: 2 }}>
  181. <Typography variant="h6" gutterBottom>
  182. {t("Pick Order Assignment")}
  183. </Typography>
  184. <Stack direction="row" spacing={2}>
  185. <Button
  186. variant="contained"
  187. color="primary"
  188. onClick={() => handleAssignByStore("2/F")}
  189. disabled={isAssigning}
  190. sx={{ minWidth: 120 }}
  191. >
  192. {t("Assign 2/F")}
  193. </Button>
  194. <Button
  195. variant="contained"
  196. color="secondary"
  197. onClick={() => handleAssignByStore("4/F")}
  198. disabled={isAssigning}
  199. sx={{ minWidth: 120 }}
  200. >
  201. {t("Assign 4/F")}
  202. </Button>
  203. </Stack>
  204. <Stack direction="row" spacing={2} sx={{ mt: 1 }}>
  205. <Button
  206. variant="outlined"
  207. color="primary"
  208. onClick={() => handleReleaseByStore("2/F")}
  209. disabled={isAssigning}
  210. sx={{ minWidth: 120 }}
  211. >
  212. {t("Release 2/F")}
  213. </Button>
  214. <Button
  215. variant="outlined"
  216. color="secondary"
  217. onClick={() => handleReleaseByStore("4/F")}
  218. disabled={isAssigning}
  219. sx={{ minWidth: 120 }}
  220. >
  221. {t("Release 4/F")}
  222. </Button>
  223. </Stack>
  224. </Box>
  225. )}
  226. */}
  227. <DoInfoCard />
  228. <DoLineTable />
  229. <Stack direction="row" justifyContent="flex-end" gap={1}>
  230. <Button
  231. variant="outlined"
  232. startIcon={<ArrowBackIcon />}
  233. onClick={handleBack}
  234. >
  235. {t("Back")}
  236. </Button>
  237. </Stack>
  238. </Stack>
  239. </FormProvider>
  240. </>
  241. }
  242. export default DoDetail;