Skip to content

Commit

Permalink
Simplify host check selection state
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Jul 12, 2023
1 parent f35123b commit de9467b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 59 deletions.
7 changes: 3 additions & 4 deletions assets/js/components/HostDetails/HostSettingsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useParams } from 'react-router-dom';

import LoadingBox from '@components/LoadingBox';

import { checksSelected } from '@state/checksSelection';
import { checksSelected } from '@state/hostChecksSelection';
import { updateCatalog } from '@state/actions/catalog';
import { getCatalog } from '@state/selectors/catalog';
import { getHost } from '@state/selectors';
Expand Down Expand Up @@ -58,9 +58,8 @@ function HostSettingsPage() {
onSaveSelection={(selection, targetID) =>
dispatch(
checksSelected({
targetID,
targetType: 'host',
targetName: host.hostname,
hostID: targetID,
hostName: host.hostname,
checks: selection,
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createAction, createSlice } from '@reduxjs/toolkit';

const initialState = {
saving: false,
savingSuccess: false,
};

export const checksSelectionSlice = createSlice({
Expand All @@ -11,24 +10,17 @@ export const checksSelectionSlice = createSlice({
reducers: {
startSavingChecksSelection: (state) => {
state.saving = true;
state.savingSuccess = false;
},
stopSavingChecksSelection: (state) => {
state.saving = false;
},
setChecksSelectionSavingSuccess: (state) => {
state.savingSuccess = true;
},
},
});

export const CHECKS_SELECTED = 'CHECKS_SELECTED';
export const checksSelected = createAction(CHECKS_SELECTED);
export const HOST_CHECKS_SELECTED = 'HOST_CHECKS_SELECTED';
export const checksSelected = createAction(HOST_CHECKS_SELECTED);

export const {
startSavingChecksSelection,
stopSavingChecksSelection,
setChecksSelectionSavingSuccess,
} = checksSelectionSlice.actions;
export const { startSavingChecksSelection, stopSavingChecksSelection } =
checksSelectionSlice.actions;

export default checksSelectionSlice.reducer;
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import checksSelectionReducer, {
startSavingChecksSelection,
stopSavingChecksSelection,
setChecksSelectionSavingSuccess,
} from '@state/checksSelection';
} from '@state/hostChecksSelection';

describe('Checks Selection reducer', () => {
it('should mark a check selection as saving', () => {
const initialState = {
saving: false,
savingSuccess: false,
};

const action = startSavingChecksSelection();

const expectedState = {
saving: true,
savingSuccess: false,
};

expect(checksSelectionReducer(initialState, action)).toEqual(expectedState);
Expand All @@ -24,30 +21,12 @@ describe('Checks Selection reducer', () => {
it('should mark a check selection as completed', () => {
const initialState = {
saving: true,
savingSuccess: false,
};

const action = stopSavingChecksSelection();

const expectedState = {
saving: false,
savingSuccess: false,
};

expect(checksSelectionReducer(initialState, action)).toEqual(expectedState);
});

it('should mark a successfully saved check selection', () => {
const initialState = {
saving: true,
savingSuccess: false,
};

const action = setChecksSelectionSavingSuccess();

const expectedState = {
saving: true,
savingSuccess: true,
};

expect(checksSelectionReducer(initialState, action)).toEqual(expectedState);
Expand Down
2 changes: 1 addition & 1 deletion assets/js/state/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const hostsListSlice = createSlice({
},
updateSelectedChecks: (state, action) => {
state.hosts = state.hosts.map((host) => {
if (host.id === action.payload.targetID) {
if (host.id === action.payload.hostID) {
host.selected_checks = action.payload.checks;
}
return host;
Expand Down
2 changes: 1 addition & 1 deletion assets/js/state/hosts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Hosts reducer', () => {
const newChecksSelection = [faker.datatype.uuid(), faker.datatype.uuid()];

const action = updateSelectedChecks({
targetID: host1.id,
hostID: host1.id,
checks: newChecksSelection,
});

Expand Down
4 changes: 2 additions & 2 deletions assets/js/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sapSystemsHealthSummaryReducer from './healthSummary';
import hostsListReducer from './hosts';
import clustersListReducer from './clusters';
import clusterChecksSelectionReducer from './clusterChecksSelection';
import checksSelectionReducer from './checksSelection';
import hostChecksSelectionReducer from './hostChecksSelection';
import checksResultsFiltersReducer from './checksResultsFilters';
import sapSystemListReducer from './sapSystems';
import databasesListReducer from './databases';
Expand All @@ -24,7 +24,7 @@ export const store = configureStore({
hostsList: hostsListReducer,
clustersList: clustersListReducer,
clusterChecksSelection: clusterChecksSelectionReducer,
checksSelection: checksSelectionReducer,
checksSelection: hostChecksSelectionReducer,
checksResultsFilters: checksResultsFiltersReducer,
sapSystemsList: sapSystemListReducer,
databasesList: databasesListReducer,
Expand Down
18 changes: 8 additions & 10 deletions assets/js/state/sagas/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import {
} from '@state/hosts';

import {
setChecksSelectionSavingSuccess,
startSavingChecksSelection,
stopSavingChecksSelection,
CHECKS_SELECTED,
} from '@state/checksSelection';
HOST_CHECKS_SELECTED,
} from '@state/hostChecksSelection';

import { notify } from '@state/actions/notifications';

Expand Down Expand Up @@ -74,26 +73,25 @@ export function* deregisterHost({ payload }) {
}

export function* checksSelected({ payload }) {
const { targetID, targetName } = payload;
const { hostID, hostName, checks } = payload;
yield put(startSavingChecksSelection());

try {
yield call(post, `/hosts/${targetID}/checks`, {
checks: payload.checks,
yield call(post, `/hosts/${hostID}/checks`, {
checks,
});

yield put(updateSelectedChecks(payload));
yield put(
notify({
text: `Checks selection for ${targetName} saved`,
text: `Checks selection for ${hostName} saved`,
icon: '💾',
})
);
yield put(setChecksSelectionSavingSuccess());
} catch (error) {
yield put(
notify({
text: `Unable to save selection for ${targetName}`,
text: `Unable to save selection for ${hostName}`,
icon: '❌',
})
);
Expand All @@ -114,5 +112,5 @@ export function* watchDeregisterHost() {
}

export function* watchHostChecksSelection() {
yield takeEvery(CHECKS_SELECTED, checksSelected);
yield takeEvery(HOST_CHECKS_SELECTED, checksSelected);
}
12 changes: 5 additions & 7 deletions assets/js/state/sagas/hosts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import {
} from '@state/hosts';

import {
setChecksSelectionSavingSuccess,
startSavingChecksSelection,
stopSavingChecksSelection,
} from '@state/checksSelection';
} from '@state/hostChecksSelection';

import { networkClient } from '@lib/network';
import { notify } from '@state/actions/notifications';
Expand Down Expand Up @@ -130,8 +129,8 @@ describe('Hosts sagas', () => {
axiosMock.onPost(`/hosts/${host.id}/checks`).reply(202, {});

const actionPayload = {
targetID: host.id,
targetName: host.hostname,
hostID: host.id,
hostName: host.hostname,
checks: [faker.datatype.uuid(), faker.datatype.uuid()],
};
const dispatched = await recordSaga(checksSelected, {
Expand All @@ -145,7 +144,6 @@ describe('Hosts sagas', () => {
text: `Checks selection for ${host.hostname} saved`,
icon: '💾',
}),
setChecksSelectionSavingSuccess(),
stopSavingChecksSelection(),
]);
});
Expand All @@ -156,8 +154,8 @@ describe('Hosts sagas', () => {
axiosMock.onPost(`/hosts/${host.id}/checks`).reply(400, {});

const actionPayload = {
targetID: host.id,
targetName: host.hostname,
hostID: host.id,
hostName: host.hostname,
checks: [faker.datatype.uuid(), faker.datatype.uuid()],
};
const dispatched = await recordSaga(checksSelected, {
Expand Down

0 comments on commit de9467b

Please sign in to comment.