From 8a65a03a13cd5b4c0ce091fb92c95430c272b2f5 Mon Sep 17 00:00:00 2001 From: veselovnk Date: Thu, 11 Apr 2024 16:50:42 +0300 Subject: [PATCH] MSP-3107 add appService --- src/constants/api/apiPaths.ts | 9 ++++++++- src/services/appService.ts | 33 +++++++++++++++++++++++++++++++++ src/stores/index.tsx | 9 +++++++-- src/type/app.type.ts | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/services/appService.ts create mode 100644 src/type/app.type.ts diff --git a/src/constants/api/apiPaths.ts b/src/constants/api/apiPaths.ts index 682273e..e34d694 100644 --- a/src/constants/api/apiPaths.ts +++ b/src/constants/api/apiPaths.ts @@ -1,5 +1,7 @@ type PathKeys = | 'baseUrl' + | 'baseSystemUrl' + | 'baseConfigUrl' | 'getAllRoles' | 'createRole' | 'updateRole' @@ -7,8 +9,11 @@ type PathKeys = | 'login' | 'logout' | 'getProfile' + | 'getSystemTree' export const apiPaths: Record = { baseUrl: '/api/admin', + baseSystemUrl: '/api/system', + baseConfigUrl: '/api/config', // ======= ROLES ====== getAllRoles: '/role/all', createRole: '/role/create', @@ -18,5 +23,7 @@ export const apiPaths: Record = { login: '/api/admin/auth/login', logout: '/api/admin/auth/logout', // ======= PROFILE ====== - getProfile: '/api/admin/user/get_profile' + getProfile: '/api/admin/user/get_profile', + // ======= APPLICATIONS ====== + getSystemTree: '/application/get_system_tree' } diff --git a/src/services/appService.ts b/src/services/appService.ts new file mode 100644 index 0000000..eec1c33 --- /dev/null +++ b/src/services/appService.ts @@ -0,0 +1,33 @@ +import { createApi } from '@reduxjs/toolkit/query/react' + +import { apiPaths } from '@constants/api/apiPaths.ts' + +import { axiosBaseQuery } from '@utils/apiUtils.ts' + +import { AppApiResponseType, DomainType } from '@type/app.type.ts' + + +const appApi = createApi({ + reducerPath: 'appService', + refetchOnFocus: true, + tagTypes: ['SystemTreeApps'], + baseQuery: axiosBaseQuery({ baseUrl: apiPaths.baseSystemUrl }), + endpoints: (builder) => ({ + getSystemTree: builder.query({ + query: () => ({ url: apiPaths.getSystemTree }), + transformResponse: (response: DomainType[]) => { + const apps = response.flatMap(({ services }) => + services.flatMap(({ apps }) => apps) + ) + + return { + originalResponse: response, + appList: apps + } + }, + providesTags: () => ['SystemTreeApps'] + }) + }) +}) + +export default appApi diff --git a/src/stores/index.tsx b/src/stores/index.tsx index c9fcea7..f313980 100644 --- a/src/stores/index.tsx +++ b/src/stores/index.tsx @@ -3,6 +3,7 @@ import { theme } from 'antd' import { MapToken, SeedToken } from 'antd/es/theme/interface' import { createContext, Dispatch, SetStateAction } from 'react' +import appApi from '@services/appService.ts' import roleApi from '@services/roleService.ts' import profileReducer from './redusers/ProfileSlice.ts' @@ -20,14 +21,18 @@ export const Context = createContext({ const rootReducer = combineReducers({ profileReducer, - [roleApi.reducerPath]: roleApi.reducer + [roleApi.reducerPath]: roleApi.reducer, + [appApi.reducerPath]: appApi.reducer, }) export const setupStore = () => configureStore({ reducer: rootReducer, middleware: (getDefaultMiddleware) => - getDefaultMiddleware().concat([roleApi.middleware]) + getDefaultMiddleware().concat( + roleApi.middleware, + appApi.middleware, + ) }) export type RootState = ReturnType diff --git a/src/type/app.type.ts b/src/type/app.type.ts new file mode 100644 index 0000000..138ddeb --- /dev/null +++ b/src/type/app.type.ts @@ -0,0 +1,33 @@ +interface TokenType { + appId: boolean + createdAt: string + expireTime: boolean + token: string +} + +export interface SystemTreeAppType { + description: string + id: boolean + name: string + tokens: TokenType[] + type: string +} + +interface SystemServiceType { + apps: SystemTreeAppType[] + description: string + id: boolean + name: string +} + +export interface DomainType { + description: string + id: boolean + name: string + services: SystemServiceType[] +} + +export interface AppApiResponseType { + originalResponse: DomainType[] + appList: SystemTreeAppType[] +}