Skip to content

Commit 08c38ff

Browse files
committed
ItemDef sources
1 parent 3bc922a commit 08c38ff

9 files changed

Lines changed: 210 additions & 197 deletions

File tree

app/actions/item.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
CL_VALUELISTS,
3535
CL_COMMENTS,
3636
CL_WHERECLAUSES,
37+
CL_ITEMDEFS,
3738
} from 'constants/action-types';
3839

3940
// Item Ref/Def actions
@@ -207,3 +208,10 @@ export const cleanWhereClauses = (deleteObj) => (
207208
deleteObj,
208209
}
209210
);
211+
212+
export const cleanItemDefs = (deleteObj) => (
213+
{
214+
type: CL_ITEMDEFS,
215+
deleteObj,
216+
}
217+
);

app/constants/action-types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export const CL_METHODS = 'CL_METHODS';
136136
export const CL_VALUELISTS = 'CL_VALUELISTS';
137137
export const CL_COMMENTS = 'CL_COMMENTS';
138138
export const CL_WHERECLAUSES = 'CL_WHERECLAUSES';
139+
export const CL_ITEMDEFS = 'CL_ITEMDEFS';
139140

140141
// Labels for Undo/Action History
141142
export const actionLabels = {
@@ -216,4 +217,5 @@ export const actionLabels = {
216217
'CL_VALUELISTS': 'Remove unused value lists',
217218
'CL_COMMENTS': 'Remove unused comments',
218219
'CL_WHERECLAUSES': 'Remove unused where clauses',
220+
'CL_ITEMDEFS': 'Remove unused variables',
219221
};

app/reducers/itemDefs.js

Lines changed: 30 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
ADD_VARS,
2323
DEL_VARS,
2424
DEL_CODELISTS,
25-
DEL_ITEMGROUPS,
2625
ADD_VALUELIST,
2726
ADD_VALUELISTFROMCODELIST,
2827
INSERT_VAR,
@@ -34,6 +33,7 @@ import {
3433
DEL_REVIEWCOMMENT,
3534
ADD_IMPORTMETADATA,
3635
DEL_DUPLICATECOMMENTS,
36+
CL_ITEMDEFS,
3737
} from 'constants/action-types';
3838
import deepEqual from 'fast-deep-equal';
3939
import { ItemDef, TranslatedText, Origin } from 'core/defineStructure.js';
@@ -123,86 +123,34 @@ const addVariables = (state, action) => {
123123
};
124124

125125
const deleteVariables = (state, action) => {
126-
// action.deleteObj.itemDefOids: [itemDefOid1, itemDefOid2, ...]
127-
// action.deleteObj.vlmItemDefOids: { valueListOid1: [itemDefOid1, itemDefOid2, ...], valueListOid2: [itemDefOid3, ...]
128-
let newState = Object.assign({}, state);
129-
// First go through itemDefs which are coming from the variable level;
130-
action.deleteObj.itemDefOids.forEach(itemDefOid => {
131-
// If it is referened only in 1 dataset, remove it
132-
let sourceNum = [].concat.apply([], Object.keys(state[itemDefOid].sources).map(type => (state[itemDefOid].sources[type]))).length;
133-
if (sourceNum === 1 &&
134-
state[itemDefOid].sources.itemGroups[0] === action.source.itemGroupOid) {
126+
// action.deleteObj.removedItemDefOids: [itemDefOid1, itemDefOid2, ...]
127+
const { removedItemDefOids } = action.deleteObj;
128+
let newState = { ...state };
129+
if (removedItemDefOids !== undefined) {
130+
removedItemDefOids.forEach(itemDefOid => {
135131
delete newState[itemDefOid];
136-
} else if (state[itemDefOid].sources.itemGroups.includes(action.source.itemGroupOid)) {
137-
// Delete the dataset from the sources
138-
// TODO: Currently review comments are always removed, as in case a variable is removed, review comment is removed as well
139-
// TODO: Implement better review comment handling in the future
140-
let newSourcesForType = state[itemDefOid].sources.itemGroups.slice();
141-
newSourcesForType.splice(newSourcesForType.indexOf(action.source.itemGroupOid), 1);
142-
newState = {
143-
...newState,
144-
[itemDefOid]: {
145-
...new ItemDef({
146-
...state[itemDefOid],
147-
reviewCommentOids: [],
148-
sources: { ...state[itemDefOid].sources, itemGroups: newSourcesForType }
149-
})
150-
}
151-
};
152-
}
153-
});
154-
// Remove value levels
155-
Object.keys(action.deleteObj.vlmItemDefOids).forEach(valueListOid => {
156-
action.deleteObj.vlmItemDefOids[valueListOid].forEach(itemDefOid => {
157-
// It is possible that valueList was shared between different ItemDefs and already removed in this action
158-
if (newState.hasOwnProperty(itemDefOid)) {
159-
// If it is referened only in 1 dataset, remove it
160-
let sourceNum = [].concat.apply([], Object.keys(state[itemDefOid].sources).map(type => (state[itemDefOid].sources[type]))).length;
161-
if (sourceNum === 1 &&
162-
state[itemDefOid].sources.valueLists[0] === valueListOid) {
163-
delete newState[itemDefOid];
164-
} else if (state[itemDefOid].sources.valueLists.includes(valueListOid)) {
165-
// Delete the dataset from the sources
166-
// TODO: Currently review comments are always removed, as in case a variable is removed, review comment is removed as well
167-
// TODO: Implement better review comment handling in the future
168-
let newSourcesForType = state[itemDefOid].sources.valueLists.slice();
169-
newSourcesForType.splice(newSourcesForType.indexOf(valueListOid), 1);
170-
newState = {
171-
...newState,
172-
[itemDefOid]: { ...new ItemDef({ ...state[itemDefOid],
173-
reviewCommentOids: [],
174-
sources: { ...state[itemDefOid].sources, valueLists: newSourcesForType }
175-
}) }
176-
};
177-
}
178-
}
179132
});
180-
});
181-
// When only value level is removed, delete reference to it
182-
Object.keys(action.deleteObj.valueListOids).forEach(itemDefOid => {
183-
if (newState.hasOwnProperty(itemDefOid)) {
184-
newState = {
185-
...newState,
186-
[itemDefOid]: { ...new ItemDef({ ...newState[itemDefOid], valueListOid: undefined }) }
187-
};
188-
}
189-
});
190-
return newState;
133+
return newState;
134+
} else {
135+
return state;
136+
}
191137
};
192138

193-
const deleteItemGroups = (state, action) => {
194-
// action.deleteObj.itemGroupData contains:
195-
// {[itemGroupOid] : itemDefOids: { [itemOid1, itemOid2, ...]}}
196-
// {[itemGroupOid] : vlmItemDefOids: { [itemOid1, itemOid2, ...]}}
197-
// {[itemGroupOid] : valueListOids: { [vlOid1, vlOid2, ...]}}
139+
const handleDeleteVariables = (state, action) => {
140+
// action.deleteObj.vlmItemDefOids: { valueListOid1: [itemDefOid1, itemDefOid2, ...], valueListOid2: [itemDefOid3, ...]
141+
const { valueListOids } = action.deleteObj;
198142
let newState = { ...state };
199-
Object.keys(action.deleteObj.itemGroupData).forEach(itemGroupOid => {
200-
let subAction = { deleteObj: {}, source: { itemGroupOid } };
201-
subAction.deleteObj.itemDefOids = action.deleteObj.itemGroupData[itemGroupOid].itemDefOids;
202-
subAction.deleteObj.vlmItemDefOids = action.deleteObj.itemGroupData[itemGroupOid].vlmItemDefOids;
203-
subAction.deleteObj.valueListOids = action.deleteObj.itemGroupData[itemGroupOid].valueListOids;
204-
newState = deleteVariables(newState, subAction);
205-
});
143+
// Delete references to removed vaueLists
144+
if (valueListOids !== undefined) {
145+
Object.keys(valueListOids).forEach(itemDefOid => {
146+
if (newState.hasOwnProperty(itemDefOid)) {
147+
newState = {
148+
...newState,
149+
[itemDefOid]: { ...new ItemDef({ ...newState[itemDefOid], valueListOid: undefined }) }
150+
};
151+
}
152+
});
153+
}
206154
return newState;
207155
};
208156

@@ -221,7 +169,6 @@ const handleAddValueList = (state, action) => {
221169
// Create a new itemDef for the valueList
222170
let newItemDef = { ...new ItemDef({
223171
oid: action.itemDefOid,
224-
sources: { itemGroups: [], valueLists: [action.valueListOid] },
225172
parentItemDefOid: action.source.oid,
226173
}) };
227174
// Update existing itemDef to reference VLM
@@ -233,7 +180,7 @@ const handleAddValueList = (state, action) => {
233180
};
234181

235182
const handleAddValueListFromCodeList = (state, action) => {
236-
// create the first itemDef
183+
// Create the first itemDef
237184
let firstVl = handleAddValueList(state, {
238185
source: {
239186
oid: action.updateObj.sourceOid,
@@ -243,7 +190,7 @@ const handleAddValueListFromCodeList = (state, action) => {
243190
whereClauseOid: action.updateObj.whereClauseOids[0],
244191
});
245192

246-
// add subsequent itemDefs
193+
// Add subsequent itemDefs
247194
let subsequentVls = action.updateObj.itemDefOids.slice(1).reduce((object, value, key) => {
248195
return insertValueLevel(object, {
249196
type: INSERT_VALLVL,
@@ -257,7 +204,7 @@ const handleAddValueListFromCodeList = (state, action) => {
257204
});
258205
}, firstVl);
259206

260-
// add names, labels, and additional attributes to all itemDefs
207+
// Add names, labels, and additional attributes to all itemDefs
261208
let namedAndLabelledVls = action.updateObj.itemDefOids.reduce((object, value, key) => {
262209
return updateItemDef(object, {
263210
type: UPD_ITEMDEF,
@@ -280,7 +227,6 @@ const insertVariable = (state, action) => {
280227
// Create a new itemDef
281228
let newItemDef = { ...new ItemDef({
282229
oid: action.itemDefOid,
283-
sources: { itemGroups: [action.itemGroupOid], valueLists: [] },
284230
}) };
285231
return { ...state, [action.itemDefOid]: newItemDef };
286232
};
@@ -289,7 +235,6 @@ const insertValueLevel = (state, action) => {
289235
// Create a new itemDef
290236
let newItemDef = { ...new ItemDef({
291237
oid: action.itemDefOid,
292-
sources: { itemGroups: [], valueLists: [action.valueListOid] },
293238
parentItemDefOid: action.parentItemDefOid,
294239
}) };
295240
return { ...state, [action.itemDefOid]: newItemDef };
@@ -540,11 +485,9 @@ const itemDefs = (state = {}, action) => {
540485
case ADD_VARS:
541486
return addVariables(state, action);
542487
case DEL_VARS:
543-
return deleteVariables(state, action);
488+
return handleDeleteVariables(state, action);
544489
case ADD_ITEMGROUPS:
545490
return handleAddItemGroups(state, action);
546-
case DEL_ITEMGROUPS:
547-
return deleteItemGroups(state, action);
548491
case DEL_CODELISTS:
549492
return deleteCodeLists(state, action);
550493
case ADD_VALUELIST:
@@ -567,6 +510,8 @@ const itemDefs = (state = {}, action) => {
567510
return addImportMetadata(state, action);
568511
case DEL_DUPLICATECOMMENTS:
569512
return deleteDuplicateComments(state, action);
513+
case CL_ITEMDEFS:
514+
return deleteVariables(state, action);
570515
default:
571516
return state;
572517
}

app/store/cleanState.js

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
import getUnusedMethods from 'utils/getUnsedMethods.js';
2-
import getUnsedValueLists from 'utils/getUnsedValueLists.js';
3-
import getUnusedComments from 'utils/getUnsedComments';
4-
import getUnusedWhereClauses from 'utils/getUnsedWhereClauses';
5-
import { cleanMethods, cleanValueLists, cleanComments, cleanWhereClauses } from 'actions/item.js';
1+
import getUnusedItems from 'utils/getUnsedItems';
2+
import {
3+
cleanMethods,
4+
cleanValueLists,
5+
cleanComments,
6+
cleanWhereClauses,
7+
cleanItemDefs
8+
} from 'actions/item.js';
69
import {
710
UPD_ITEMDESCRIPTION,
8-
DEL_VARS,
9-
DEL_ITEMGROUPS,
1011
UPD_ITEMSBULK,
1112
ADD_VARS,
1213
ADD_ITEMGROUPS,
1314
ADD_IMPORTMETADATA,
1415
UPD_ARMSTATUS,
16+
DEL_VARS,
17+
DEL_ITEMGROUPS,
18+
DEL_CODELISTS,
1519
DEL_RESULTDISPLAY,
1620
DEL_ANALYSISRESULT,
21+
DEL_ITEMGROUPCOMMENT,
1722
UPD_ITEMGROUPCOMMENT,
1823
UPD_NAMELABELWHERECLAUSE,
1924
UPD_MDV,
2025
UPD_STD,
2126
UPD_ANALYSISRESULT,
22-
DEL_ITEMGROUPCOMMENT,
2327
REP_ITEMGROUPCOMMENT
2428
} from 'constants/action-types';
2529

@@ -34,6 +38,7 @@ const cleanState = store => next => action => {
3438
let checkValueLists = false;
3539
let checkComments = false;
3640
let checkWhereClauses = false;
41+
let checkItemDefs = false;
3742

3843
// Different actions can impact different parts of the state, select only those which can created orphaned elements
3944
if ([UPD_ITEMDESCRIPTION, DEL_VARS, DEL_ITEMGROUPS, UPD_ITEMSBULK, ADD_VARS, ADD_ITEMGROUPS, ADD_IMPORTMETADATA].includes(action.type)) {
@@ -43,13 +48,19 @@ const cleanState = store => next => action => {
4348
checkValueLists = true;
4449
}
4550
if ([DEL_VARS, DEL_ITEMGROUPS, UPD_ITEMSBULK, ADD_VARS, UPD_ARMSTATUS, DEL_RESULTDISPLAY, DEL_ANALYSISRESULT, UPD_ITEMDESCRIPTION, UPD_ITEMGROUPCOMMENT,
46-
UPD_NAMELABELWHERECLAUSE, UPD_MDV, UPD_STD, UPD_ANALYSISRESULT, DEL_ITEMGROUPCOMMENT, REP_ITEMGROUPCOMMENT].includes(action.type)
51+
UPD_NAMELABELWHERECLAUSE, UPD_MDV, UPD_STD, UPD_ANALYSISRESULT, DEL_ITEMGROUPCOMMENT, DEL_CODELISTS, REP_ITEMGROUPCOMMENT].includes(action.type)
4752
) {
4853
checkComments = true;
4954
}
5055
if ([UPD_NAMELABELWHERECLAUSE, DEL_ITEMGROUPS, DEL_RESULTDISPLAY, DEL_ANALYSISRESULT, UPD_ARMSTATUS, DEL_VARS].includes(action.type)) {
5156
checkWhereClauses = true;
5257
}
58+
if ([DEL_ITEMGROUPS, DEL_VARS].includes(action.type)) {
59+
checkWhereClauses = true;
60+
}
61+
if ([DEL_ITEMGROUPS, DEL_VARS].includes(action.type)) {
62+
checkItemDefs = true;
63+
}
5364

5465
if (!checkMethods && !checkValueLists && !checkComments) {
5566
return result;
@@ -61,42 +72,47 @@ const cleanState = store => next => action => {
6172
return result;
6273
}
6374

64-
const mdv = state.present.odm.study.metaDataVersion;
65-
66-
/* Clean methods that are not referenced anymore */
67-
if (checkMethods) {
68-
const removedMethodOids = getUnusedMethods(mdv);
69-
70-
// Form an action to remove those methods
71-
if (removedMethodOids.length > 0) {
72-
store.dispatch(cleanMethods({ removedMethodOids }));
75+
if (checkItemDefs) {
76+
const removedItemDefOids = getUnusedItems(store, 'ItemDef');
77+
// Form an action to remove those itemDefs
78+
if (removedItemDefOids.length > 0) {
79+
store.dispatch(cleanItemDefs({ removedItemDefOids }));
7380
}
7481
}
7582

7683
if (checkValueLists) {
77-
const removedValueListOids = getUnsedValueLists(mdv);
84+
const removedValueListOids = getUnusedItems(store, 'ValueList');
7885
// Form an action to remove those valueLists
7986
if (removedValueListOids.length > 0) {
8087
store.dispatch(cleanValueLists({ removedValueListOids }));
8188
}
8289
}
8390

84-
if (checkComments) {
85-
const removedCommentOids = getUnusedComments(mdv);
86-
// Form an action to remove those comments
87-
if (removedCommentOids.length > 0) {
88-
store.dispatch(cleanComments({ removedCommentOids }));
89-
}
90-
}
91-
9291
if (checkWhereClauses) {
93-
const removedWhereClauseOids = getUnusedWhereClauses(mdv);
92+
const removedWhereClauseOids = getUnusedItems(store, 'WhereClause');
9493
// Form an action to remove those whereClauses
9594
if (removedWhereClauseOids.length > 0) {
9695
store.dispatch(cleanWhereClauses({ removedWhereClauseOids }));
9796
}
9897
}
9998

99+
if (checkMethods) {
100+
const removedMethodOids = getUnusedItems(store, 'Method');
101+
102+
// Form an action to remove those methods
103+
if (removedMethodOids.length > 0) {
104+
store.dispatch(cleanMethods({ removedMethodOids }));
105+
}
106+
}
107+
108+
if (checkComments) {
109+
const removedCommentOids = getUnusedItems(store, 'Comment');
110+
// Form an action to remove those comments
111+
if (removedCommentOids.length > 0) {
112+
store.dispatch(cleanComments({ removedCommentOids }));
113+
}
114+
}
115+
100116
return result;
101117
};
102118

app/utils/getUnsedComments.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)