import { useMutation } from '@apollo/client'; import LoadingButton from '@mui/lab/LoadingButton'; import Grid from '@mui/material/Grid'; import Stack from '@mui/material/Stack'; import MuiTextField from '@mui/material/TextField'; import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar'; import * as React from 'react'; import { useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; import Can from 'components/Can'; import Container from 'components/Container'; import ControlledAutocomplete from 'components/ControlledAutocomplete'; import Form from 'components/Form'; import PageTitle from 'components/PageTitle'; import TextField from 'components/TextField'; import * as URLS from 'config/urls'; import { CREATE_USER } from 'graphql/mutations/create-user.ee'; import useFormatMessage from 'hooks/useFormatMessage'; import useRoles from 'hooks/useRoles.ee'; function generateRoleOptions(roles) { return roles?.map(({ name: label, id: value }) => ({ label, value })); } export default function CreateUser() { const navigate = useNavigate(); const formatMessage = useFormatMessage(); const [createUser, { loading }] = useMutation(CREATE_USER); const { data, loading: isRolesLoading } = useRoles(); const roles = data?.data; const enqueueSnackbar = useEnqueueSnackbar(); const queryClient = useQueryClient(); const handleUserCreation = async (userData) => { try { await createUser({ variables: { input: { fullName: userData.fullName, password: userData.password, email: userData.email, role: { id: userData.role?.id, }, }, }, }); queryClient.invalidateQueries({ queryKey: ['admin', 'users'] }); enqueueSnackbar(formatMessage('createUser.successfullyCreated'), { variant: 'success', persist: true, SnackbarProps: { 'data-test': 'snackbar-create-user-success', }, }); navigate(URLS.USERS); } catch (error) { throw new Error('Failed while creating!'); } }; return ( {formatMessage('createUserPage.title')}
( )} loading={isRolesLoading} /> {formatMessage('createUser.submit')}
); }