diff --git a/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStartDialog.tsx b/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStartDialog.tsx index 97a6e4e030..b74ac783ef 100644 --- a/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStartDialog.tsx +++ b/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStartDialog.tsx @@ -18,7 +18,7 @@ import React from 'react'; import { Dialog } from '@uikit'; import { useDispatch, useStore } from '@hooks'; -import { closeStartRuleDialog, startRule } from '@store/adh/rules/rulesActionsSlice'; +import { closeStartRuleDialog, startRuleWithUpdate } from '@store/adh/rules/rulesActionsSlice'; const RuleStartDialog: React.FC = () => { const dispatch = useDispatch(); @@ -30,7 +30,7 @@ const RuleStartDialog: React.FC = () => { }; const handleDelete = () => { if (rule) { - dispatch(startRule(rule.id)); + dispatch(startRuleWithUpdate(rule.id)); } }; diff --git a/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStopDialog.tsx b/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStopDialog.tsx index f5f5ef7ef8..1055f8a4b8 100644 --- a/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStopDialog.tsx +++ b/smart-frontend/app/src/components/pages/RulesPage/RulesDialogs/RuleStopDialog.tsx @@ -18,7 +18,7 @@ import React from 'react'; import { Dialog } from '@uikit'; import { useDispatch, useStore } from '@hooks'; -import { closeStopRuleDialog, stopRule } from '@store/adh/rules/rulesActionsSlice'; +import { closeStopRuleDialog, stopRuleWithUpdate } from '@store/adh/rules/rulesActionsSlice'; const RuleStopDialog: React.FC = () => { const dispatch = useDispatch(); @@ -30,7 +30,7 @@ const RuleStopDialog: React.FC = () => { }; const handleDelete = () => { if (rule) { - dispatch(stopRule(rule.id)); + dispatch(stopRuleWithUpdate(rule.id)); } }; diff --git a/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts b/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts index b0712f8b47..8cca2c256f 100644 --- a/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts +++ b/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts @@ -20,7 +20,7 @@ import type { AdhRule } from '@models/adh'; import type { ModalState } from '@models/modal'; import { createAsyncThunk } from '@store/redux'; // eslint-disable-next-line import/no-cycle -import { getRules } from './rulesSlice'; +import { getRules, getRulesMetaInfo } from './rulesSlice'; import { showError, showSuccess } from '@store/notificationsSlice'; import { getErrorMessage } from '@utils/responseUtils'; import type { RequestError } from '@api'; @@ -77,22 +77,30 @@ const stopRule = createAsyncThunk('adh/rulesActions/stopRule', async (ruleId: nu } }); -const withUpdate = (name: string, dialogAction: ReturnType) => { +type DialogActionType = ReturnType; + +const withFullUpdate = (name: string, dialogAction: DialogActionType) => { + return createAsyncThunk(name, async (payload: unknown, thunkAPI) => { + await thunkAPI.dispatch(dialogAction(payload)).unwrap(); + thunkAPI.dispatch(getRulesMetaInfo()); + thunkAPI.dispatch(getRules()); + }); +}; + +const withMetaInfoUpdate = (name: string, dialogAction: DialogActionType) => { return createAsyncThunk(name, async (payload: unknown, thunkAPI) => { await thunkAPI.dispatch(dialogAction(payload)).unwrap(); - await thunkAPI.dispatch(getRules()); + thunkAPI.dispatch(getRulesMetaInfo()); }); }; -const createRuleWithUpdate = withUpdate( - 'adh/rulesActions/createRuleWithUpdate', - createRule as ReturnType, -); +const createRuleWithUpdate = withFullUpdate('adh/rulesActions/createRuleWithUpdate', createRule as DialogActionType); + +const deleteRuleWithUpdate = withFullUpdate('adh/rulesActions/deleteRuleWithUpdate', deleteRule as DialogActionType); + +const startRuleWithUpdate = withMetaInfoUpdate('adh/rulesActions/startRuleWithUpdate', startRule as DialogActionType); -const deleteRuleWithUpdate = withUpdate( - 'adh/rulesActions/deleteRuleWithUpdate', - deleteRule as ReturnType, -); +const stopRuleWithUpdate = withMetaInfoUpdate('adh/rulesActions/stopRuleWithUpdate', stopRule as DialogActionType); interface AdhRulesActions extends ModalState { startDialog: { @@ -170,8 +178,8 @@ export { closeStopRuleDialog, createRuleWithUpdate, deleteRuleWithUpdate, - startRule, - stopRule, + startRuleWithUpdate, + stopRuleWithUpdate, }; export default rulesActionsSlice.reducer; diff --git a/smart-frontend/app/src/store/adh/rules/rulesSlice.ts b/smart-frontend/app/src/store/adh/rules/rulesSlice.ts index 8aee32b1d2..9f037ea74f 100644 --- a/smart-frontend/app/src/store/adh/rules/rulesSlice.ts +++ b/smart-frontend/app/src/store/adh/rules/rulesSlice.ts @@ -25,7 +25,7 @@ import { AdhRulesApi } from '@api'; import { executeWithMinDelay } from '@utils/requestUtils'; import { defaultSpinnerDelay } from '@constants'; // eslint-disable-next-line import/no-cycle -import { startRule, stopRule } from './rulesActionsSlice'; +import { startRuleWithUpdate, stopRuleWithUpdate } from './rulesActionsSlice'; import type { AdhRule } from '@models/adh'; import { AdhRuleState } from '@models/adh'; @@ -79,8 +79,15 @@ const refreshRules = createAsyncThunk('adh/rules/refreshRules', async (_, thunkA const getRulesMetaInfo = createAsyncThunk('adh/rules/getRulesMetaInfo', async (_, thunkAPI) => { try { - const allCollection = await AdhRulesApi.getRules({}); - const activeCollection = await AdhRulesApi.getRules({ ruleStates: [AdhRuleState.Active] }); + const [ + // + allCollection, + activeCollection, + ] = await Promise.all([ + // + AdhRulesApi.getRules({}), + AdhRulesApi.getRules({ ruleStates: [AdhRuleState.Active] }), + ]); return { allCollection, @@ -124,14 +131,14 @@ const rulesSlice = createSlice({ state.rules = []; state.totalCount = 0; }); - builder.addCase(startRule.fulfilled, (state, action) => { + builder.addCase(startRuleWithUpdate.fulfilled, (state, action) => { const ruleId = action.meta.arg; const currentRule = state.rules.find((rule) => rule.id === ruleId); if (currentRule) { currentRule.state = AdhRuleState.Active; } }); - builder.addCase(stopRule.fulfilled, (state, action) => { + builder.addCase(stopRuleWithUpdate.fulfilled, (state, action) => { const ruleId = action.meta.arg; const currentRule = state.rules.find((rule) => rule.id === ruleId); if (currentRule) {