(ui): Update Queue state when new list of groups is fetched

This commit is contained in:
Ken Hibino 2022-04-01 10:39:35 -07:00
parent 33e76f263d
commit c8d7da05eb
2 changed files with 27 additions and 2 deletions

View File

@ -9,15 +9,18 @@ export const LIST_GROUPS_ERROR = "LIST_GROUPS_ERROR";
interface ListGroupsBeginAction { interface ListGroupsBeginAction {
type: typeof LIST_GROUPS_BEGIN; type: typeof LIST_GROUPS_BEGIN;
queue: string;
} }
interface ListGroupsSuccessAction { interface ListGroupsSuccessAction {
type: typeof LIST_GROUPS_SUCCESS; type: typeof LIST_GROUPS_SUCCESS;
payload: ListGroupsResponse; payload: ListGroupsResponse;
queue: string;
} }
interface ListGroupsErrorAction { interface ListGroupsErrorAction {
type: typeof LIST_GROUPS_ERROR; type: typeof LIST_GROUPS_ERROR;
queue: string;
error: string; error: string;
} }
@ -29,18 +32,20 @@ export type GroupsActionTypes =
export function listGroupsAsync(qname: string) { export function listGroupsAsync(qname: string) {
return async (dispatch: Dispatch<GroupsActionTypes>) => { return async (dispatch: Dispatch<GroupsActionTypes>) => {
dispatch({ type: LIST_GROUPS_BEGIN }); dispatch({ type: LIST_GROUPS_BEGIN, queue: qname });
try { try {
const response = await listGroups(qname); const response = await listGroups(qname);
dispatch({ dispatch({
type: LIST_GROUPS_SUCCESS, type: LIST_GROUPS_SUCCESS,
payload: response, payload: response,
queue: qname,
}); });
} catch (error) { } catch (error) {
console.error(`listGroupsAsync: ${toErrorStringWithHttpStatus(error)}`); console.error(`listGroupsAsync: ${toErrorStringWithHttpStatus(error)}`);
dispatch({ dispatch({
type: LIST_GROUPS_ERROR, type: LIST_GROUPS_ERROR,
error: toErrorString(error), error: toErrorString(error),
queue: qname,
}); });
} }
}; };

View File

@ -1,3 +1,7 @@
import {
GroupsActionTypes,
LIST_GROUPS_SUCCESS,
} from "../actions/groupsActions";
import { import {
LIST_QUEUES_SUCCESS, LIST_QUEUES_SUCCESS,
LIST_QUEUES_BEGIN, LIST_QUEUES_BEGIN,
@ -81,7 +85,7 @@ const initialState: QueuesState = { data: [], loading: false, error: "" };
function queuesReducer( function queuesReducer(
state = initialState, state = initialState,
action: QueuesActionTypes | TasksActionTypes action: QueuesActionTypes | TasksActionTypes | GroupsActionTypes
): QueuesState { ): QueuesState {
switch (action.type) { switch (action.type) {
case LIST_QUEUES_BEGIN: case LIST_QUEUES_BEGIN:
@ -955,6 +959,22 @@ function queuesReducer(
return { ...state, data: newData }; return { ...state, data: newData };
} }
case LIST_GROUPS_SUCCESS: {
const newData = state.data.map((queueInfo) => {
if (queueInfo.name !== action.queue) {
return queueInfo;
}
return {
...queueInfo,
currentStats: {
...queueInfo.currentStats,
groups: action.payload.groups.length,
},
};
});
return { ...state, data: newData };
}
default: default:
return state; return state;
} }