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.
 
 

227 lines
6.7 KiB

  1. "use client";
  2. import { useCallback, useEffect, useMemo, useState } from "react";
  3. import { useRouter, useSearchParams } from "next/navigation";
  4. import { useTranslation } from "react-i18next";
  5. import { CreateItemInputs, saveItem } from "@/app/api/settings/item/actions";
  6. import {
  7. FormProvider,
  8. SubmitErrorHandler,
  9. SubmitHandler,
  10. useForm,
  11. } from "react-hook-form";
  12. import { deleteDialog } from "../Swal/CustomAlerts";
  13. import {
  14. Box,
  15. Button,
  16. Grid,
  17. Stack,
  18. Tab,
  19. Tabs,
  20. TabsProps,
  21. Typography,
  22. } from "@mui/material";
  23. import { Check, Close, EditNote } from "@mui/icons-material";
  24. import { TypeEnum } from "@/app/utils/typeEnum";
  25. import ProductDetails from "./ProductDetails";
  26. import { CreateItemResponse } from "@/app/api/utils";
  27. import QcDetails from "./QcDetails";
  28. import { ItemQc } from "@/app/api/settings/item";
  29. import { saveItemQcChecks } from "@/app/api/settings/qcCheck/actions";
  30. import { useGridApiRef } from "@mui/x-data-grid";
  31. import { QcCategoryCombo } from "@/app/api/settings/qcCategory";
  32. type Props = {
  33. isEditMode: boolean;
  34. // type: TypeEnum;
  35. defaultValues: Partial<CreateItemInputs> | undefined;
  36. qcChecks: ItemQc[];
  37. qcCategoryCombo: QcCategoryCombo[]
  38. };
  39. const CreateItem: React.FC<Props> = ({
  40. isEditMode,
  41. // type,
  42. defaultValues,
  43. qcChecks,
  44. qcCategoryCombo,
  45. }) => {
  46. // console.log(type)
  47. const apiRef = useGridApiRef();
  48. const params = useSearchParams();
  49. console.log(params.get("id"));
  50. const [serverError, setServerError] = useState("");
  51. const [tabIndex, setTabIndex] = useState(0);
  52. const { t } = useTranslation("items");
  53. const router = useRouter();
  54. const title = "Product / Material";
  55. const [mode, redirPath] = useMemo(() => {
  56. // var typeId = TypeEnum.CONSUMABLE_ID
  57. let title = "";
  58. let mode = "";
  59. let redirPath = "";
  60. // if (type === TypeEnum.MATERIAL) {
  61. // typeId = TypeEnum.MATERIAL_ID
  62. // title = "Material";
  63. // redirPath = "/settings/material";
  64. // }
  65. // if (type === TypeEnum.PRODUCT) {
  66. // typeId = TypeEnum.PRODUCT_ID
  67. title = "Product";
  68. redirPath = "/settings/items";
  69. // }
  70. // if (type === TypeEnum.BYPRODUCT) {
  71. // typeId = TypeEnum.BYPRODUCT_ID
  72. // title = "By-Product";
  73. // redirPath = "/settings/byProduct";
  74. // }
  75. if (isEditMode) {
  76. mode = "Edit";
  77. } else {
  78. mode = "Create";
  79. }
  80. return [mode, redirPath];
  81. }, [isEditMode]);
  82. // console.log(typeId)
  83. const formProps = useForm<CreateItemInputs>({
  84. defaultValues: defaultValues ? defaultValues : {},
  85. });
  86. const errors = formProps.formState.errors;
  87. const handleTabChange = useCallback<NonNullable<TabsProps["onChange"]>>(
  88. (_e, newValue) => {
  89. setTabIndex(newValue);
  90. },
  91. [],
  92. );
  93. const handleCancel = () => {
  94. router.replace(`/settings/product`);
  95. };
  96. const onSubmit = useCallback<SubmitHandler<CreateItemInputs & {}>>(
  97. async (data, event) => {
  98. const hasErrors = false;
  99. console.log(errors);
  100. // console.log(apiRef.current.getCellValue(2, "lowerLimit"))
  101. // apiRef.current.
  102. try {
  103. if (hasErrors) {
  104. setServerError(t("An error has occurred. Please try again later."));
  105. return false;
  106. }
  107. console.log("data posted");
  108. console.log(data);
  109. const qcCheck =
  110. data.qcChecks.length > 0
  111. ? data.qcChecks
  112. .filter((q) => data.qcChecks_active.includes(q.id!))
  113. .map((qc) => {
  114. return {
  115. qcItemId: qc.id,
  116. instruction: qc.instruction,
  117. lowerLimit: qc.lowerLimit,
  118. upperLimit: qc.upperLimit,
  119. itemId: parseInt(params.get("id")!.toString()),
  120. };
  121. })
  122. : [];
  123. const test = data.qcChecks.filter((q) =>
  124. data.qcChecks_active.includes(q.id!),
  125. );
  126. // TODO:
  127. // 1. check field ( directly modify col def / check here )
  128. // 2. set error change tab index
  129. console.log(test);
  130. console.log(qcCheck);
  131. // return
  132. // do api
  133. console.log("asdad");
  134. const responseI = await saveItem(data);
  135. console.log("asdad");
  136. const responseQ = await saveItemQcChecks(qcCheck);
  137. if (responseI && responseQ) {
  138. if (!Boolean(responseI.id)) {
  139. formProps.setError(
  140. responseI.errorPosition! as keyof CreateItemInputs,
  141. {
  142. message: responseI.message!,
  143. type: "required",
  144. },
  145. );
  146. } else if (!Boolean(responseQ.id)) {
  147. } else if (Boolean(responseI.id) && Boolean(responseQ.id)) {
  148. router.replace(redirPath);
  149. }
  150. }
  151. } catch (e) {
  152. // backend error
  153. setServerError(t("An error has occurred. Please try again later."));
  154. console.log(e);
  155. }
  156. },
  157. [apiRef, router, t],
  158. );
  159. // multiple tabs
  160. const onSubmitError = useCallback<SubmitErrorHandler<CreateItemInputs>>(
  161. (errors) => {},
  162. [],
  163. );
  164. return (
  165. <>
  166. <FormProvider {...formProps}>
  167. <Stack
  168. spacing={2}
  169. component="form"
  170. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  171. >
  172. <Grid>
  173. <Typography mb={2} variant="h4">
  174. {t(`${mode} ${title}`)}
  175. </Typography>
  176. </Grid>
  177. <Tabs
  178. value={tabIndex}
  179. onChange={handleTabChange}
  180. variant="scrollable"
  181. >
  182. <Tab label={t("Product / Material Details")} iconPosition="end" />
  183. <Tab label={t("Qc items")} iconPosition="end" />
  184. </Tabs>
  185. {serverError && (
  186. <Typography variant="body2" color="error" alignSelf="flex-end">
  187. {serverError}
  188. </Typography>
  189. )}
  190. {tabIndex === 0 && <ProductDetails isEditMode={isEditMode} qcCategoryCombo={qcCategoryCombo}/>}
  191. {tabIndex === 1 && <QcDetails apiRef={apiRef} />}
  192. {/* {type === TypeEnum.MATERIAL && <MaterialDetails />} */}
  193. {/* {type === TypeEnum.BYPRODUCT && <ByProductDetails />} */}
  194. {/*
  195. <Stack direction="row" justifyContent="flex-end" gap={1}>
  196. <Button
  197. name="submit"
  198. variant="contained"
  199. startIcon={<Check />}
  200. type="submit"
  201. >
  202. {isEditMode ? t("Save") : t("Confirm")}
  203. </Button>
  204. <Button
  205. variant="outlined"
  206. startIcon={<Close />}
  207. onClick={handleCancel}
  208. >
  209. {t("Cancel")}
  210. </Button>
  211. </Stack>
  212. */}
  213. </Stack>
  214. </FormProvider>
  215. </>
  216. );
  217. };
  218. export default CreateItem;