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

DoDetail.tsx 9.8 KiB

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