Merge remote-tracking branch 'refs/remotes/origin/staging' into staging
This commit is contained in:
@@ -139,6 +139,11 @@ export type Organization = {
|
|||||||
name: string;
|
name: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Corporate = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type Permisions = {
|
export type Permisions = {
|
||||||
name: string;
|
name: string;
|
||||||
guard_name: string;
|
guard_name: string;
|
||||||
@@ -152,6 +157,7 @@ export type UserAccess = {
|
|||||||
person: Person;
|
person: Person;
|
||||||
role: Role;
|
role: Role;
|
||||||
organization_id: Organization;
|
organization_id: Organization;
|
||||||
|
corporate_id: Corporate;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Person = {
|
export type Person = {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export default function UserAccessCreate() {
|
|||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
const [ currentUserAccess, setCurrentUserAccess ] = useState<UserAccess>();
|
const [ currentUserAccess, setCurrentUserAccess ] = useState<UserAccess>();
|
||||||
const [ roles, setRole ] = useState<any>();
|
const [ roles, setRole ] = useState<any>();
|
||||||
|
const [ corporate, setCorporate ] = useState<any>();
|
||||||
const [ organizations, setOrganization ] = useState<any>();
|
const [ organizations, setOrganization ] = useState<any>();
|
||||||
|
|
||||||
|
|
||||||
@@ -42,8 +43,7 @@ export default function UserAccessCreate() {
|
|||||||
navigate('/404');
|
navigate('/404');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
axios.get('/organization-list')
|
||||||
axios.get('/organization-list')
|
|
||||||
.then((res)=> {
|
.then((res)=> {
|
||||||
setOrganization(res.data)
|
setOrganization(res.data)
|
||||||
})
|
})
|
||||||
@@ -53,8 +53,20 @@ export default function UserAccessCreate() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
axios.get('/corporates')
|
||||||
|
.then((res)=> {
|
||||||
|
setCorporate(res.data)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
if (err.response.status === 404) {
|
||||||
|
navigate('/404');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
|
console.log(corporate, 'test')
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Page title= "User Access">
|
<Page title= "User Access">
|
||||||
@@ -69,7 +81,7 @@ export default function UserAccessCreate() {
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<UserAccessForm isEdit={isEdit} currentUserAccess={currentUserAccess} roles={roles} organizations={organizations}/>
|
<UserAccessForm isEdit={isEdit} currentUserAccess={currentUserAccess} roles={roles} organizations={organizations} corporate={corporate} />
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,25 @@ import * as Yup from 'yup';
|
|||||||
import { LoadingButton } from "@mui/lab";
|
import { LoadingButton } from "@mui/lab";
|
||||||
import { Box, Card, Grid, Stack, Typography } from "@mui/material";
|
import { Box, Card, Grid, Stack, Typography } from "@mui/material";
|
||||||
import { Role, UserAccess, Organization } from "../../../@types/user";
|
import { Role, UserAccess, Organization } from "../../../@types/user";
|
||||||
import { FormProvider, RHFSelect, RHFSwitch, RHFTextField } from "../../../components/hook-form";
|
import { FormProvider, RHFSelect, RHFTextField } from "../../../components/hook-form";
|
||||||
import { useEffect, useMemo } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
import { useSnackbar } from 'notistack';
|
import { useSnackbar } from 'notistack';
|
||||||
import { useNavigate, useParams } from 'react-router-dom';
|
import { useNavigate, useParams } from 'react-router-dom';
|
||||||
import axios from '../../../utils/axios';
|
import axios from '../../../utils/axios';
|
||||||
import palette from '@/theme/palette';
|
import palette from '@/theme/palette';
|
||||||
|
import { Corporate } from '@/@types/corporates';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
isEdit: boolean;
|
isEdit: boolean;
|
||||||
currentUserAccess?: UserAccess;
|
currentUserAccess?: UserAccess;
|
||||||
roles: Role;
|
roles: Role;
|
||||||
organizations: Organization;
|
organizations: Organization;
|
||||||
|
corporate: Corporate;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function AccsessForm({ isEdit, currentUserAccess, roles, organizations }: Props) {
|
export default function AccessForm({ isEdit, currentUserAccess, roles, organizations, corporate }: Props) {
|
||||||
|
|
||||||
const { enqueueSnackbar } = useSnackbar();
|
const { enqueueSnackbar } = useSnackbar();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -28,14 +30,14 @@ export default function AccsessForm({ isEdit, currentUserAccess, roles, organiza
|
|||||||
name: Yup.string().required('Name is required'),
|
name: Yup.string().required('Name is required'),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(currentUserAccess, 'test')
|
|
||||||
const defaultValues = useMemo(
|
const defaultValues = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
name: currentUserAccess?.person?.name || '',
|
name: currentUserAccess?.person?.name || '',
|
||||||
username: currentUserAccess?.username || '',
|
username: currentUserAccess?.username || '',
|
||||||
email: currentUserAccess?.email || '',
|
email: currentUserAccess?.email || '',
|
||||||
roles: currentUserAccess?.role?.id || [],
|
roles: currentUserAccess?.role?.id || '',
|
||||||
organizations: currentUserAccess?.organization_id || [],
|
organizations: currentUserAccess?.organization_id || '',
|
||||||
|
corporates: currentUserAccess?.corporate_id || '',
|
||||||
password: '',
|
password: '',
|
||||||
}),
|
}),
|
||||||
[currentUserAccess]
|
[currentUserAccess]
|
||||||
@@ -58,108 +60,119 @@ export default function AccsessForm({ isEdit, currentUserAccess, roles, organiza
|
|||||||
const {
|
const {
|
||||||
reset,
|
reset,
|
||||||
watch,
|
watch,
|
||||||
control,
|
|
||||||
setValue,
|
|
||||||
getValues,
|
|
||||||
setError,
|
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
setError,
|
||||||
formState: { isSubmitting },
|
formState: { isSubmitting },
|
||||||
} = methods;
|
} = methods;
|
||||||
|
|
||||||
|
// Watch 'roles' field to conditionally render other fields
|
||||||
|
const selectedRole = watch('roles');
|
||||||
|
|
||||||
const onSubmit = async (data: any) => {
|
const onSubmit = async (data: any) => {
|
||||||
|
console.log(data, 'test123');
|
||||||
console.log(data);
|
|
||||||
if (!isEdit) {
|
if (!isEdit) {
|
||||||
await axios
|
await axios
|
||||||
.post('/user/access', data)
|
.post('/user/access', data)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
enqueueSnackbar('User created successfully', { variant: 'success' });
|
enqueueSnackbar('User created successfully', { variant: 'success' });
|
||||||
})
|
navigate('/user-access', { replace: true });
|
||||||
.then((res) => {
|
})
|
||||||
navigate('/user-access', { replace: true });
|
.catch(({ response }) => {
|
||||||
})
|
if (response.status === 422) {
|
||||||
.catch(({ response }) => {
|
for (const [key, value] of Object.entries(response.data.errors)) {
|
||||||
if (response.status === 422) {
|
setError(key, { message: value[0] });
|
||||||
for (const [key, value] of Object.entries(response.data.errors)) {
|
enqueueSnackbar(value[0] ?? 'Failed Processing Request', { variant: 'error' });
|
||||||
setError(key, { message: value[0] });
|
}
|
||||||
enqueueSnackbar(value[0] ?? 'Failed Processing Request', { variant: 'error' });
|
} else {
|
||||||
|
enqueueSnackbar('Create Failed : ' + response.data.message, { variant: 'error' });
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
else {
|
|
||||||
enqueueSnackbar('Create Failed : '+ response.data.message, { variant: 'error' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
await axios
|
await axios
|
||||||
.put('/user/access/' + currentUserAccess?.id, data)
|
.put('/user/access/' + currentUserAccess?.id, data)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
enqueueSnackbar('User updated successfully', { variant: 'success' });
|
enqueueSnackbar('User updated successfully', { variant: 'success' });
|
||||||
})
|
navigate('/user-access', { replace: true });
|
||||||
.then((res) => {
|
|
||||||
navigate('/user-access' , { replace: true });
|
|
||||||
})
|
})
|
||||||
.catch(({ response }) => {
|
.catch(({ response }) => {
|
||||||
enqueueSnackbar('Update Failed : '+ response.data.message, { variant: 'error' });
|
enqueueSnackbar('Update Failed : ' + response.data.message, { variant: 'error' });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionsRoles = roles?.data?.map(item => ({
|
// Map role, organization, and corporate options
|
||||||
|
const optionsRoles = roles?.data?.map((item) => ({
|
||||||
value: item.id,
|
value: item.id,
|
||||||
label: item.name
|
label: item.name,
|
||||||
})) ?? [];
|
})) ?? [];
|
||||||
|
|
||||||
if (optionsRoles.length > 0) {
|
const optionsOrganization = organizations?.data?.map((item) => ({
|
||||||
optionsRoles.unshift({ value: '', label: '' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const optionsOrganization = organizations?.data?.map(item => ({
|
|
||||||
value: item.id,
|
value: item.id,
|
||||||
label: item.name
|
label: item.name,
|
||||||
})) ?? [];
|
})) ?? [];
|
||||||
|
|
||||||
if (optionsOrganization.length > 0) {
|
const optionsCorporate = corporate?.data?.map((item) => ({
|
||||||
optionsOrganization.unshift({ value: '', label: '' });
|
value: item.id,
|
||||||
}
|
label: item.name,
|
||||||
|
})) ?? [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
|
<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
|
||||||
<Box sx={{ px: 2 }}>
|
<Box sx={{ px: 2 }}>
|
||||||
<Grid container spacing={2}>
|
<Grid container spacing={2}>
|
||||||
<Grid item xs={12} sm={12}>
|
<Grid item xs={12} sm={12}>
|
||||||
<Card sx={{ px: 3, py: 4 }}>
|
<Card sx={{ px: 3, py: 4 }}>
|
||||||
<Stack spacing={2}>
|
<Stack spacing={2}>
|
||||||
<Typography variant="h6" color={palette.light.primary.main}>User Access</Typography>
|
<Typography variant="h6" color={palette.light.primary.main}>
|
||||||
<RHFTextField name="name" label="Name" />
|
User Access
|
||||||
<RHFTextField name="username" label="Username" />
|
</Typography>
|
||||||
<RHFTextField type="email" name="email" label="Email" />
|
<RHFTextField name="name" label="Name" />
|
||||||
<RHFTextField type="password" name="password" label="Password" />
|
<RHFTextField name="username" label="Username" />
|
||||||
<RHFSelect name="roles" label="Roles">
|
<RHFTextField type="email" name="email" label="Email" />
|
||||||
{optionsRoles.map((option, index) => (
|
<RHFTextField type="password" name="password" label="Password" />
|
||||||
<option key={index} value={option.value}>
|
|
||||||
{option.label}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</RHFSelect>
|
|
||||||
<RHFSelect name="organizations" label="organizations">
|
|
||||||
{optionsOrganization.map((option, index) => (
|
|
||||||
<option key={index} value={option.value}>
|
|
||||||
{option.label}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</RHFSelect>
|
|
||||||
|
|
||||||
|
{/* Select for roles */}
|
||||||
|
<RHFSelect name="roles" label="Roles">
|
||||||
|
{optionsRoles.map((option, index) => (
|
||||||
|
<option key={index} value={option.value}>
|
||||||
|
{option.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</RHFSelect>
|
||||||
|
|
||||||
<LoadingButton type="submit" variant="contained" size="large" fullWidth={true} loading={isSubmitting}>
|
{/* Conditional rendering based on selectedRole */}
|
||||||
{ isEdit? 'Update' : 'Create' }
|
{selectedRole && (['8'].includes(selectedRole) ? (
|
||||||
</LoadingButton>
|
<RHFSelect name="organizations" label="Organizations">
|
||||||
|
{optionsOrganization.map((option, index) => (
|
||||||
|
<option key={index} value={option.value}>
|
||||||
|
{option.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</RHFSelect>
|
||||||
|
) : ['2'].includes(selectedRole) ? (
|
||||||
|
<RHFSelect name="corporates" label="Corporates">
|
||||||
|
{optionsCorporate.map((option, index) => (
|
||||||
|
<option key={index} value={option.value}>
|
||||||
|
{option.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</RHFSelect>
|
||||||
|
) : null)}
|
||||||
|
|
||||||
</Stack>
|
<LoadingButton
|
||||||
</Card>
|
type="submit"
|
||||||
</Grid>
|
variant="contained"
|
||||||
</Grid>
|
size="large"
|
||||||
</Box>
|
fullWidth={true}
|
||||||
</FormProvider>
|
loading={isSubmitting}
|
||||||
|
>
|
||||||
|
{isEdit ? 'Update' : 'Create'}
|
||||||
|
</LoadingButton>
|
||||||
|
</Stack>
|
||||||
|
</Card>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Box>
|
||||||
|
</FormProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user