2022-01-05 08:32:38 -06:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
|
2022-03-04 05:04:07 -06:00
|
|
|
import {
|
|
|
|
ApiKey,
|
|
|
|
Role,
|
|
|
|
ServiceAccountDTO,
|
|
|
|
ServiceAccountFilter,
|
|
|
|
ServiceAccountProfileState,
|
|
|
|
ServiceAccountsState,
|
|
|
|
} from 'app/types';
|
2022-01-05 08:32:38 -06:00
|
|
|
|
2022-03-04 05:04:07 -06:00
|
|
|
// serviceAccountsProfilePage
|
2022-01-19 10:03:45 -06:00
|
|
|
export const initialStateProfile: ServiceAccountProfileState = {
|
|
|
|
serviceAccount: {} as ServiceAccountDTO,
|
|
|
|
isLoading: true,
|
2022-02-08 05:35:15 -06:00
|
|
|
tokens: [] as ApiKey[],
|
2022-01-19 10:03:45 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export const serviceAccountProfileSlice = createSlice({
|
|
|
|
name: 'serviceaccount',
|
|
|
|
initialState: initialStateProfile,
|
|
|
|
reducers: {
|
|
|
|
serviceAccountLoaded: (state, action: PayloadAction<ServiceAccountDTO>): ServiceAccountProfileState => {
|
|
|
|
return { ...state, serviceAccount: action.payload, isLoading: false };
|
|
|
|
},
|
2022-02-08 05:35:15 -06:00
|
|
|
serviceAccountTokensLoaded: (state, action: PayloadAction<ApiKey[]>): ServiceAccountProfileState => {
|
|
|
|
return { ...state, tokens: action.payload, isLoading: false };
|
|
|
|
},
|
2022-01-19 10:03:45 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-04 05:04:07 -06:00
|
|
|
export const serviceAccountProfileReducer = serviceAccountProfileSlice.reducer;
|
|
|
|
export const { serviceAccountLoaded, serviceAccountTokensLoaded } = serviceAccountProfileSlice.actions;
|
|
|
|
|
|
|
|
// serviceAccountsListPage
|
|
|
|
export const initialStateList: ServiceAccountsState = {
|
|
|
|
serviceAccounts: [] as ServiceAccountDTO[],
|
|
|
|
isLoading: true,
|
|
|
|
builtInRoles: {},
|
|
|
|
roleOptions: [],
|
|
|
|
serviceAccountToRemove: null,
|
|
|
|
query: '',
|
|
|
|
page: 0,
|
|
|
|
perPage: 50,
|
|
|
|
totalPages: 1,
|
|
|
|
showPaging: false,
|
2022-03-18 09:50:34 -05:00
|
|
|
filters: [{ name: 'expiredTokens', value: false }],
|
2022-03-04 05:04:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
interface ServiceAccountsFetched {
|
|
|
|
serviceAccounts: ServiceAccountDTO[];
|
|
|
|
perPage: number;
|
|
|
|
page: number;
|
|
|
|
totalCount: number;
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:32:38 -06:00
|
|
|
const serviceAccountsSlice = createSlice({
|
|
|
|
name: 'serviceaccounts',
|
2022-03-04 05:04:07 -06:00
|
|
|
initialState: initialStateList,
|
2022-01-05 08:32:38 -06:00
|
|
|
reducers: {
|
2022-03-04 05:04:07 -06:00
|
|
|
serviceAccountsFetched: (state, action: PayloadAction<ServiceAccountsFetched>): ServiceAccountsState => {
|
|
|
|
const { totalCount, perPage, ...rest } = action.payload;
|
|
|
|
const totalPages = Math.ceil(totalCount / perPage);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...rest,
|
|
|
|
totalPages,
|
|
|
|
perPage,
|
|
|
|
showPaging: totalPages > 1,
|
|
|
|
isLoading: false,
|
|
|
|
};
|
2022-01-05 08:32:38 -06:00
|
|
|
},
|
2022-03-04 05:04:07 -06:00
|
|
|
serviceAccountsFetchBegin: (state) => {
|
|
|
|
return { ...state, isLoading: true };
|
2022-01-05 08:32:38 -06:00
|
|
|
},
|
2022-03-04 05:04:07 -06:00
|
|
|
serviceAccountsFetchEnd: (state) => {
|
|
|
|
return { ...state, isLoading: false };
|
2022-01-05 08:32:38 -06:00
|
|
|
},
|
2022-02-10 07:04:07 -06:00
|
|
|
acOptionsLoaded: (state, action: PayloadAction<Role[]>): ServiceAccountsState => {
|
|
|
|
return { ...state, roleOptions: action.payload };
|
|
|
|
},
|
|
|
|
builtInRolesLoaded: (state, action: PayloadAction<Record<string, Role[]>>): ServiceAccountsState => {
|
|
|
|
return { ...state, builtInRoles: action.payload };
|
|
|
|
},
|
2022-02-10 11:21:04 -06:00
|
|
|
serviceAccountToRemoveLoaded: (state, action: PayloadAction<ServiceAccountDTO | null>): ServiceAccountsState => {
|
|
|
|
return { ...state, serviceAccountToRemove: action.payload };
|
|
|
|
},
|
2022-03-04 05:04:07 -06:00
|
|
|
queryChanged: (state, action: PayloadAction<string>) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
query: action.payload,
|
|
|
|
page: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
pageChanged: (state, action: PayloadAction<number>) => ({
|
|
|
|
...state,
|
|
|
|
page: action.payload,
|
|
|
|
}),
|
|
|
|
filterChanged: (state, action: PayloadAction<ServiceAccountFilter>) => {
|
|
|
|
const { name, value } = action.payload;
|
|
|
|
|
|
|
|
if (state.filters.some((filter) => filter.name === name)) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
filters: state.filters.map((filter) => (filter.name === name ? { ...filter, value } : filter)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
filters: [...state.filters, action.payload],
|
|
|
|
};
|
|
|
|
},
|
2022-01-05 08:32:38 -06:00
|
|
|
},
|
|
|
|
});
|
2022-03-04 05:04:07 -06:00
|
|
|
export const serviceAccountsReducer = serviceAccountsSlice.reducer;
|
2022-01-05 08:32:38 -06:00
|
|
|
|
2022-02-10 07:04:07 -06:00
|
|
|
export const {
|
2022-03-04 05:04:07 -06:00
|
|
|
serviceAccountsFetchBegin,
|
|
|
|
serviceAccountsFetchEnd,
|
|
|
|
serviceAccountsFetched,
|
2022-02-10 07:04:07 -06:00
|
|
|
acOptionsLoaded,
|
|
|
|
builtInRolesLoaded,
|
2022-02-10 11:21:04 -06:00
|
|
|
serviceAccountToRemoveLoaded,
|
2022-03-04 05:04:07 -06:00
|
|
|
pageChanged,
|
|
|
|
filterChanged,
|
|
|
|
queryChanged,
|
2022-02-10 07:04:07 -06:00
|
|
|
} = serviceAccountsSlice.actions;
|
2022-01-05 08:32:38 -06:00
|
|
|
|
|
|
|
export default {
|
2022-01-19 10:03:45 -06:00
|
|
|
serviceAccountProfile: serviceAccountProfileReducer,
|
2022-01-05 08:32:38 -06:00
|
|
|
serviceAccounts: serviceAccountsReducer,
|
|
|
|
};
|