Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

182 wiersze
6.2 KiB

  1. "use client";
  2. import { useCallback, useEffect, useState } from "react";
  3. import CustomInputForm from "../CustomInputForm";
  4. import { useRouter } from "next/navigation";
  5. import { useTranslation } from "react-i18next";
  6. import {
  7. FieldErrors,
  8. FormProvider,
  9. SubmitErrorHandler,
  10. SubmitHandler,
  11. useForm,
  12. } from "react-hook-form";
  13. import { CreateStaffInputs, saveStaff, testing } from "@/app/api/staff/actions";
  14. import { Button, Stack, Typography } from "@mui/material";
  15. // import CreateStaffForm from "../CreateStaffForm";
  16. import { comboProp, fetchCompanyCombo } from "@/app/api/companys/actions";
  17. import { fetchTeamCombo } from "@/app/api/team/actions";
  18. import { fetchDepartmentCombo } from "@/app/api/departments/actions";
  19. import { fetchPositionCombo } from "@/app/api/positions/actions";
  20. import { fetchGradeCombo } from "@/app/api/grades/actions";
  21. import { fetchSkillCombo } from "@/app/api/skill/actions";
  22. import { fetchSalaryCombo } from "@/app/api/salarys/actions";
  23. import StaffInfo from "./StaffInfo";
  24. import { Check, Close } from "@mui/icons-material";
  25. import { ServerFetchError } from "@/app/utils/fetchUtil";
  26. interface Field {
  27. id: string;
  28. label: string;
  29. type: string;
  30. value?: any;
  31. required?: boolean;
  32. pattern?: string;
  33. message?: string;
  34. options?: any[];
  35. readOnly?: boolean;
  36. }
  37. export interface comboItem {
  38. company: comboProp[];
  39. team: comboProp[];
  40. department: comboProp[];
  41. position: comboProp[];
  42. grade: comboProp[];
  43. skill: comboProp[];
  44. salary: comboProp[];
  45. }
  46. interface formProps {
  47. // Title?: string[];
  48. combos: comboItem;
  49. }
  50. const CreateStaff: React.FC<formProps> = ({ combos }) => {
  51. const { t } = useTranslation();
  52. const formProps = useForm<CreateStaffInputs>();
  53. const [serverError, setServerError] = useState("");
  54. const router = useRouter();
  55. const [tabIndex, setTabIndex] = useState(0);
  56. const errors = formProps.formState.errors;
  57. const checkDuplicates = (str1: string, str2: string, str3: string) => {
  58. return str1 === str2 || str1 === str3 || str2 === str3;
  59. }
  60. const onSubmit = useCallback<SubmitHandler<CreateStaffInputs>>(
  61. async (data) => {
  62. try {
  63. console.log(data);
  64. let haveError = false;
  65. const regex_email = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
  66. const regex_phone = /^\d{8}$/
  67. if (!regex_email.test(data.email)) {
  68. haveError = true
  69. formProps.setError("email", { message: t("Please Enter Correct Email."), type: "required" })
  70. }
  71. if (!regex_phone.test(data.phone1)) {
  72. haveError = true
  73. formProps.setError("phone1", { message: t("Please Enter Correct Phone No.."), type: "required" })
  74. }
  75. if (data.emergContactPhone && !regex_phone.test(data.emergContactPhone)) {
  76. haveError = true
  77. formProps.setError("emergContactPhone", { message: t("Please Enter Correct Phone No.."), type: "required" })
  78. }
  79. if (data.phone2 && data.phone2?.length > 0) {
  80. if(!regex_phone.test(data.phone2)) {
  81. haveError = true
  82. formProps.setError("phone2", { message: t("Please Enter Correct Phone No.."), type: "required" })
  83. }
  84. }
  85. if (data.phone1 === data.phone2 || data.phone1 === data.emergContactPhone || data.phone2 && data.phone2 === data.emergContactPhone && data.phone2.length > 0) {
  86. haveError = true
  87. formProps.setError("phone1", { message: t("Please Enter Different Phone No.."), type: "required" })
  88. if (data.phone2!.length > 0) {
  89. formProps.setError("phone2", { message: t("Please Enter Different Phone No.."), type: "required" })
  90. }
  91. formProps.setError("emergContactPhone", { message: t("Please Enter Different Phone No.."), type: "required" })
  92. }
  93. if (!regex_email.test(data.email)) {
  94. haveError = true
  95. formProps.setError("email", { message: t("Please Enter Correct Email."), type: "required" })
  96. }
  97. if (!data.companyId) {
  98. haveError = true
  99. formProps.setError("companyId", { message: t("Please Enter Company."), type: "required" })
  100. }
  101. if (!data.employType) {
  102. haveError = true
  103. formProps.setError("employType", { message: t("Please Enter Employ Type."), type: "required" })
  104. }
  105. if (!data.salaryId) {
  106. haveError = true
  107. formProps.setError("salaryId", { message: t("Please Enter Salary."), type: "required" })
  108. }
  109. if (data.joinDate &&data.departDate && new Date(data.departDate) <= new Date(data.joinDate)) {
  110. haveError = true
  111. formProps.setError("departDate", { message: t("Depart Date cannot be earlier than Join Date."), type: "required" })
  112. }
  113. if (haveError) {
  114. return
  115. }
  116. console.log("passed")
  117. console.log(data)
  118. await saveStaff(data)
  119. router.replace("/settings/staff")
  120. } catch (e: any) {
  121. console.log(e);
  122. formProps.setError("staffId", { message: t("Please Enter Employ Type."), type: "required" })
  123. let msg = ""
  124. if (e.message === "Duplicated StaffId Found") {
  125. msg = t("Duplicated StaffId Found")
  126. }
  127. setServerError(`${t("An error has occurred. Please try again later.")} ${msg} `);
  128. }
  129. },
  130. [router]
  131. );
  132. const handleCancel = () => {
  133. router.back();
  134. };
  135. return (
  136. <>
  137. <FormProvider {...formProps}>
  138. <Stack
  139. spacing={2}
  140. component="form"
  141. onSubmit={formProps.handleSubmit(onSubmit)}
  142. >
  143. {serverError && (
  144. <Typography variant="body2" color="error" alignSelf="flex-end">
  145. {serverError}
  146. </Typography>
  147. )}
  148. <StaffInfo combos={combos}/>
  149. <Stack direction="row" justifyContent="flex-end" gap={1}>
  150. <Button
  151. variant="outlined"
  152. startIcon={<Close />}
  153. onClick={handleCancel}
  154. >
  155. {t("Cancel")}
  156. </Button>
  157. <Button
  158. variant="contained"
  159. startIcon={<Check />}
  160. type="submit"
  161. // disabled={Boolean(formProps.watch("isGridEditing"))}
  162. >
  163. {t("Confirm")}
  164. </Button>
  165. </Stack>
  166. </Stack>
  167. </FormProvider>
  168. </>
  169. );
  170. };
  171. export default CreateStaff;