import * as plugins from '../plugins.js'; import * as interfaces from '../../ts_interfaces/index.js'; import { appState } from './shared.js'; import { getActionContext } from './login.js'; export interface IConfigEventState { events: interfaces.data.IOpsConfigEvent[]; total: number; unacknowledgedCount: number; nextCursor?: interfaces.data.IOpsConfigEventCursor; showAcknowledged: boolean; isLoading: boolean; } export const configEventStatePart = await appState.getStatePart( 'configEvents', { events: [], total: 0, unacknowledgedCount: 0, nextCursor: undefined, showAcknowledged: false, isLoading: false, }, 'soft' ); export const fetchConfigEventsAction = configEventStatePart.createAction<{ showAcknowledged?: boolean; cursor?: interfaces.data.IOpsConfigEventCursor; append?: boolean; } | null>(async (statePartArg, dataArg): Promise => { const context = getActionContext(); const currentState = statePartArg.getState()!; if (!context.identity) return currentState; const showAcknowledged = dataArg?.showAcknowledged ?? currentState.showAcknowledged; const listRequest = new plugins.domtools.plugins.typedrequest.TypedRequest< interfaces.requests.IReq_ListConfigEvents >('/typedrequest', 'listConfigEvents'); const response = await listRequest.fire({ identity: context.identity, acknowledged: showAcknowledged ? undefined : false, limit: 200, cursor: dataArg?.cursor, }); const append = dataArg?.append === true && Boolean(dataArg.cursor); const events = append ? Array.from(new Map([ ...currentState.events, ...(response.events || []), ].map((eventArg) => [eventArg.id, eventArg])).values()) : response.events || []; return { ...currentState, events, total: response.total || 0, unacknowledgedCount: response.unacknowledgedCount || 0, nextCursor: response.nextCursor, showAcknowledged, isLoading: false, }; }); export const acknowledgeConfigEventsAction = configEventStatePart.createAction<{ ids: string[]; }>(async (statePartArg, dataArg): Promise => { const context = getActionContext(); const currentState = statePartArg.getState()!; if (!context.identity || dataArg.ids.length === 0) return currentState; const ackRequest = new plugins.domtools.plugins.typedrequest.TypedRequest< interfaces.requests.IReq_AcknowledgeConfigEvents >('/typedrequest', 'acknowledgeConfigEvents'); await ackRequest.fire({ identity: context.identity, ids: dataArg.ids, }); const listRequest = new plugins.domtools.plugins.typedrequest.TypedRequest< interfaces.requests.IReq_ListConfigEvents >('/typedrequest', 'listConfigEvents'); const response = await listRequest.fire({ identity: context.identity, acknowledged: currentState.showAcknowledged ? undefined : false, limit: 200, }); return { ...currentState, events: response.events || [], total: response.total || 0, unacknowledgedCount: response.unacknowledgedCount || 0, nextCursor: response.nextCursor, isLoading: false, }; });