No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

320 líneas
7.8 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 { 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 { fetchTeamCombo } from "@/app/api/team/actions";
  24. // import { fetchDepartmentCombo } from "@/app/api/departments/actions";
  25. // import { fetchPositionCombo } from "@/app/api/positions/actions";
  26. // import { fetchGradeCombo } from "@/app/api/grades/actions";
  27. // import { fetchSkillCombo } from "@/app/api/skill/actions";
  28. // import { fetchSalaryCombo } from "@/app/api/salarys/actions";
  29. interface Field {
  30. // subtitle: string;
  31. id: string;
  32. label: string;
  33. type: string;
  34. value?: any;
  35. required?: boolean;
  36. pattern?: string;
  37. message?: string;
  38. options?: any[];
  39. readOnly?: boolean;
  40. }
  41. interface formProps {
  42. Title?: string[];
  43. // fieldLists: Field[][];
  44. }
  45. export interface comboItem {
  46. company: comboProp[];
  47. team: comboProp[];
  48. department: comboProp[];
  49. position: comboProp[];
  50. grade: comboProp[];
  51. skill: comboProp[];
  52. salary: comboProp[];
  53. }
  54. const CreateStaff: React.FC<formProps> = ({ Title }) => {
  55. // const router = useRouter();
  56. const { t } = useTranslation();
  57. const [companyCombo, setCompanyCombo] = useState<comboProp[]>();
  58. const [teamCombo, setTeamCombo] = useState<comboProp[]>();
  59. const [departmentCombo, setDepartmentCombo] = useState<comboProp[]>();
  60. const [positionCombo, setPositionCombo] = useState<comboProp[]>();
  61. const [gradeCombo, setGradeCombo] = useState<comboProp[]>();
  62. const [skillCombo, setSkillCombo] = useState<comboProp[]>();
  63. const [salaryCombo, setSalaryCombo] = useState<comboProp[]>();
  64. // const [serverError, setServerError] = useState("");
  65. let comboItem: comboItem = {
  66. company: [],
  67. team: [],
  68. department: [],
  69. position: [],
  70. grade: [],
  71. skill: [],
  72. salary: [],
  73. };
  74. const fetchCompany = async () => {
  75. await fetchCompanyCombo().then((data) => {
  76. if (data) setCompanyCombo(data.records);
  77. });
  78. }
  79. const fetchTeam = async () => {
  80. await fetchTeamCombo().then((data) => {
  81. if (data) setTeamCombo(data.records);
  82. });
  83. }
  84. const fetchDepartment = async () => {
  85. await fetchDepartmentCombo().then((data) => {
  86. if (data) setDepartmentCombo(data.records);
  87. });
  88. }
  89. const fetchPosition = async () => {
  90. await fetchPositionCombo().then((data) => {
  91. if (data) setPositionCombo(data.records);
  92. });
  93. }
  94. const fetchGrade = async () => {
  95. await fetchGradeCombo().then((data) => {
  96. if (data) setGradeCombo(data.records);
  97. });
  98. }
  99. const fetchSkill = async () => {
  100. await fetchSkillCombo().then((data) => {
  101. if (data) setSkillCombo(data.records);
  102. });
  103. }
  104. const fetchSalary = async () => {
  105. await fetchSalaryCombo().then((data) => {
  106. if (data) setSalaryCombo(data.records);
  107. });
  108. }
  109. useEffect(() => {
  110. fetchCompany()
  111. fetchTeam()
  112. fetchDepartment()
  113. fetchPosition()
  114. fetchGrade()
  115. fetchSkill()
  116. fetchSalary()
  117. }, []);
  118. useEffect(() => {
  119. if(!companyCombo)
  120. fetchCompany()
  121. if(!teamCombo)
  122. fetchTeam()
  123. if(!departmentCombo)
  124. fetchDepartment()
  125. if(!positionCombo)
  126. fetchPosition()
  127. if(!gradeCombo)
  128. fetchGrade()
  129. if(!skillCombo)
  130. fetchSkill()
  131. if(!salaryCombo)
  132. fetchSalary()
  133. }, [companyCombo, teamCombo, departmentCombo, positionCombo, gradeCombo, skillCombo, salaryCombo]);
  134. // useEffect(() => {
  135. // console.log(companyCombo)
  136. // }, [companyCombo]);
  137. const fieldLists: Field[][] = [
  138. [
  139. {
  140. id: "staffId",
  141. label: t("Staff ID"),
  142. type: "text",
  143. value: "",
  144. required: true,
  145. },
  146. {
  147. id: "name",
  148. label: t("Staff Name"),
  149. type: "text",
  150. value: "",
  151. required: true,
  152. },
  153. {
  154. id: "companyId",
  155. label: t("Company"),
  156. type: "combo-Obj",
  157. options: companyCombo,
  158. required: true,
  159. },
  160. {
  161. id: "teamId",
  162. label: t("Team"),
  163. type: "combo-Obj",
  164. options: teamCombo,
  165. required: false,
  166. },
  167. {
  168. id: "departmentId",
  169. label: t("Department"),
  170. type: "combo-Obj",
  171. options: departmentCombo,
  172. required: true,
  173. },
  174. {
  175. id: "gradeId",
  176. label: t("Grade"),
  177. type: "combo-Obj",
  178. options: gradeCombo,
  179. required: false,
  180. },
  181. {
  182. id: "skillSetId",
  183. label: t("Skillset"),
  184. type: "combo-Obj",
  185. options: skillCombo,
  186. required: false,
  187. },
  188. {
  189. id: "currentPositionId",
  190. label: t("Current Position"),
  191. type: "combo-Obj",
  192. options: positionCombo,
  193. required: true,
  194. },
  195. {
  196. id: "salaryId",
  197. label: t("Salary Point"),
  198. type: "combo-Obj",
  199. options: salaryCombo,
  200. required: true,
  201. },
  202. // {
  203. // id: "hourlyRate",
  204. // label: t("Hourly Rate"),
  205. // type: "numeric-testing",
  206. // value: "",
  207. // required: false,
  208. // },
  209. {
  210. id: "employType",
  211. label: t("Employ Type"),
  212. type: "combo-Obj",
  213. options: [{id: "FT", label: t("FT")}, {id: "PT", label: t("PT")}],
  214. value: "",
  215. required: true,
  216. },
  217. {
  218. id: "email",
  219. label: t("Email"),
  220. type: "text",
  221. value: "",
  222. pattern: "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$",
  223. message: t("input matching format"),
  224. required: true,
  225. },
  226. {
  227. id: "phone1",
  228. label: t("Phone1"),
  229. type: "text",
  230. value: "",
  231. // pattern: "^\\d{8}$",
  232. message: t("input correct phone no."),
  233. required: true,
  234. },
  235. {
  236. id: "phone2",
  237. label: t("Phone2"),
  238. type: "text",
  239. value: "",
  240. // pattern: "^\\d{8}$",
  241. message: t("input correct phone no."),
  242. required: false,
  243. },
  244. ],
  245. [
  246. {
  247. id: "emergContactName",
  248. label: t("Emergency Contact Name"),
  249. type: "text",
  250. value: "",
  251. required: true,
  252. },
  253. {
  254. id: "emergContactPhone",
  255. label: t("Emergency Contact Phone"),
  256. type: "text",
  257. value: "",
  258. // pattern: "^\\d{8}$",
  259. message: t("input correct phone no."),
  260. required: true,
  261. },
  262. {
  263. id: "joinDate",
  264. label: t("Join Date"),
  265. type: "multiDate",
  266. value: "",
  267. required: true,
  268. },
  269. {
  270. id: "joinPositionId",
  271. label: t("Join Position"),
  272. type: "combo-Obj",
  273. options: positionCombo,
  274. required: true,
  275. },
  276. {
  277. id: "departDate",
  278. label: t("Depart Date"),
  279. type: "multiDate",
  280. value: "",
  281. },
  282. {
  283. id: "departReason",
  284. label: t("Depart Reason"),
  285. type: "text",
  286. value: "",
  287. },
  288. {
  289. id: "remark",
  290. label: t("Remark"),
  291. type: "remarks",
  292. value: "",
  293. },
  294. ]
  295. ];
  296. return (
  297. <>
  298. <CreateStaffForm Title={Title} fieldLists={fieldLists}/>
  299. </>
  300. );
  301. };
  302. export default CreateStaff;