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.
 
 

152 regels
4.3 KiB

  1. "use client";
  2. import { useRouter } from "next/navigation";
  3. import React, {
  4. useCallback,
  5. useEffect,
  6. useState,
  7. } from "react";
  8. import { useTranslation } from "react-i18next";
  9. import {
  10. Button,
  11. Stack,
  12. Typography,
  13. } from "@mui/material";
  14. import {
  15. FormProvider,
  16. SubmitErrorHandler,
  17. SubmitHandler,
  18. useForm,
  19. } from "react-hook-form";
  20. import { Check, Close, RestartAlt } from "@mui/icons-material";
  21. import {
  22. WarehouseInputs,
  23. createWarehouse,
  24. } from "@/app/api/warehouse/actions";
  25. import WarehouseDetail from "./WarehouseDetail";
  26. const CreateWarehouse: React.FC = () => {
  27. const { t } = useTranslation(["warehouse", "common"]);
  28. const formProps = useForm<WarehouseInputs>();
  29. const router = useRouter();
  30. const [serverError, setServerError] = useState("");
  31. const resetForm = React.useCallback((e?: React.MouseEvent<HTMLButtonElement>) => {
  32. e?.preventDefault();
  33. e?.stopPropagation();
  34. try {
  35. formProps.reset({
  36. store_id: "",
  37. warehouse: "",
  38. area: "",
  39. slot: "",
  40. order: "",
  41. stockTakeSection: "",
  42. stockTakeSectionDescription: "",
  43. });
  44. } catch (error) {
  45. console.log(error);
  46. setServerError(t("An error has occurred. Please try again later."));
  47. }
  48. }, [formProps, t]);
  49. useEffect(() => {
  50. resetForm();
  51. }, []);
  52. const handleCancel = () => {
  53. router.back();
  54. };
  55. const onSubmit = useCallback<SubmitHandler<WarehouseInputs>>(
  56. async (data) => {
  57. try {
  58. // Automatically append "F" to store_id if not already present
  59. // Remove any existing "F" to avoid duplication, then append it
  60. const cleanStoreId = (data.store_id || "").replace(/F$/i, "").trim();
  61. const storeIdWithF = cleanStoreId ? `${cleanStoreId}F` : "";
  62. // Generate code, name, description from the input fields
  63. // Format: store_idF-warehouse-area-slot (F is automatically appended)
  64. const code = storeIdWithF
  65. ? `${storeIdWithF}-${data.warehouse || ""}-${data.area || ""}-${data.slot || ""}`
  66. : `${data.warehouse || ""}-${data.area || ""}-${data.slot || ""}`;
  67. const name = storeIdWithF
  68. ? `${storeIdWithF}-${data.warehouse || ""}`
  69. : `${data.warehouse || ""}`;
  70. const description = storeIdWithF
  71. ? `${storeIdWithF}-${data.warehouse || ""}`
  72. : `${data.warehouse || ""}`;
  73. const warehouseData: WarehouseInputs = {
  74. ...data,
  75. store_id: storeIdWithF, // Save with F (F is automatically appended)
  76. code: code.trim(),
  77. name: name.trim(),
  78. description: description.trim(),
  79. capacity: 10000, // Default capacity
  80. };
  81. await createWarehouse(warehouseData);
  82. router.replace("/settings/warehouse");
  83. } catch (e) {
  84. console.log(e);
  85. const message = e instanceof Error ? e.message : t("An error has occurred. Please try again later.");
  86. setServerError(message);
  87. }
  88. },
  89. [router, t],
  90. );
  91. const onSubmitError = useCallback<SubmitErrorHandler<WarehouseInputs>>(
  92. (errors) => {
  93. console.log(errors);
  94. },
  95. [],
  96. );
  97. return (
  98. <>
  99. {serverError && (
  100. <Typography variant="body2" color="error" alignSelf="flex-end">
  101. {serverError}
  102. </Typography>
  103. )}
  104. <FormProvider {...formProps}>
  105. <Stack
  106. spacing={2}
  107. component="form"
  108. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  109. >
  110. <WarehouseDetail />
  111. <Stack direction="row" justifyContent="flex-end" gap={1}>
  112. <Button
  113. variant="text"
  114. startIcon={<RestartAlt />}
  115. onClick={(e) => {
  116. e.preventDefault();
  117. e.stopPropagation();
  118. resetForm(e);
  119. }}
  120. type="button"
  121. >
  122. {t("Reset")}
  123. </Button>
  124. <Button
  125. variant="outlined"
  126. startIcon={<Close />}
  127. onClick={handleCancel}
  128. type="button"
  129. >
  130. {t("Cancel")}
  131. </Button>
  132. <Button variant="contained" startIcon={<Check />} type="submit">
  133. {t("Confirm")}
  134. </Button>
  135. </Stack>
  136. </Stack>
  137. </FormProvider>
  138. </>
  139. );
  140. };
  141. export default CreateWarehouse;