FPSMS-frontend
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

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