role crud fix
This commit is contained in:
@@ -6,16 +6,17 @@ import { toast } from 'react-toastify'
|
|||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
import Button from '@/components/Button'
|
import Button from '@/components/Button'
|
||||||
import Input from '@/components/Input'
|
import Input from '@/components/Input'
|
||||||
import { useCreateRole } from './hooks/useRolesData'
|
import { Checkbox } from '@/components/ui/checkbox'
|
||||||
|
import { useCreateRole, useGetPermissions } from './hooks/useRolesData'
|
||||||
import type { CreateRoleType } from './types/Types'
|
import type { CreateRoleType } from './types/Types'
|
||||||
import { Pages } from '@/config/Pages'
|
import { Pages } from '@/config/Pages'
|
||||||
import type { ErrorType } from '@/helpers/types'
|
import type { ErrorType } from '@/helpers/types'
|
||||||
import { extractErrorMessage } from '@/config/func'
|
import { extractErrorMessage } from '@/config/func'
|
||||||
|
|
||||||
const CreateRole: FC = () => {
|
const CreateRole: FC = () => {
|
||||||
const { mutate: createRole, isPending } = useCreateRole()
|
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
const { mutate: createRole, isPending } = useCreateRole()
|
||||||
|
const { data: permissionsData } = useGetPermissions()
|
||||||
const formik = useFormik<CreateRoleType>({
|
const formik = useFormik<CreateRoleType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
name: '',
|
name: '',
|
||||||
@@ -69,6 +70,31 @@ const CreateRole: FC = () => {
|
|||||||
error_text={formik.touched.name && formik.errors.name ? formik.errors.name : ''}
|
error_text={formik.touched.name && formik.errors.name ? formik.errors.name : ''}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div className='mt-6'>
|
||||||
|
<div className='text-sm mb-3'>دسترسیها</div>
|
||||||
|
{permissionsData?.data && permissionsData.data.length > 0 ? (
|
||||||
|
<div className='flex gap-6 flex-wrap'>
|
||||||
|
{permissionsData.data.map((permission) => (
|
||||||
|
<div key={permission.id} className='flex items-center gap-2'>
|
||||||
|
<Checkbox
|
||||||
|
checked={formik.values.permissionIds.includes(permission.id)}
|
||||||
|
onCheckedChange={(checked) => {
|
||||||
|
const currentIds = formik.values.permissionIds
|
||||||
|
if (checked) {
|
||||||
|
formik.setFieldValue('permissionIds', [...currentIds, permission.id])
|
||||||
|
} else {
|
||||||
|
formik.setFieldValue('permissionIds', currentIds.filter((id) => id !== permission.id))
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<label className='text-xs cursor-pointer'>{permission.title}</label>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className='text-sm text-gray-500'>دسترسیای یافت نشد</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import { toast } from 'react-toastify'
|
|||||||
import { useNavigate, useParams } from 'react-router-dom'
|
import { useNavigate, useParams } from 'react-router-dom'
|
||||||
import Button from '@/components/Button'
|
import Button from '@/components/Button'
|
||||||
import Input from '@/components/Input'
|
import Input from '@/components/Input'
|
||||||
import { useUpdateRole, useGetRoleDetails } from './hooks/useRolesData'
|
import { Checkbox } from '@/components/ui/checkbox'
|
||||||
|
import { useUpdateRole, useGetRoleDetails, useGetPermissions } from './hooks/useRolesData'
|
||||||
import type { CreateRoleType } from './types/Types'
|
import type { CreateRoleType } from './types/Types'
|
||||||
import { Pages } from '@/config/Pages'
|
import { Pages } from '@/config/Pages'
|
||||||
import type { ErrorType } from '@/helpers/types'
|
import type { ErrorType } from '@/helpers/types'
|
||||||
@@ -16,6 +17,7 @@ import PageLoading from '@/components/PageLoading'
|
|||||||
const UpdateRole: FC = () => {
|
const UpdateRole: FC = () => {
|
||||||
const { id } = useParams<{ id: string }>()
|
const { id } = useParams<{ id: string }>()
|
||||||
const { data: roleData, isLoading } = useGetRoleDetails(id || '')
|
const { data: roleData, isLoading } = useGetRoleDetails(id || '')
|
||||||
|
const { data: permissionsData } = useGetPermissions()
|
||||||
const { mutate: updateRole, isPending } = useUpdateRole()
|
const { mutate: updateRole, isPending } = useUpdateRole()
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
@@ -100,6 +102,31 @@ const UpdateRole: FC = () => {
|
|||||||
error_text={formik.touched.name && formik.errors.name ? formik.errors.name : ''}
|
error_text={formik.touched.name && formik.errors.name ? formik.errors.name : ''}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div className='mt-6'>
|
||||||
|
<div className='text-sm mb-3'>دسترسیها</div>
|
||||||
|
{permissionsData?.data && permissionsData.data.length > 0 ? (
|
||||||
|
<div className='flex gap-6 flex-wrap'>
|
||||||
|
{permissionsData.data.map((permission) => (
|
||||||
|
<div key={permission.id} className='flex items-center gap-2'>
|
||||||
|
<Checkbox
|
||||||
|
checked={formik.values.permissionIds.includes(permission.id)}
|
||||||
|
onCheckedChange={(checked) => {
|
||||||
|
const currentIds = formik.values.permissionIds
|
||||||
|
if (checked) {
|
||||||
|
formik.setFieldValue('permissionIds', [...currentIds, permission.id])
|
||||||
|
} else {
|
||||||
|
formik.setFieldValue('permissionIds', currentIds.filter((id) => id !== permission.id))
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<label className='text-xs cursor-pointer'>{permission.title}</label>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className='text-sm text-gray-500'>دسترسیای یافت نشد</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -48,3 +48,10 @@ export const useUpdateRole = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useGetPermissions = () => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["permissions"],
|
||||||
|
queryFn: api.getPermissions,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type {
|
|||||||
CreateRoleType,
|
CreateRoleType,
|
||||||
GetRolesResponseType,
|
GetRolesResponseType,
|
||||||
GetRoleDetailsResponseType,
|
GetRoleDetailsResponseType,
|
||||||
|
GetPermissionsResponseType,
|
||||||
} from "../types/Types";
|
} from "../types/Types";
|
||||||
|
|
||||||
export type GetRolesParams = {
|
export type GetRolesParams = {
|
||||||
@@ -43,3 +44,10 @@ export const updateRole = async (id: string, params: CreateRoleType) => {
|
|||||||
const { data } = await axios.patch(`/admin/roles/${id}`, params);
|
const { data } = await axios.patch(`/admin/roles/${id}`, params);
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getPermissions = async (): Promise<GetPermissionsResponseType> => {
|
||||||
|
const { data } = await axios.get<GetPermissionsResponseType>(
|
||||||
|
`/admin/permissions`
|
||||||
|
);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export type PermissionType = {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
name: string;
|
name: string;
|
||||||
|
title: string;
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -34,3 +35,4 @@ export type RoleType = {
|
|||||||
|
|
||||||
export type GetRolesResponseType = IResponse<RoleType[]>;
|
export type GetRolesResponseType = IResponse<RoleType[]>;
|
||||||
export type GetRoleDetailsResponseType = IResponse<RoleType>;
|
export type GetRoleDetailsResponseType = IResponse<RoleType>;
|
||||||
|
export type GetPermissionsResponseType = IResponse<PermissionType[]>;
|
||||||
|
|||||||
Reference in New Issue
Block a user