MSP-3104 add roleService
This commit is contained in:
1 parent
e1afa417ee
commit
89151474b7
5 files changed
+111
-14
No files matched your search
@@ -1,7 +1,12 @@
|
||||
type PathKeys = 'baseUrl' | 'login' | 'logout'
|
||||
|
||||
type PathKeys = 'baseUrl' | 'getAllRoles' | 'createRole' | 'updateRole' | 'deleteRole' | 'login' | 'logout'
|
||||
export const apiPaths: Record<PathKeys, string> = {
|
||||
baseUrl: '/',
|
||||
baseUrl: '/api/admin',
|
||||
// ======= ROLES ======
|
||||
getAllRoles: '/role/all',
|
||||
createRole: '/role/create',
|
||||
updateRole: '/role/update',
|
||||
deleteRole: '/role/delete',
|
||||
// ======= AUTH ======
|
||||
login: '/api/admin/auth/login',
|
||||
logout: '/api/admin/auth/logout'
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { createApi } from '@reduxjs/toolkit/query/react'
|
||||
|
||||
import { apiPaths } from '@constants/api/apiPaths.ts'
|
||||
|
||||
import { axiosBaseQuery } from '@utils/apiUtils.ts'
|
||||
|
||||
import { NewRoleType, RoleType } from '@type/roles.type.ts'
|
||||
|
||||
|
||||
const roleApi = createApi({
|
||||
reducerPath: 'roleApi',
|
||||
refetchOnFocus: true,
|
||||
tagTypes: ['Roles'],
|
||||
baseQuery: axiosBaseQuery({ baseUrl: apiPaths.baseUrl }),
|
||||
endpoints: (builder) => ({
|
||||
getAllRoles: builder.query<RoleType[], void>({
|
||||
query: () => ({ url: apiPaths.getAllRoles }),
|
||||
providesTags: () => ['Roles']
|
||||
}),
|
||||
createRole: builder.mutation<RoleType, NewRoleType>({
|
||||
query: (newRole) => ({
|
||||
url: apiPaths.createRole,
|
||||
data: newRole
|
||||
}),
|
||||
invalidatesTags: ['Roles']
|
||||
}),
|
||||
updateRole: builder.mutation<void, RoleType>({
|
||||
query: (role) => ({
|
||||
url: apiPaths.updateRole,
|
||||
data: role
|
||||
}),
|
||||
invalidatesTags: ['Roles']
|
||||
}),
|
||||
removeRole: builder.mutation<void, number>({
|
||||
query: (id) => ({
|
||||
url: apiPaths.deleteRole,
|
||||
data: { id }
|
||||
}),
|
||||
invalidatesTags: ['Roles']
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
export default roleApi
|
||||
@@ -3,8 +3,11 @@ import { theme } from 'antd'
|
||||
import { MapToken, SeedToken } from 'antd/es/theme/interface'
|
||||
import { createContext, Dispatch, SetStateAction } from 'react'
|
||||
|
||||
import roleApi from '@services/roleService.ts'
|
||||
|
||||
import profileReducer from './redusers/ProfileSlice.ts'
|
||||
|
||||
|
||||
export interface ContextProps {
|
||||
setTheme: Dispatch<
|
||||
SetStateAction<{ algorithm: (token: SeedToken) => MapToken }>
|
||||
@@ -16,12 +19,15 @@ export const Context = createContext<ContextProps>({
|
||||
})
|
||||
|
||||
const rootReducer = combineReducers({
|
||||
profileReducer
|
||||
profileReducer,
|
||||
[roleApi.reducerPath]: roleApi.reducer
|
||||
})
|
||||
|
||||
export const setupStore = () =>
|
||||
configureStore({
|
||||
reducer: rootReducer
|
||||
reducer: rootReducer,
|
||||
middleware: (getDefaultMiddleware) =>
|
||||
getDefaultMiddleware().concat([roleApi.middleware])
|
||||
})
|
||||
|
||||
export type RootState = ReturnType<typeof rootReducer>
|
||||
|
||||
@@ -11,15 +11,6 @@ export enum PermissionKeys {
|
||||
role_view = 'role_view', // Просмотр экрана ролей
|
||||
}
|
||||
|
||||
export type ConfigRoles = {
|
||||
operator: string[]
|
||||
supervisor: string[]
|
||||
ontologist: string[]
|
||||
businessMonitoring: string[]
|
||||
technicalMonitoring: string[]
|
||||
administratorIS: string[]
|
||||
}
|
||||
|
||||
export type RoleType = {
|
||||
changeMessage?: string
|
||||
createdAt: string
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { BaseQueryFn, retry } from '@reduxjs/toolkit/query/react';
|
||||
|
||||
import { AxiosError, AxiosRequestConfig } from 'axios';
|
||||
import { apiService } from '@services/apiService.ts'
|
||||
import { MSPError } from '@type/index.ts'
|
||||
|
||||
export const axiosBaseQuery =
|
||||
(
|
||||
{ baseUrl }: { baseUrl: string } = { baseUrl: '' },
|
||||
): BaseQueryFn<
|
||||
{
|
||||
url: string;
|
||||
method?: AxiosRequestConfig['method'];
|
||||
data?: AxiosRequestConfig['data'];
|
||||
params?: AxiosRequestConfig['params'];
|
||||
},
|
||||
unknown,
|
||||
unknown
|
||||
> =>
|
||||
async ({ url, method = 'post', data, params }) => {
|
||||
try {
|
||||
const result = await apiService({
|
||||
url: baseUrl + url,
|
||||
method,
|
||||
data,
|
||||
params,
|
||||
});
|
||||
return { data: result.data };
|
||||
} catch (axiosError) {
|
||||
const err = axiosError as AxiosError<MSPError>;
|
||||
return {
|
||||
error: { ...err.response },
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const staggeredBaseQueryWithBailOut = (baseUrl: string) =>
|
||||
retry(
|
||||
async (args: string | AxiosRequestConfig['data'], api, extraOptions) => {
|
||||
const result = await axiosBaseQuery({ baseUrl })(args, api, extraOptions);
|
||||
const error = result.error as AxiosError<MSPError>;
|
||||
if (error && error?.status === 400) {
|
||||
retry.fail(result.error);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
{
|
||||
maxRetries: 5,
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user