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.
 
 

127 rivejä
3.4 KiB

  1. "use client";
  2. import {
  3. FieldErrors,
  4. FormProvider,
  5. SubmitErrorHandler,
  6. SubmitHandler,
  7. useForm,
  8. } from "react-hook-form";
  9. import StaffAllocation from "./StaffAllocation";
  10. import { StaffResult } from "@/app/api/staff";
  11. import { CreateTeamInputs, saveTeam } from "@/app/api/team/actions";
  12. import { Button, Stack, Tab, Tabs, TabsProps, Typography } from "@mui/material";
  13. import { Check, Close } from "@mui/icons-material";
  14. import { useCallback, useState } from "react";
  15. import { useRouter, useSearchParams } from "next/navigation";
  16. import { useTranslation } from "react-i18next";
  17. import { Error } from "@mui/icons-material";
  18. import TeamInfo from "./TeamInfo";
  19. export interface Props {
  20. allstaff: StaffResult[];
  21. }
  22. const CreateTeam: React.FC<Props> = ({ allstaff }) => {
  23. const formProps = useForm<CreateTeamInputs>();
  24. const [serverError, setServerError] = useState("");
  25. const router = useRouter();
  26. const [tabIndex, setTabIndex] = useState(0);
  27. const { t } = useTranslation();
  28. const searchParams = useSearchParams()
  29. const errors = formProps.formState.errors;
  30. const onSubmit = useCallback<SubmitHandler<CreateTeamInputs>>(
  31. async (data) => {
  32. try {
  33. console.log(data);
  34. await saveTeam(data);
  35. router.replace("/settings/team");
  36. } catch (e) {
  37. console.log(e);
  38. setServerError(t("An error has occurred. Please try again later."));
  39. }
  40. },
  41. [router]
  42. );
  43. const handleCancel = () => {
  44. router.back();
  45. };
  46. const handleTabChange = useCallback<NonNullable<TabsProps["onChange"]>>(
  47. (_e, newValue) => {
  48. setTabIndex(newValue);
  49. },
  50. [],
  51. );
  52. const hasErrorsInTab = (
  53. tabIndex: number,
  54. errors: FieldErrors<CreateTeamInputs>,
  55. ) => {
  56. switch (tabIndex) {
  57. case 0:
  58. return Object.keys(errors).length > 0;
  59. default:
  60. false;
  61. }
  62. };
  63. return (
  64. <>
  65. <FormProvider {...formProps}>
  66. <Stack
  67. spacing={2}
  68. component="form"
  69. onSubmit={formProps.handleSubmit(onSubmit)}
  70. >
  71. <Tabs
  72. value={tabIndex}
  73. onChange={handleTabChange}
  74. variant="scrollable"
  75. >
  76. <Tab
  77. label={t("Team Info")}
  78. icon={
  79. hasErrorsInTab(0, errors) ? (
  80. <Error sx={{ marginInlineEnd: 1 }} color="error" />
  81. ) : undefined
  82. }
  83. iconPosition="end"
  84. />
  85. <Tab label={t("Staff Allocation")} iconPosition="end" />
  86. </Tabs>
  87. {serverError && (
  88. <Typography variant="body2" color="error" alignSelf="flex-end">
  89. {serverError}
  90. </Typography>
  91. )}
  92. {tabIndex === 0 && <TeamInfo/>}
  93. {tabIndex === 1 && <StaffAllocation allStaffs={allstaff} />}
  94. {/* <StaffAllocation allStaffs={allstaff} /> */}
  95. <Stack direction="row" justifyContent="flex-end" gap={1}>
  96. <Button
  97. variant="outlined"
  98. startIcon={<Close />}
  99. onClick={handleCancel}
  100. >
  101. {t("Cancel")}
  102. </Button>
  103. <Button
  104. variant="contained"
  105. startIcon={<Check />}
  106. type="submit"
  107. // disabled={Boolean(formProps.watch("isGridEditing"))}
  108. >
  109. {t("Confirm")}
  110. </Button>
  111. </Stack>
  112. </Stack>
  113. </FormProvider>
  114. </>
  115. );
  116. };
  117. export default CreateTeam;