reducers.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import {
  3. ApiKey,
  4. Role,
  5. ServiceAccountDTO,
  6. ServiceAccountFilter,
  7. ServiceAccountProfileState,
  8. ServiceAccountsState,
  9. } from 'app/types';
  10. // serviceAccountsProfilePage
  11. export const initialStateProfile: ServiceAccountProfileState = {
  12. serviceAccount: {} as ServiceAccountDTO,
  13. isLoading: true,
  14. tokens: [] as ApiKey[],
  15. };
  16. export const serviceAccountProfileSlice = createSlice({
  17. name: 'serviceaccount',
  18. initialState: initialStateProfile,
  19. reducers: {
  20. serviceAccountLoaded: (state, action: PayloadAction<ServiceAccountDTO>): ServiceAccountProfileState => {
  21. return { ...state, serviceAccount: action.payload, isLoading: false };
  22. },
  23. serviceAccountTokensLoaded: (state, action: PayloadAction<ApiKey[]>): ServiceAccountProfileState => {
  24. return { ...state, tokens: action.payload, isLoading: false };
  25. },
  26. },
  27. });
  28. export const serviceAccountProfileReducer = serviceAccountProfileSlice.reducer;
  29. export const { serviceAccountLoaded, serviceAccountTokensLoaded } = serviceAccountProfileSlice.actions;
  30. // serviceAccountsListPage
  31. export const initialStateList: ServiceAccountsState = {
  32. serviceAccounts: [] as ServiceAccountDTO[],
  33. isLoading: true,
  34. builtInRoles: {},
  35. roleOptions: [],
  36. serviceAccountToRemove: null,
  37. query: '',
  38. page: 0,
  39. perPage: 50,
  40. totalPages: 1,
  41. showPaging: false,
  42. filters: [{ name: 'expiredTokens', value: false }],
  43. };
  44. interface ServiceAccountsFetched {
  45. serviceAccounts: ServiceAccountDTO[];
  46. perPage: number;
  47. page: number;
  48. totalCount: number;
  49. }
  50. const serviceAccountsSlice = createSlice({
  51. name: 'serviceaccounts',
  52. initialState: initialStateList,
  53. reducers: {
  54. serviceAccountsFetched: (state, action: PayloadAction<ServiceAccountsFetched>): ServiceAccountsState => {
  55. const { totalCount, perPage, ...rest } = action.payload;
  56. const totalPages = Math.ceil(totalCount / perPage);
  57. return {
  58. ...state,
  59. ...rest,
  60. totalPages,
  61. perPage,
  62. showPaging: totalPages > 1,
  63. isLoading: false,
  64. };
  65. },
  66. serviceAccountsFetchBegin: (state) => {
  67. return { ...state, isLoading: true };
  68. },
  69. serviceAccountsFetchEnd: (state) => {
  70. return { ...state, isLoading: false };
  71. },
  72. acOptionsLoaded: (state, action: PayloadAction<Role[]>): ServiceAccountsState => {
  73. return { ...state, roleOptions: action.payload };
  74. },
  75. builtInRolesLoaded: (state, action: PayloadAction<Record<string, Role[]>>): ServiceAccountsState => {
  76. return { ...state, builtInRoles: action.payload };
  77. },
  78. serviceAccountToRemoveLoaded: (state, action: PayloadAction<ServiceAccountDTO | null>): ServiceAccountsState => {
  79. return { ...state, serviceAccountToRemove: action.payload };
  80. },
  81. queryChanged: (state, action: PayloadAction<string>) => {
  82. return {
  83. ...state,
  84. query: action.payload,
  85. page: 0,
  86. };
  87. },
  88. pageChanged: (state, action: PayloadAction<number>) => ({
  89. ...state,
  90. page: action.payload,
  91. }),
  92. filterChanged: (state, action: PayloadAction<ServiceAccountFilter>) => {
  93. const { name, value } = action.payload;
  94. if (state.filters.some((filter) => filter.name === name)) {
  95. return {
  96. ...state,
  97. filters: state.filters.map((filter) => (filter.name === name ? { ...filter, value } : filter)),
  98. };
  99. }
  100. return {
  101. ...state,
  102. filters: [...state.filters, action.payload],
  103. };
  104. },
  105. },
  106. });
  107. export const serviceAccountsReducer = serviceAccountsSlice.reducer;
  108. export const {
  109. serviceAccountsFetchBegin,
  110. serviceAccountsFetchEnd,
  111. serviceAccountsFetched,
  112. acOptionsLoaded,
  113. builtInRolesLoaded,
  114. serviceAccountToRemoveLoaded,
  115. pageChanged,
  116. filterChanged,
  117. queryChanged,
  118. } = serviceAccountsSlice.actions;
  119. export default {
  120. serviceAccountProfile: serviceAccountProfileReducer,
  121. serviceAccounts: serviceAccountsReducer,
  122. };