25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

131 satır
3.6 KiB

  1. "use client";
  2. import { CreateGroupInputs, auth, saveGroup } from "@/app/api/group/actions";
  3. import { useRouter } from "next/navigation";
  4. import { useCallback, useState } from "react";
  5. import { FieldErrors, FormProvider, SubmitHandler, useForm } from "react-hook-form";
  6. import { useTranslation } from "react-i18next";
  7. import { Button, Stack, Tab, Tabs, TabsProps, Typography } from "@mui/material";
  8. import { Check, Close, Error } from "@mui/icons-material";
  9. import GroupInfo from "./GroupInfo";
  10. import AuthorityAllocation from "./AuthorityAllocation";
  11. import UserAllocation from "./UserAllocation";
  12. import { UserResult } from "@/app/api/user";
  13. interface Props {
  14. auth?: auth[]
  15. users?: UserResult[]
  16. }
  17. const CreateGroup: React.FC<Props> = ({ auth, users }) => {
  18. const formProps = useForm<CreateGroupInputs>();
  19. const [serverError, setServerError] = useState("");
  20. const router = useRouter();
  21. const [tabIndex, setTabIndex] = useState(0);
  22. const { t } = useTranslation();
  23. const errors = formProps.formState.errors;
  24. const onSubmit = useCallback<SubmitHandler<CreateGroupInputs>>(
  25. async (data) => {
  26. try {
  27. console.log(data);
  28. const postData = {
  29. ...data,
  30. removeUserIds: [],
  31. removeAuthIds: [],
  32. }
  33. console.log(postData)
  34. await saveGroup(postData)
  35. router.replace("/settings/group")
  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<CreateGroupInputs>,
  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("Group 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("Authority Allocation")} iconPosition="end" />
  86. <Tab label={t("User Allocation")} iconPosition="end" />
  87. </Tabs>
  88. {serverError && (
  89. <Typography variant="body2" color="error" alignSelf="flex-end">
  90. {serverError}
  91. </Typography>
  92. )}
  93. {tabIndex === 0 && <GroupInfo/>}
  94. {tabIndex === 1 && <AuthorityAllocation auth={auth!!}/>}
  95. {tabIndex === 2 && <UserAllocation users={users!!}/>}
  96. <Stack direction="row" justifyContent="flex-end" gap={1}>
  97. <Button
  98. variant="outlined"
  99. startIcon={<Close />}
  100. onClick={handleCancel}
  101. >
  102. {t("Cancel")}
  103. </Button>
  104. <Button
  105. variant="contained"
  106. startIcon={<Check />}
  107. type="submit"
  108. // disabled={Boolean(formProps.watch("isGridEditing"))}
  109. >
  110. {t("Confirm")}
  111. </Button>
  112. </Stack>
  113. </Stack>
  114. </FormProvider>
  115. </>
  116. );
  117. };
  118. export default CreateGroup;