reducers.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import {
  3. LdapConnectionInfo,
  4. LdapError,
  5. LdapState,
  6. LdapUser,
  7. SyncInfo,
  8. UserAdminError,
  9. UserAdminState,
  10. UserDTO,
  11. UserOrg,
  12. UserSession,
  13. UserListAdminState,
  14. UserFilter,
  15. } from 'app/types';
  16. const initialLdapState: LdapState = {
  17. connectionInfo: [],
  18. syncInfo: undefined,
  19. user: undefined,
  20. connectionError: undefined,
  21. userError: undefined,
  22. };
  23. const ldapSlice = createSlice({
  24. name: 'ldap',
  25. initialState: initialLdapState,
  26. reducers: {
  27. ldapConnectionInfoLoadedAction: (state, action: PayloadAction<LdapConnectionInfo>): LdapState => ({
  28. ...state,
  29. ldapError: undefined,
  30. connectionInfo: action.payload,
  31. }),
  32. ldapFailedAction: (state, action: PayloadAction<LdapError>): LdapState => ({
  33. ...state,
  34. ldapError: action.payload,
  35. }),
  36. ldapSyncStatusLoadedAction: (state, action: PayloadAction<SyncInfo>): LdapState => ({
  37. ...state,
  38. syncInfo: action.payload,
  39. }),
  40. userMappingInfoLoadedAction: (state, action: PayloadAction<LdapUser>): LdapState => ({
  41. ...state,
  42. user: action.payload,
  43. userError: undefined,
  44. }),
  45. userMappingInfoFailedAction: (state, action: PayloadAction<LdapError>): LdapState => ({
  46. ...state,
  47. user: undefined,
  48. userError: action.payload,
  49. }),
  50. clearUserMappingInfoAction: (state, action: PayloadAction<undefined>): LdapState => ({
  51. ...state,
  52. user: undefined,
  53. }),
  54. clearUserErrorAction: (state, action: PayloadAction<undefined>): LdapState => ({
  55. ...state,
  56. userError: undefined,
  57. }),
  58. },
  59. });
  60. export const {
  61. clearUserErrorAction,
  62. clearUserMappingInfoAction,
  63. ldapConnectionInfoLoadedAction,
  64. ldapFailedAction,
  65. ldapSyncStatusLoadedAction,
  66. userMappingInfoFailedAction,
  67. userMappingInfoLoadedAction,
  68. } = ldapSlice.actions;
  69. export const ldapReducer = ldapSlice.reducer;
  70. // UserAdminPage
  71. const initialUserAdminState: UserAdminState = {
  72. user: undefined,
  73. sessions: [],
  74. orgs: [],
  75. isLoading: true,
  76. error: undefined,
  77. };
  78. export const userAdminSlice = createSlice({
  79. name: 'userAdmin',
  80. initialState: initialUserAdminState,
  81. reducers: {
  82. userProfileLoadedAction: (state, action: PayloadAction<UserDTO>): UserAdminState => ({
  83. ...state,
  84. user: action.payload,
  85. }),
  86. userOrgsLoadedAction: (state, action: PayloadAction<UserOrg[]>): UserAdminState => ({
  87. ...state,
  88. orgs: action.payload,
  89. }),
  90. userSessionsLoadedAction: (state, action: PayloadAction<UserSession[]>): UserAdminState => ({
  91. ...state,
  92. sessions: action.payload,
  93. }),
  94. userAdminPageLoadedAction: (state, action: PayloadAction<boolean>): UserAdminState => ({
  95. ...state,
  96. isLoading: !action.payload,
  97. }),
  98. userAdminPageFailedAction: (state, action: PayloadAction<UserAdminError>): UserAdminState => ({
  99. ...state,
  100. error: action.payload,
  101. isLoading: false,
  102. }),
  103. },
  104. });
  105. export const {
  106. userProfileLoadedAction,
  107. userOrgsLoadedAction,
  108. userSessionsLoadedAction,
  109. userAdminPageLoadedAction,
  110. userAdminPageFailedAction,
  111. } = userAdminSlice.actions;
  112. export const userAdminReducer = userAdminSlice.reducer;
  113. // UserListAdminPage
  114. const initialUserListAdminState: UserListAdminState = {
  115. users: [],
  116. query: '',
  117. page: 0,
  118. perPage: 50,
  119. totalPages: 1,
  120. showPaging: false,
  121. filters: [{ name: 'activeLast30Days', value: false }],
  122. isLoading: false,
  123. };
  124. interface UsersFetched {
  125. users: UserDTO[];
  126. perPage: number;
  127. page: number;
  128. totalCount: number;
  129. }
  130. export const userListAdminSlice = createSlice({
  131. name: 'userListAdmin',
  132. initialState: initialUserListAdminState,
  133. reducers: {
  134. usersFetched: (state, action: PayloadAction<UsersFetched>) => {
  135. const { totalCount, perPage, ...rest } = action.payload;
  136. const totalPages = Math.ceil(totalCount / perPage);
  137. return {
  138. ...state,
  139. ...rest,
  140. totalPages,
  141. perPage,
  142. showPaging: totalPages > 1,
  143. isLoading: false,
  144. };
  145. },
  146. usersFetchBegin: (state) => {
  147. return { ...state, isLoading: true };
  148. },
  149. usersFetchEnd: (state) => {
  150. return { ...state, isLoading: false };
  151. },
  152. queryChanged: (state, action: PayloadAction<string>) => ({
  153. ...state,
  154. query: action.payload,
  155. page: 0,
  156. }),
  157. pageChanged: (state, action: PayloadAction<number>) => ({
  158. ...state,
  159. page: action.payload,
  160. }),
  161. filterChanged: (state, action: PayloadAction<UserFilter>) => {
  162. const { name, value } = action.payload;
  163. if (state.filters.some((filter) => filter.name === name)) {
  164. return {
  165. ...state,
  166. page: 0,
  167. filters: state.filters.map((filter) => (filter.name === name ? { ...filter, value } : filter)),
  168. };
  169. }
  170. return {
  171. ...state,
  172. page: 0,
  173. filters: [...state.filters, action.payload],
  174. };
  175. },
  176. },
  177. });
  178. export const { usersFetched, usersFetchBegin, usersFetchEnd, queryChanged, pageChanged, filterChanged } =
  179. userListAdminSlice.actions;
  180. export const userListAdminReducer = userListAdminSlice.reducer;
  181. export default {
  182. ldap: ldapReducer,
  183. userAdmin: userAdminReducer,
  184. userListAdmin: userListAdminReducer,
  185. };