|
@@ -3,12 +3,15 @@ import {
|
|
|
createEntityAdapter,
|
|
|
createSlice,
|
|
|
createSelector,
|
|
|
+ PayloadAction,
|
|
|
} from '@reduxjs/toolkit';
|
|
|
import {
|
|
|
Configuration,
|
|
|
- ConsumerGroup,
|
|
|
ConsumerGroupDetails,
|
|
|
+ ConsumerGroupOrdering,
|
|
|
ConsumerGroupsApi,
|
|
|
+ ConsumerGroupsPageResponse,
|
|
|
+ SortOrder,
|
|
|
} from 'generated-sources';
|
|
|
import { BASE_PARAMS } from 'lib/constants';
|
|
|
import { getResponse } from 'lib/errorHandling';
|
|
@@ -19,20 +22,28 @@ import {
|
|
|
RootState,
|
|
|
} from 'redux/interfaces';
|
|
|
import { createFetchingSelector } from 'redux/reducers/loader/selectors';
|
|
|
+import { EntityState } from '@reduxjs/toolkit/src/entities/models';
|
|
|
|
|
|
const apiClientConf = new Configuration(BASE_PARAMS);
|
|
|
export const api = new ConsumerGroupsApi(apiClientConf);
|
|
|
|
|
|
-export const fetchConsumerGroups = createAsyncThunk<
|
|
|
- ConsumerGroup[],
|
|
|
- ClusterName
|
|
|
+export const fetchConsumerGroupsPaged = createAsyncThunk<
|
|
|
+ ConsumerGroupsPageResponse,
|
|
|
+ {
|
|
|
+ clusterName: ClusterName;
|
|
|
+ orderBy?: ConsumerGroupOrdering;
|
|
|
+ sortOrder?: SortOrder;
|
|
|
+ }
|
|
|
>(
|
|
|
- 'consumerGroups/fetchConsumerGroups',
|
|
|
- async (clusterName: ClusterName, { rejectWithValue }) => {
|
|
|
+ 'consumerGroups/fetchConsumerGroupsPaged',
|
|
|
+ async ({ clusterName, orderBy, sortOrder }, { rejectWithValue }) => {
|
|
|
try {
|
|
|
- return await api.getConsumerGroups({
|
|
|
+ const response = await api.getConsumerGroupsPageRaw({
|
|
|
clusterName,
|
|
|
+ orderBy,
|
|
|
+ sortOrder,
|
|
|
});
|
|
|
+ return await response.value();
|
|
|
} catch (error) {
|
|
|
return rejectWithValue(await getResponse(error as Response));
|
|
|
}
|
|
@@ -105,19 +116,45 @@ export const resetConsumerGroupOffsets = createAsyncThunk<
|
|
|
}
|
|
|
}
|
|
|
);
|
|
|
+const SCHEMAS_PAGE_COUNT = 1;
|
|
|
|
|
|
const consumerGroupsAdapter = createEntityAdapter<ConsumerGroupDetails>({
|
|
|
selectId: (consumerGroup) => consumerGroup.groupId,
|
|
|
});
|
|
|
|
|
|
-const consumerGroupsSlice = createSlice({
|
|
|
+interface ConsumerGroupState extends EntityState<ConsumerGroupDetails> {
|
|
|
+ orderBy: ConsumerGroupOrdering | null;
|
|
|
+ sortOrder: SortOrder;
|
|
|
+ totalPages: number;
|
|
|
+}
|
|
|
+
|
|
|
+const initialState: ConsumerGroupState = {
|
|
|
+ orderBy: ConsumerGroupOrdering.NAME,
|
|
|
+ sortOrder: SortOrder.ASC,
|
|
|
+ totalPages: SCHEMAS_PAGE_COUNT,
|
|
|
+ ...consumerGroupsAdapter.getInitialState(),
|
|
|
+};
|
|
|
+
|
|
|
+export const consumerGroupsSlice = createSlice({
|
|
|
name: 'consumerGroups',
|
|
|
- initialState: consumerGroupsAdapter.getInitialState(),
|
|
|
- reducers: {},
|
|
|
+ initialState,
|
|
|
+ reducers: {
|
|
|
+ sortBy: (state, action: PayloadAction<ConsumerGroupOrdering>) => {
|
|
|
+ state.orderBy = action.payload;
|
|
|
+ state.sortOrder =
|
|
|
+ state.orderBy === action.payload && state.sortOrder === SortOrder.ASC
|
|
|
+ ? SortOrder.DESC
|
|
|
+ : SortOrder.ASC;
|
|
|
+ },
|
|
|
+ },
|
|
|
extraReducers: (builder) => {
|
|
|
- builder.addCase(fetchConsumerGroups.fulfilled, (state, { payload }) => {
|
|
|
- consumerGroupsAdapter.setAll(state, payload);
|
|
|
- });
|
|
|
+ builder.addCase(
|
|
|
+ fetchConsumerGroupsPaged.fulfilled,
|
|
|
+ (state, { payload }) => {
|
|
|
+ state.totalPages = payload.pageCount || SCHEMAS_PAGE_COUNT;
|
|
|
+ consumerGroupsAdapter.setAll(state, payload.consumerGroups || []);
|
|
|
+ }
|
|
|
+ );
|
|
|
builder.addCase(fetchConsumerGroupDetails.fulfilled, (state, { payload }) =>
|
|
|
consumerGroupsAdapter.upsertOne(state, payload)
|
|
|
);
|
|
@@ -127,13 +164,17 @@ const consumerGroupsSlice = createSlice({
|
|
|
},
|
|
|
});
|
|
|
|
|
|
+export const { sortBy } = consumerGroupsSlice.actions;
|
|
|
+
|
|
|
+const consumerGroupsState = ({
|
|
|
+ consumerGroups,
|
|
|
+}: RootState): ConsumerGroupState => consumerGroups;
|
|
|
+
|
|
|
export const { selectAll, selectById } =
|
|
|
- consumerGroupsAdapter.getSelectors<RootState>(
|
|
|
- ({ consumerGroups }) => consumerGroups
|
|
|
- );
|
|
|
+ consumerGroupsAdapter.getSelectors<RootState>(consumerGroupsState);
|
|
|
|
|
|
-export const getAreConsumerGroupsFulfilled = createSelector(
|
|
|
- createFetchingSelector('consumerGroups/fetchConsumerGroups'),
|
|
|
+export const getAreConsumerGroupsPagedFulfilled = createSelector(
|
|
|
+ createFetchingSelector('consumerGroups/fetchConsumerGroupsPaged'),
|
|
|
(status) => status === 'fulfilled'
|
|
|
);
|
|
|
|
|
@@ -152,4 +193,19 @@ export const getIsOffsetReseted = createSelector(
|
|
|
(status) => status === 'fulfilled'
|
|
|
);
|
|
|
|
|
|
+export const getConsumerGroupsOrderBy = createSelector(
|
|
|
+ consumerGroupsState,
|
|
|
+ (state) => state.orderBy
|
|
|
+);
|
|
|
+
|
|
|
+export const getConsumerGroupsSortOrder = createSelector(
|
|
|
+ consumerGroupsState,
|
|
|
+ (state) => state.sortOrder
|
|
|
+);
|
|
|
+
|
|
|
+export const getConsumerGroupsTotalPages = createSelector(
|
|
|
+ consumerGroupsState,
|
|
|
+ (state) => state.totalPages
|
|
|
+);
|
|
|
+
|
|
|
export default consumerGroupsSlice.reducer;
|