From f431300531e1cc8763f8af1efd545e4aa7e825df Mon Sep 17 00:00:00 2001 From: remizov Date: Mon, 26 Aug 2024 14:09:59 +0300 Subject: [PATCH 1/2] 4908 rules meta info update --- .../RulesDialogs/RuleStartDialog.tsx | 4 +-- .../RulesPage/RulesDialogs/RuleStopDialog.tsx | 4 +-- .../src/store/adh/rules/rulesActionsSlice.ts | 32 +++++++++++++++---- .../app/src/store/adh/rules/rulesSlice.ts | 17 +++++++--- 4 files changed, 41 insertions(+), 16 deletions(-) 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..5bdca029d4 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,23 +77,41 @@ const stopRule = createAsyncThunk('adh/rulesActions/stopRule', async (ruleId: nu } }); -const withUpdate = (name: string, dialogAction: ReturnType) => { +const withFullUpdate = (name: string, dialogAction: ReturnType) => { return createAsyncThunk(name, async (payload: unknown, thunkAPI) => { await thunkAPI.dispatch(dialogAction(payload)).unwrap(); - await thunkAPI.dispatch(getRules()); + thunkAPI.dispatch(getRulesMetaInfo()); + thunkAPI.dispatch(getRules()); }); }; -const createRuleWithUpdate = withUpdate( +const withMetaInfoUpdate = (name: string, dialogAction: ReturnType) => { + return createAsyncThunk(name, async (payload: unknown, thunkAPI) => { + await thunkAPI.dispatch(dialogAction(payload)).unwrap(); + thunkAPI.dispatch(getRulesMetaInfo()); + }); +}; + +const createRuleWithUpdate = withFullUpdate( 'adh/rulesActions/createRuleWithUpdate', createRule as ReturnType, ); -const deleteRuleWithUpdate = withUpdate( +const deleteRuleWithUpdate = withFullUpdate( 'adh/rulesActions/deleteRuleWithUpdate', deleteRule as ReturnType, ); +const startRuleWithUpdate = withMetaInfoUpdate( + 'adh/rulesActions/startRuleWithUpdate', + startRule as ReturnType, +); + +const stopRuleWithUpdate = withMetaInfoUpdate( + 'adh/rulesActions/stopRuleWithUpdate', + stopRule as ReturnType, +); + interface AdhRulesActions extends ModalState { startDialog: { rule: AdhRule | null; @@ -170,8 +188,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) { From d71d0995f26787e8c4c7e16de77256c6264ee656 Mon Sep 17 00:00:00 2001 From: remizov Date: Mon, 26 Aug 2024 15:08:59 +0300 Subject: [PATCH 2/2] 4908 rules meta info update --- .../src/store/adh/rules/rulesActionsSlice.ts | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts b/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts index 5bdca029d4..8cca2c256f 100644 --- a/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts +++ b/smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts @@ -77,7 +77,9 @@ const stopRule = createAsyncThunk('adh/rulesActions/stopRule', async (ruleId: nu } }); -const withFullUpdate = (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()); @@ -85,32 +87,20 @@ const withFullUpdate = (name: string, dialogAction: ReturnType) => { +const withMetaInfoUpdate = (name: string, dialogAction: DialogActionType) => { return createAsyncThunk(name, async (payload: unknown, thunkAPI) => { await thunkAPI.dispatch(dialogAction(payload)).unwrap(); thunkAPI.dispatch(getRulesMetaInfo()); }); }; -const createRuleWithUpdate = withFullUpdate( - 'adh/rulesActions/createRuleWithUpdate', - createRule as ReturnType, -); - -const deleteRuleWithUpdate = withFullUpdate( - 'adh/rulesActions/deleteRuleWithUpdate', - deleteRule as ReturnType, -); - -const startRuleWithUpdate = withMetaInfoUpdate( - 'adh/rulesActions/startRuleWithUpdate', - startRule as ReturnType, -); - -const stopRuleWithUpdate = withMetaInfoUpdate( - 'adh/rulesActions/stopRuleWithUpdate', - stopRule 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 stopRuleWithUpdate = withMetaInfoUpdate('adh/rulesActions/stopRuleWithUpdate', stopRule as DialogActionType); interface AdhRulesActions extends ModalState { startDialog: {