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.
 
 

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