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.
 
 

120 rivejä
3.1 KiB

  1. "use client";
  2. import Check from "@mui/icons-material/Check";
  3. import Close from "@mui/icons-material/Close";
  4. import Button from "@mui/material/Button";
  5. import Stack from "@mui/material/Stack";
  6. import Tab from "@mui/material/Tab";
  7. import Tabs, { TabsProps } from "@mui/material/Tabs";
  8. import { useRouter } from "next/navigation";
  9. import React, { useCallback, useState } from "react";
  10. import { useTranslation } from "react-i18next";
  11. import { Task, TaskTemplate } from "@/app/api/tasks";
  12. import {
  13. FieldErrors,
  14. FormProvider,
  15. SubmitErrorHandler,
  16. SubmitHandler,
  17. useForm,
  18. } from "react-hook-form";
  19. import { CreateDepartmentInputs, saveDepartment } from "@/app/api/departments/actions";
  20. import { Error } from "@mui/icons-material";
  21. import { ProjectCategory } from "@/app/api/projects";
  22. import { Typography } from "@mui/material";
  23. import DepartmentDetails from "./DepartmentDetails";
  24. import { DepartmentResult } from "@/app/api/departments";
  25. interface Props {
  26. isEdit: Boolean;
  27. department?: CreateDepartmentInputs;
  28. depCodes: string[]
  29. }
  30. const CreateDepartment: React.FC<Props> = ({
  31. isEdit,
  32. department,
  33. depCodes,
  34. }) => {
  35. const [serverError, setServerError] = useState("");
  36. const { t } = useTranslation();
  37. const router = useRouter();
  38. console.log(department)
  39. console.log(depCodes)
  40. const handleCancel = () => {
  41. router.back();
  42. };
  43. const onSubmit = useCallback<SubmitHandler<CreateDepartmentInputs>>(
  44. async (data) => {
  45. try {
  46. console.log(data);
  47. let haveError = false
  48. if (depCodes.includes(data.code.toLowerCase().trim())) {
  49. formProps.setError("code", { message: t("Duplicated code."), type: "required" })
  50. haveError = true
  51. }
  52. if (haveError) {
  53. return
  54. }
  55. console.log("passed")
  56. setServerError("");
  57. // console.log(JSON.stringify(data));
  58. await saveDepartment(data)
  59. router.replace("/settings/department");
  60. } catch (e) {
  61. setServerError(t("An error has occurred. Please try again later."));
  62. }
  63. },
  64. [router, t],
  65. );
  66. const onSubmitError = useCallback<SubmitErrorHandler<CreateDepartmentInputs>>(
  67. (errors) => {
  68. console.log(errors)
  69. },
  70. [],
  71. );
  72. const formProps = useForm<CreateDepartmentInputs>({
  73. defaultValues: {
  74. id: department?.id,
  75. code: department?.code,
  76. name: department?.name,
  77. description: department?.description,
  78. },
  79. });
  80. const errors = formProps.formState.errors;
  81. return (
  82. <FormProvider {...formProps}>
  83. <Stack
  84. spacing={2}
  85. component="form"
  86. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  87. >
  88. {
  89. <DepartmentDetails />
  90. }
  91. <Stack direction="row" justifyContent="flex-end" gap={1}>
  92. <Button
  93. variant="outlined"
  94. startIcon={<Close />}
  95. onClick={handleCancel}
  96. >
  97. {t("Cancel")}
  98. </Button>
  99. <Button variant="contained" startIcon={<Check />} type="submit">
  100. {t("Confirm")}
  101. </Button>
  102. </Stack>
  103. </Stack>
  104. </FormProvider>
  105. );
  106. };
  107. export default CreateDepartment;