@@ -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' ;
3838import deepEqual from 'fast-deep-equal' ;
3939import { ItemDef , TranslatedText , Origin } from 'core/defineStructure.js' ;
@@ -123,86 +123,34 @@ const addVariables = (state, action) => {
123123} ;
124124
125125const 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
235182const 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 }
0 commit comments