Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

217 lignes
6.2 KiB

  1. "use client";
  2. import { useRouter, useSearchParams } from "next/navigation";
  3. import { useCallback, useEffect, useMemo, useState } from "react";
  4. import SearchResults, { Column } from "../SearchResults";
  5. // import { TeamResult } from "@/app/api/team";
  6. import { useTranslation } from "react-i18next";
  7. import { Button, Stack, Tab, Tabs, TabsProps, Typography } from "@mui/material";
  8. import { CreateTeamInputs, saveTeam } from "@/app/api/team/actions";
  9. import {
  10. FieldErrors,
  11. FormProvider,
  12. SubmitHandler,
  13. useForm,
  14. useFormContext,
  15. } from "react-hook-form";
  16. import { Check, Close, Error } from "@mui/icons-material";
  17. import TeamInfo from "../EditTeam/TeamInfo";
  18. import Allocation from "./Allocation";
  19. import { StaffResult } from "@/app/api/staff";
  20. interface desc {
  21. id: number;
  22. name: string;
  23. description: string;
  24. teamLead: number;
  25. }
  26. interface Props {
  27. staff: StaffResult[];
  28. desc: desc[];
  29. // teamLead: StaffResult[]
  30. }
  31. const EditTeam: React.FC<Props> = async ({ staff, desc }) => {
  32. // console.log(desc)
  33. const { t } = useTranslation();
  34. const formProps = useForm<CreateTeamInputs>();
  35. const searchParams = useSearchParams();
  36. const idString = searchParams.get("id");
  37. const [filteredItems, setFilteredItems] = useState<StaffResult[]>();
  38. const [allStaffs, setAllStaffs] = useState<StaffResult[]>();
  39. const [filteredDesc, setFilteredDesc] = useState<string>();
  40. const [filteredName, setFilteredName] = useState<string>();
  41. const [teamLead, setTeamLead] = useState<number>();
  42. const [tabIndex, setTabIndex] = useState(0);
  43. const router = useRouter();
  44. // const [selectedStaff, setSelectedStaff] = useState<typeof filteredItems>(
  45. // initialStaffs.filter((s) => getValues("addStaffIds")?.includes(s.id))
  46. // );
  47. const errors = formProps.formState.errors;
  48. const handleTabChange = useCallback<NonNullable<TabsProps["onChange"]>>(
  49. (_e, newValue) => {
  50. setTabIndex(newValue);
  51. },
  52. []
  53. );
  54. const [serverError, setServerError] = useState("");
  55. const cols = useMemo<Column<StaffResult>[]>(
  56. () => [
  57. { label: t("Staff Id"), name: "staffId" },
  58. { label: t("Name"), name: "staffName" },
  59. // { label: t("Current Position"), name: "posCode" },
  60. ],
  61. [t]
  62. );
  63. useEffect(() => {
  64. let idList: number[] = []
  65. // console.log(desc)
  66. if (idString) {
  67. const filteredTeam = staff.filter(
  68. (item) => {
  69. return (item.teamId === parseInt(idString))}
  70. );
  71. // console.log(filteredTeam)
  72. const tempDesc = desc.filter(
  73. (item) => item.id === parseInt(idString)
  74. )
  75. // const leader = teamLead.filter(
  76. // (staff) => staff.teamId === parseInt(idString)
  77. // )
  78. // console.log(leader)
  79. console.log(tempDesc[0].teamLead)
  80. setTeamLead(tempDesc[0].teamLead)
  81. if (filteredTeam.length > 0) {
  82. const filteredIds: number[] = filteredTeam.map((i) => (
  83. i.id
  84. ))
  85. // const teamLead = tempDesc[0].teamLead
  86. // const index = filteredIds.indexOf(teamLead);
  87. // if (index !== -1) {
  88. // filteredIds.splice(index, 1);
  89. // filteredIds.unshift(teamLead);
  90. // }
  91. idList = filteredIds
  92. // console.log(filteredIds)
  93. }
  94. // console.log(idList)
  95. setFilteredItems(filteredTeam);
  96. formProps.reset({description: tempDesc[0].description, addStaffIds: idList})
  97. setFilteredDesc(tempDesc[0].description)
  98. setFilteredName(tempDesc[0].name)
  99. }
  100. // console.log(staff)
  101. setAllStaffs(staff)
  102. }, [searchParams]);
  103. // useEffect(() => {
  104. // console.log(allStaffs)
  105. // }, [allStaffs]);
  106. const hasErrorsInTab = (
  107. tabIndex: number,
  108. errors: FieldErrors<CreateTeamInputs>
  109. ) => {
  110. switch (tabIndex) {
  111. case 0:
  112. return Object.keys(errors).length > 0;
  113. default:
  114. false;
  115. }
  116. };
  117. const onSubmit = useCallback<SubmitHandler<CreateTeamInputs>>(
  118. async (data) => {
  119. try {
  120. // console.log(data);
  121. const tempData = {
  122. description: data.description,
  123. addStaffIds: data.addStaffIds,
  124. deleteStaffIds: data.deleteStaffIds,
  125. id: parseInt(idString!!)
  126. }
  127. console.log(tempData)
  128. await saveTeam(tempData);
  129. router.replace("/settings/team");
  130. } catch (e) {
  131. console.log(e);
  132. setServerError(t("An error has occurred. Please try again later."));
  133. }
  134. },
  135. [router]
  136. );
  137. return (
  138. <>
  139. {serverError && (
  140. <Typography variant="body2" color="error" alignSelf="flex-end">
  141. {serverError}
  142. </Typography>
  143. )}
  144. <FormProvider {...formProps}>
  145. <Stack
  146. spacing={2}
  147. component="form"
  148. onSubmit={formProps.handleSubmit(onSubmit)}
  149. >
  150. <Typography variant="h4" marginInlineEnd={2}>
  151. {t("Edit Team")} - {filteredName}
  152. </Typography>
  153. <Stack
  154. direction="row"
  155. justifyContent="space-between"
  156. flexWrap="wrap"
  157. rowGap={2}
  158. >
  159. <Tabs
  160. value={tabIndex}
  161. onChange={handleTabChange}
  162. variant="scrollable"
  163. >
  164. <Tab
  165. label={t("Team Info")}
  166. icon={
  167. hasErrorsInTab(0, errors) ? (
  168. <Error sx={{ marginInlineEnd: 1 }} color="error" />
  169. ) : undefined
  170. }
  171. iconPosition="end"
  172. />
  173. <Tab label={t("Staff Allocation")} iconPosition="end" />
  174. </Tabs>
  175. </Stack>
  176. {tabIndex === 0 && <TeamInfo value={filteredDesc!!} />}
  177. {tabIndex === 1 && <Allocation allStaffs={allStaffs!!} teamLead={teamLead!!}/>}
  178. <Stack direction="row" justifyContent="flex-end" gap={1}>
  179. <Button
  180. variant="outlined"
  181. startIcon={<Close />}
  182. // onClick={handleCancel}
  183. >
  184. {t("Cancel")}
  185. </Button>
  186. <Button
  187. variant="contained"
  188. startIcon={<Check />}
  189. type="submit"
  190. // disabled={Boolean(formProps.watch("isGridEditing"))}
  191. >
  192. {t("Confirm")}
  193. </Button>
  194. </Stack>
  195. </Stack>
  196. </FormProvider>
  197. </>
  198. );
  199. };
  200. export default EditTeam;