您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EditTeam.tsx 5.5 KiB

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