"use client"; import { CreateGroupInputs, auth, saveGroup } from "@/app/api/group/actions"; import { useRouter } from "next/navigation"; import { useCallback, useState } from "react"; import { FieldErrors, FormProvider, SubmitHandler, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Button, Stack, Tab, Tabs, TabsProps, Typography } from "@mui/material"; import { Check, Close, Error } from "@mui/icons-material"; import GroupInfo from "./GroupInfo"; import AuthorityAllocation from "./AuthorityAllocation"; import UserAllocation from "./UserAllocation"; import { UserResult } from "@/app/api/user"; interface Props { auth?: auth[] users?: UserResult[] } const CreateGroup: React.FC = ({ auth, users }) => { const formProps = useForm(); const [serverError, setServerError] = useState(""); const router = useRouter(); const [tabIndex, setTabIndex] = useState(0); const { t } = useTranslation("group"); const errors = formProps.formState.errors; const onSubmit = useCallback>( async (data) => { try { console.log(data); const postData = { ...data, removeUserIds: [], removeAuthIds: [], } console.log(postData) await saveGroup(postData) router.replace("/settings/group") } catch (e) { console.log(e); console.log(data) if (!data.addUserIds || data.addUserIds.length == 0) { setServerError(t("Please allocate users.")); } else if (!data.addAuthIds || data.addAuthIds.length == 0) { setServerError(t("Please allocate auths.")); } else { setServerError(t("An error has occurred. Please try again later.")); } } }, [router] ); const handleCancel = () => { router.back(); }; const handleTabChange = useCallback>( (_e, newValue) => { setTabIndex(newValue); }, [] ); const hasErrorsInTab = ( tabIndex: number, errors: FieldErrors, ) => { switch (tabIndex) { case 0: return Object.keys(errors).length > 0; default: false; } }; return ( <> ) : undefined } iconPosition="end" /> {serverError && ( {serverError} )} {tabIndex === 0 && } {tabIndex === 1 && } {tabIndex === 2 && } ); }; export default CreateGroup;