Skip to content

Commit 748831f

Browse files
committed
Refactor usage of selection of rows
1 parent 1d41e85 commit 748831f

File tree

6 files changed

+29
-35
lines changed

6 files changed

+29
-35
lines changed

src/SmartComponents/SystemAdvisories/SystemAdvisories.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { withRouter } from 'react-router-dom';
55
import AdvisoriesTable from '../../PresentationalComponents/AdvisoriesTable/AdvisoriesTable';
66
import { systemAdvisoriesColumns } from '../../PresentationalComponents/AdvisoriesTable/AdvisoriesTableAssets';
77
import Error from '../../PresentationalComponents/Snippets/Error';
8-
import { changeSystemAdvisoryListParams, clearSystemAdvisoriesStore,
9-
expandSystemAdvisoryRow, fetchApplicableSystemAdvisories, selectSystemAdvisoryRow } from '../../store/Actions/Actions';
8+
import { changeSystemAdvisoryListParams, clearSystemAdvisoriesStore, expandSystemAdvisoryRow,
9+
fetchApplicableSystemAdvisories, selectSystemAdvisoryRow } from '../../store/Actions/Actions';
1010
import { STATUS_REJECTED } from '../../Utilities/constants';
1111
import { createSystemAdvisoriesRows } from '../../Utilities/DataMappers';
1212
import { arrayFromObj, createSortBy, decodeQueryparams, encodeURLParams,
@@ -71,15 +71,15 @@ const SystemAdvisories = ({ history }) => {
7171
)
7272
);
7373

74-
const onSelect = React.useCallback((event, value, rowId) => {
74+
const onSelect = React.useCallback((event, selected, rowId) => {
7575
const toSelect = [];
7676
switch (event) {
7777
case 'none': {
7878
Object.keys(selectedRows).forEach(id=>{
7979
toSelect.push(
8080
{
81-
rowId: id,
82-
value: false
81+
id,
82+
selected: false
8383
}
8484
);
8585
});
@@ -90,17 +90,17 @@ const SystemAdvisories = ({ history }) => {
9090
advisories.forEach(({ id })=>{
9191
toSelect.push(
9292
{
93-
rowId: id,
94-
value: true
93+
id,
94+
selected: true
9595
}
9696
);});
9797
break;
9898
}
9999

100100
default: {
101101
toSelect.push({
102-
rowId: getRowIdByIndexExpandable(advisories, rowId),
103-
value
102+
id: getRowIdByIndexExpandable(advisories, rowId),
103+
selected
104104
});
105105
}}
106106

src/Utilities/Helpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export const addOrRemoveItemFromSet = (targetObj, inputArr) => {
3838
return result;
3939
};
4040

41+
export const getNewSelectedItems = (selectedItems, currentItems) => {
42+
let payload = [].concat(selectedItems).map(item=>({ rowId: item.id, value: item.selected }));
43+
return addOrRemoveItemFromSet(
44+
currentItems,
45+
payload
46+
);
47+
};
48+
4149
// for expandable rows only
4250
export const getRowIdByIndexExpandable = (arrayOfObjects, index) => {
4351
return arrayOfObjects[index / 2].id;

src/store/Actions/Actions.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
fetchAdvisoryDetailsApi,
3-
fetchAffectedSystems,
4-
fetchApplicableAdvisoriesApi,
5-
fetchApplicableSystemAdvisoriesApi,
6-
fetchSystems
7-
} from '../../Utilities/api';
1+
import { fetchAdvisoryDetailsApi, fetchAffectedSystems, fetchApplicableAdvisoriesApi,
2+
fetchApplicableSystemAdvisoriesApi, fetchSystems } from '../../Utilities/api';
83
import * as ActionTypes from '../ActionTypes';
94

105
export const fetchApplicableAdvisories = params => ({
@@ -57,22 +52,22 @@ export const changeAffectedSystemsParams = params => ({
5752

5853
export const expandAdvisoryRow = rowState => ({
5954
type: ActionTypes.EXPAND_ADVISORY_ROW,
60-
payload: [].concat(rowState)
55+
payload: rowState
6156
});
6257

6358
export const expandSystemAdvisoryRow = rowState => ({
6459
type: ActionTypes.EXPAND_SYSTEM_ADVISORY_ROW,
65-
payload: [].concat(rowState)
60+
payload: rowState
6661
});
6762

6863
export const selectAdvisoryRow = rowState => ({
6964
type: ActionTypes.SELECT_ADVISORY_ROW,
70-
payload: [].concat(rowState)
65+
payload: rowState
7166
});
7267

7368
export const selectSystemAdvisoryRow = rowState => ({
7469
type: ActionTypes.SELECT_SYSTEM_ADVISORY_ROW,
75-
payload: [].concat(rowState)
70+
payload: rowState
7671
});
7772

7873
export const clearSystemAdvisoriesStore = () => ({

src/store/Reducers/AdvisoryListStore.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { STATUS_LOADING, STATUS_REJECTED, STATUS_RESOLVED, storeListDefaults } from '../../Utilities/constants';
2-
import { addOrRemoveItemFromSet, changeListParams } from '../../Utilities/Helpers';
2+
import { addOrRemoveItemFromSet, changeListParams, getNewSelectedItems } from '../../Utilities/Helpers';
33
import * as ActionTypes from '../ActionTypes';
44

55
export const AdvisoryListStore = (state = storeListDefaults, action) => {
@@ -41,10 +41,7 @@ export const AdvisoryListStore = (state = storeListDefaults, action) => {
4141
}
4242

4343
case ActionTypes.SELECT_ADVISORY_ROW: {
44-
const selectedUpdated = addOrRemoveItemFromSet(
45-
newState.selectedRows,
46-
action.payload
47-
);
44+
const selectedUpdated = getNewSelectedItems(action.payload, newState.selectedRows);
4845
newState = { ...newState, selectedRows: selectedUpdated };
4946
return newState;
5047
}

src/store/Reducers/AffectedSystemsStore.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { STATUS_LOADING, STATUS_REJECTED, STATUS_RESOLVED, storeListDefaults } from '../../Utilities/constants';
2-
import { addOrRemoveItemFromSet, changeListParams } from '../../Utilities/Helpers';
2+
import { changeListParams, getNewSelectedItems } from '../../Utilities/Helpers';
33
import * as ActionTypes from '../ActionTypes';
44

55
export const AffectedSystemsStore = (state = storeListDefaults, action) => {
@@ -16,10 +16,7 @@ export const AffectedSystemsStore = (state = storeListDefaults, action) => {
1616
return newState;
1717

1818
case 'SELECT_ENTITY': {
19-
const selectedUpdated = addOrRemoveItemFromSet(
20-
newState.selectedRows,
21-
[{ rowId: action.payload.id, value: action.payload.selected }]
22-
);
19+
const selectedUpdated = getNewSelectedItems(action.payload, newState.selectedRows);
2320
newState = { ...newState, selectedRows: selectedUpdated };
2421
return newState;
2522
}

src/store/Reducers/SystemAdvisoryListStore.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { STATUS_LOADING, STATUS_RESOLVED, storeListDefaults } from '../../Utilities/constants';
2-
import { addOrRemoveItemFromSet } from '../../Utilities/Helpers';
2+
import { addOrRemoveItemFromSet, getNewSelectedItems } from '../../Utilities/Helpers';
33
import * as ActionTypes from '../ActionTypes';
44

55
export const SystemAdvisoryListStore = (state = storeListDefaults, action) => {
@@ -33,10 +33,7 @@ export const SystemAdvisoryListStore = (state = storeListDefaults, action) => {
3333
}
3434

3535
case ActionTypes.SELECT_SYSTEM_ADVISORY_ROW: {
36-
const selectedUpdated = addOrRemoveItemFromSet(
37-
newState.selectedRows,
38-
action.payload
39-
);
36+
const selectedUpdated = getNewSelectedItems(action.payload, newState.selectedRows);
4037
newState = { ...newState, selectedRows: selectedUpdated };
4138
return newState;
4239
}

0 commit comments

Comments
 (0)