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.
 
 

332 wiersze
9.9 KiB

  1. "use client";
  2. import EditStaffForm from "../EditStaffForm";
  3. import { useSearchParams } from "next/navigation";
  4. import { useEffect, useState } from "react";
  5. import { BASE_API_URL } from "@/config/api";
  6. import { fetchStaffEdit } from "@/app/api/staff/actions";
  7. import { getServerI18n } from "@/i18n";
  8. import { useTranslation } from "react-i18next";
  9. import { comboProp, fetchCompanyCombo } from "@/app/api/companys/actions";
  10. import { fetchTeamCombo } from "@/app/api/team/actions";
  11. import { fetchDepartmentCombo } from "@/app/api/departments/actions";
  12. import { fetchPositionCombo } from "@/app/api/positions/actions";
  13. import { fetchGradeCombo } from "@/app/api/grades/actions";
  14. import { fetchSkillCombo } from "@/app/api/skill/actions";
  15. import { fetchSalaryCombo } from "@/app/api/salarys/actions";
  16. // import { Field } from "react-hook-form";
  17. interface dataType {
  18. [key: string]: any;
  19. }
  20. interface Options {
  21. id: any;
  22. label: string;
  23. [key: string]: any;
  24. }
  25. // interface Field {
  26. // id: string;
  27. // label: string;
  28. // type: string;
  29. // value: any;
  30. // required?: boolean;
  31. // options?: comboProp[] | undefined | null;
  32. // readOnly?: boolean;
  33. // }
  34. export interface Field {
  35. // subtitle: string;
  36. id: string;
  37. label: string;
  38. type: string;
  39. value?: any;
  40. required?: boolean;
  41. pattern?: string;
  42. message?: string;
  43. options?: Options[] | null;
  44. readOnly?: boolean;
  45. size?: number;
  46. setValue?: any[];
  47. }
  48. const EditStaff: React.FC = async () => {
  49. const searchParams = useSearchParams();
  50. const { t } = useTranslation();
  51. const idString = searchParams.get("id");
  52. const [id, setId] = useState(0);
  53. const [fieldLists, setFieldLists] = useState<Field[][]>();
  54. const [companyCombo, setCompanyCombo] = useState<comboProp[]>();
  55. const [teamCombo, setTeamCombo] = useState<comboProp[]>();
  56. const [departmentCombo, setDepartmentCombo] = useState<comboProp[]>();
  57. const [positionCombo, setPositionCombo] = useState<comboProp[]>();
  58. const [gradeCombo, setGradeCombo] = useState<comboProp[]>();
  59. const [skillCombo, setSkillCombo] = useState<comboProp[]>();
  60. const [salaryCombo, setSalaryCombo] = useState<comboProp[]>();
  61. // const employTypeCombo = [{id: "FT", label: t("FT")}, {id: "PT", label: t("PT")}];
  62. const title = ["", t('Additional Info')]
  63. const employTypeCombo = [
  64. { id: "FT", label: t("FT") },
  65. { id: "PT", label: t("PT") },
  66. ];
  67. const keyOrder1 = [
  68. "staffId",
  69. "name",
  70. "company",
  71. "team",
  72. "department",
  73. "grade",
  74. "skill",
  75. "currentPosition",
  76. "salary",
  77. "hourlyRate",
  78. "employType",
  79. "email",
  80. "phone1",
  81. "phone2",
  82. ];
  83. const keyOrder2 = [
  84. "emergContactName",
  85. "emergContactPhone",
  86. "joinDate",
  87. "joinPosition",
  88. "departDate",
  89. "departPosition",
  90. "departReason",
  91. "remark",
  92. ];
  93. //fetch all combo
  94. useEffect(() => {
  95. fetchCompanyCombo().then((data) => {
  96. if (data) setCompanyCombo(data.records);
  97. });
  98. fetchTeamCombo().then((data) => {
  99. if (data) setTeamCombo(data.records);
  100. });
  101. fetchDepartmentCombo().then((data) => {
  102. if (data) setDepartmentCombo(data.records);
  103. });
  104. fetchPositionCombo().then((data) => {
  105. if (data) setPositionCombo(data.records);
  106. });
  107. fetchGradeCombo().then((data) => {
  108. if (data) setGradeCombo(data.records);
  109. });
  110. fetchSkillCombo().then((data) => {
  111. if (data) setSkillCombo(data.records);
  112. });
  113. fetchSalaryCombo().then((data) => {
  114. if (data) setSalaryCombo(data.records);
  115. });
  116. }, [searchParams]);
  117. useEffect(() => {
  118. let id = 0;
  119. if (idString) {
  120. id = parseInt(idString);
  121. setId(id);
  122. }
  123. fetchStaffEdit(id).then((staff) => {
  124. console.log(staff.data);
  125. const data = staff.data;
  126. ///////////////////// list 1 /////////////////////
  127. const list1 = keyOrder1
  128. .map((key) => {
  129. switch (key) {
  130. case "staffId":
  131. return {
  132. id: `${key}`,
  133. label: t(`Staff ID`),
  134. type: "text",
  135. value: data[key] ?? "",
  136. required: true,
  137. };
  138. case "name":
  139. return {
  140. id: `${key}`,
  141. label: t(`Staff Name`),
  142. type: "text",
  143. value: data[key] ?? "",
  144. };
  145. case "company":
  146. return {
  147. id: `${key}Id`,
  148. label: t(`Company`),
  149. type: "combo-Obj",
  150. options: companyCombo,
  151. value: data[key].id ?? "",
  152. };
  153. case "team":
  154. return {
  155. id: `${key}Id`,
  156. label: t(`Team`),
  157. type: "combo-Obj",
  158. options: teamCombo,
  159. value: data[key].id ?? "",
  160. };
  161. case "department":
  162. return {
  163. id: `${key}Id`,
  164. label: t(`Department`),
  165. type: "combo-Obj",
  166. options: departmentCombo,
  167. value: data[key]?.id ?? "",
  168. // later check
  169. };
  170. case "grade":
  171. return {
  172. id: `${key}Id`,
  173. label: t(`Grade`),
  174. type: "combo-Obj",
  175. options: gradeCombo,
  176. value: data[key] !== null ? data[key].id ?? "" : "",
  177. };
  178. case "skill":
  179. return {
  180. id: `${key}SetId`,
  181. label: t(`Skillset`),
  182. type: "combo-Obj",
  183. options: skillCombo,
  184. value: data[key] !== null ? data[key].id ?? "" : "",
  185. };
  186. case "currentPosition":
  187. return {
  188. id: `${key}Id`,
  189. label: t(`Current Position`),
  190. type: "combo-Obj",
  191. options: positionCombo,
  192. value: data[key].id ?? "",
  193. };
  194. case "salary":
  195. return {
  196. id: `salaryId`,
  197. label: t(`Salary Point`),
  198. type: "combo-Obj",
  199. options: salaryCombo,
  200. value: data[key] !== null ? data[key].id ?? "" : "",
  201. };
  202. // case "hourlyRate":
  203. // return {
  204. // id: `${key}`,
  205. // label: t(`hourlyRate`),
  206. // type: "text",
  207. // value: "",
  208. // // value: data[key],
  209. // readOnly: true,
  210. // };
  211. case "employType":
  212. return {
  213. id: `${key}`,
  214. label: t(`Employ Type`),
  215. type: "combo-Obj",
  216. options: employTypeCombo,
  217. value: data[key] ?? "",
  218. };
  219. case "email":
  220. return {
  221. id: `${key}`,
  222. label: t(`Email`),
  223. type: "text",
  224. value: data[key] ?? "",
  225. pattern: "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$",
  226. message: t("input matching format"),
  227. };
  228. case "phone1":
  229. return {
  230. id: `${key}`,
  231. label: t(`Phone1`),
  232. type: "text",
  233. // pattern: "^\\d{8}$",
  234. message: t("input correct phone no."),
  235. value: data[key] ?? "",
  236. };
  237. case "phone2":
  238. return {
  239. id: `${key}`,
  240. label: t(`Phone2`),
  241. type: "text",
  242. // pattern: "^\\d{8}$",
  243. message: t("input correct phone no."),
  244. value: data[key] ?? "",
  245. } as Field;
  246. default:
  247. return null;
  248. }
  249. }).filter((item): item is Field => item !== null);
  250. ///////////////////// list 2 /////////////////////
  251. const list2 = keyOrder2
  252. .map((key) => {
  253. switch (key) {
  254. case "emergContactName":
  255. return {
  256. id: `${key}`,
  257. label: t(`Emergency Contact Name`),
  258. type: "text",
  259. value: data[key] ?? "",
  260. } as Field;
  261. case "emergContactPhone":
  262. return {
  263. id: `${key}`,
  264. label: t(`Emergency Contact Phonee`),
  265. type: "text",
  266. // pattern: "^\\d{8}$",
  267. message: t("input correct phone no."),
  268. value: data[key] ?? "",
  269. } as Field;
  270. case "joinDate":
  271. return {
  272. id: `${key}`,
  273. label: t(`Join Date`),
  274. type: "multiDate",
  275. value: data[key] ?? "",
  276. } as Field;
  277. case "joinPosition":
  278. return {
  279. id: `${key}Id`,
  280. label: t(`Join Position`),
  281. type: "combo-Obj",
  282. options: positionCombo,
  283. value: data[key].id ?? "",
  284. } as Field;
  285. case "departDate":
  286. return {
  287. id: `${key}`,
  288. label: t(`Depart Date`),
  289. type: "multiDate",
  290. value: data[key] ?? "",
  291. } as Field;
  292. case "departReason":
  293. return {
  294. id: `${key}`,
  295. label: t(`Depart Reason`),
  296. type: "text",
  297. value: data[key] ?? "",
  298. } as Field;
  299. case "remark":
  300. return {
  301. id: `remark`,
  302. label: t(`Remark`),
  303. type: "remarks",
  304. value: data[key] ?? "",
  305. } as Field;
  306. default:
  307. return null;
  308. }
  309. }).filter((item): item is Field => item !== null);
  310. console.log(list2);
  311. console.log([list1]);
  312. setFieldLists([list1,list2]);
  313. });
  314. }, [companyCombo]);
  315. return (
  316. <>
  317. {/* {console.log(fieldLists)} */}
  318. <EditStaffForm Title={title} id={id} fieldLists={fieldLists as Field[][] || [[]]} />
  319. </>
  320. );
  321. };
  322. export default EditStaff;