Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] adh-4908 rules meta info update #101

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -30,7 +30,7 @@ const RuleStartDialog: React.FC = () => {
};
const handleDelete = () => {
if (rule) {
dispatch(startRule(rule.id));
dispatch(startRuleWithUpdate(rule.id));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -30,7 +30,7 @@ const RuleStopDialog: React.FC = () => {
};
const handleDelete = () => {
if (rule) {
dispatch(stopRule(rule.id));
dispatch(stopRuleWithUpdate(rule.id));
}
};

Expand Down
32 changes: 25 additions & 7 deletions smart-frontend/app/src/store/adh/rules/rulesActionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,23 +77,41 @@ const stopRule = createAsyncThunk('adh/rulesActions/stopRule', async (ruleId: nu
}
});

const withUpdate = (name: string, dialogAction: ReturnType<typeof createAsyncThunk>) => {
const withFullUpdate = (name: string, dialogAction: ReturnType<typeof createAsyncThunk>) => {
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<typeof createAsyncThunk>) => {
KuzminIA marked this conversation as resolved.
Show resolved Hide resolved
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<typeof createAsyncThunk>,
);

const deleteRuleWithUpdate = withUpdate(
const deleteRuleWithUpdate = withFullUpdate(
'adh/rulesActions/deleteRuleWithUpdate',
deleteRule as ReturnType<typeof createAsyncThunk>,
);

const startRuleWithUpdate = withMetaInfoUpdate(
'adh/rulesActions/startRuleWithUpdate',
startRule as ReturnType<typeof createAsyncThunk>,
);

const stopRuleWithUpdate = withMetaInfoUpdate(
'adh/rulesActions/stopRuleWithUpdate',
stopRule as ReturnType<typeof createAsyncThunk>,
);

interface AdhRulesActions extends ModalState<AdhRule, 'rule'> {
startDialog: {
rule: AdhRule | null;
Expand Down Expand Up @@ -170,8 +188,8 @@ export {
closeStopRuleDialog,
createRuleWithUpdate,
deleteRuleWithUpdate,
startRule,
stopRule,
startRuleWithUpdate,
stopRuleWithUpdate,
};

export default rulesActionsSlice.reducer;
17 changes: 12 additions & 5 deletions smart-frontend/app/src/store/adh/rules/rulesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
Loading