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.
 
 

219 lines
6.3 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. console.log(item)
  70. console.log(parseInt(idString))
  71. return (item.teamId === parseInt(idString))}
  72. );
  73. console.log(filteredTeam)
  74. const tempDesc = desc.filter(
  75. (item) => item.id === parseInt(idString)
  76. )
  77. // const leader = teamLead.filter(
  78. // (staff) => staff.teamId === parseInt(idString)
  79. // )
  80. // console.log(leader)
  81. console.log(tempDesc[0].teamLead)
  82. setTeamLead(tempDesc[0].teamLead)
  83. if (filteredTeam.length > 0) {
  84. const filteredIds: number[] = filteredTeam.map((i) => (
  85. i.id
  86. ))
  87. // const teamLead = tempDesc[0].teamLead
  88. // const index = filteredIds.indexOf(teamLead);
  89. // if (index !== -1) {
  90. // filteredIds.splice(index, 1);
  91. // filteredIds.unshift(teamLead);
  92. // }
  93. idList = filteredIds
  94. console.log(filteredIds)
  95. }
  96. console.log(idList)
  97. setFilteredItems(filteredTeam);
  98. formProps.reset({description: tempDesc[0].description, addStaffIds: idList})
  99. setFilteredDesc(tempDesc[0].description)
  100. setFilteredName(tempDesc[0].name)
  101. }
  102. console.log(staff)
  103. setAllStaffs(staff)
  104. }, [searchParams]);
  105. // useEffect(() => {
  106. // console.log(allStaffs)
  107. // }, [allStaffs]);
  108. const hasErrorsInTab = (
  109. tabIndex: number,
  110. errors: FieldErrors<CreateTeamInputs>
  111. ) => {
  112. switch (tabIndex) {
  113. case 0:
  114. return Object.keys(errors).length > 0;
  115. default:
  116. false;
  117. }
  118. };
  119. const onSubmit = useCallback<SubmitHandler<CreateTeamInputs>>(
  120. async (data) => {
  121. try {
  122. console.log(data);
  123. const tempData = {
  124. description: data.description,
  125. addStaffIds: data.addStaffIds,
  126. deleteStaffIds: data.deleteStaffIds,
  127. id: parseInt(idString!!)
  128. }
  129. console.log(tempData)
  130. await saveTeam(tempData);
  131. router.replace("/settings/team");
  132. } catch (e) {
  133. console.log(e);
  134. setServerError(t("An error has occurred. Please try again later."));
  135. }
  136. },
  137. [router]
  138. );
  139. return (
  140. <>
  141. {serverError && (
  142. <Typography variant="body2" color="error" alignSelf="flex-end">
  143. {serverError}
  144. </Typography>
  145. )}
  146. <FormProvider {...formProps}>
  147. <Stack
  148. spacing={2}
  149. component="form"
  150. onSubmit={formProps.handleSubmit(onSubmit)}
  151. >
  152. <Typography variant="h4" marginInlineEnd={2}>
  153. {t("Edit Team")} - {filteredName}
  154. </Typography>
  155. <Stack
  156. direction="row"
  157. justifyContent="space-between"
  158. flexWrap="wrap"
  159. rowGap={2}
  160. >
  161. <Tabs
  162. value={tabIndex}
  163. onChange={handleTabChange}
  164. variant="scrollable"
  165. >
  166. <Tab
  167. label={t("Team Info")}
  168. icon={
  169. hasErrorsInTab(0, errors) ? (
  170. <Error sx={{ marginInlineEnd: 1 }} color="error" />
  171. ) : undefined
  172. }
  173. iconPosition="end"
  174. />
  175. <Tab label={t("Staff Allocation")} iconPosition="end" />
  176. </Tabs>
  177. </Stack>
  178. {tabIndex === 0 && <TeamInfo value={filteredDesc!!} />}
  179. {tabIndex === 1 && <Allocation allStaffs={allStaffs!!} teamLead={teamLead!!}/>}
  180. <Stack direction="row" justifyContent="flex-end" gap={1}>
  181. <Button
  182. variant="outlined"
  183. startIcon={<Close />}
  184. // onClick={handleCancel}
  185. >
  186. {t("Cancel")}
  187. </Button>
  188. <Button
  189. variant="contained"
  190. startIcon={<Check />}
  191. type="submit"
  192. // disabled={Boolean(formProps.watch("isGridEditing"))}
  193. >
  194. {t("Confirm")}
  195. </Button>
  196. </Stack>
  197. </Stack>
  198. </FormProvider>
  199. </>
  200. );
  201. };
  202. export default EditTeam;