Skip to content

Commit 2cfe80a

Browse files
committed
Fix for group errors
Fix several bugs related to datasets only having one group. fixes #181
1 parent bd160e1 commit 2cfe80a

4 files changed

Lines changed: 53 additions & 19 deletions

File tree

src/app/new.state/xml/xml.effects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ export class XmlEffects {
211211
);
212212
});
213213

214-
saveVariableStatusPending$ = createEffect(() => {
214+
saveBulkVariableStatusPending$ = createEffect(() => {
215215
return this.actions$.pipe(
216216
ofType(XmlManipulationActions.bulkSaveWeightAndGroupChange),
217217
map(() => DatasetActions.saveVariableStatusPending()),
218218
);
219219
});
220220

221-
clearVariableStatusSuccess$ = createEffect(() => {
221+
clearBulkVariableStatusSuccess$ = createEffect(() => {
222222
return this.actions$.pipe(
223223
ofType(XmlManipulationActions.bulkSaveWeightAndGroupChangeSuccess),
224224
delay(10000),

src/app/new.state/xml/xml.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ interface FileDescription {
139139
}
140140

141141
interface DataDescription {
142-
varGrp: VariableGroup[];
142+
varGrp: VariableGroup[] | VariableGroup;
143143
var: Variable[];
144144
processedVar?: { [variableID: string]: Variable };
145145
}

src/app/new.state/xml/xml.reducer.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ export const xmlReducer = createReducer(
120120
);
121121
if (variableTemplate.groups) {
122122
duplicateState.dataset.codeBook.dataDscr.varGrp = updateGroups(
123-
importDdiData.codeBook.dataDscr.varGrp,
123+
Array.isArray(importDdiData.codeBook.dataDscr.varGrp)
124+
? importDdiData.codeBook.dataDscr.varGrp
125+
: [importDdiData.codeBook.dataDscr.varGrp],
124126
variablesMatched,
125127
);
126128
}
@@ -189,13 +191,22 @@ export const xmlReducer = createReducer(
189191
};
190192
}),
191193
on(XmlManipulationActions.createGroup, (state, { groupID, label }) => {
192-
const duplicateVariableGroups = structuredClone(
194+
let duplicateVariableGroups = structuredClone(
193195
state.dataset?.codeBook.dataDscr.varGrp || [],
194196
);
195-
duplicateVariableGroups.push({
197+
const newGroup = {
196198
'@_ID': groupID,
197199
labl: label,
198-
});
200+
};
201+
202+
if (Array.isArray(duplicateVariableGroups)) {
203+
duplicateVariableGroups.push(newGroup);
204+
} else if (duplicateVariableGroups) {
205+
duplicateVariableGroups = [duplicateVariableGroups, newGroup];
206+
} else {
207+
duplicateVariableGroups = [newGroup];
208+
}
209+
199210
return {
200211
...state,
201212
dataset: !state.dataset

src/app/new.state/xml/xml.util.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function changeSingleVariable(
239239
}
240240

241241
export function changeGroupsForSingleVariable(
242-
variableGroups: VariableGroup[],
242+
variableGroups: VariableGroup[] | VariableGroup,
243243
variableID: string,
244244
groups: string[],
245245
) {
@@ -300,13 +300,19 @@ export function changeGroupsForMultipleVariables(
300300
}
301301

302302
export function renameVariableGroup(
303-
duplicateVariableGroups: VariableGroup[],
303+
duplicateVariableGroups: VariableGroup[] | VariableGroup,
304304
groupID: string,
305305
newLabel: string,
306306
) {
307-
const variableGroupArrayLength: number = duplicateVariableGroups.length;
308-
for (let i = 0; i < variableGroupArrayLength - 1; i++) {
309-
const currentVariableGroup = duplicateVariableGroups[i];
307+
const variableGroupArrayLength: number = Array.isArray(
308+
duplicateVariableGroups,
309+
)
310+
? duplicateVariableGroups.length
311+
: 1;
312+
for (let i = 0; i < variableGroupArrayLength; i++) {
313+
const currentVariableGroup = Array.isArray(duplicateVariableGroups)
314+
? duplicateVariableGroups[i]
315+
: duplicateVariableGroups;
310316
if (currentVariableGroup['@_ID'] === groupID) {
311317
currentVariableGroup.labl = newLabel;
312318
break;
@@ -316,32 +322,49 @@ export function renameVariableGroup(
316322
}
317323

318324
export function deleteVariableGroup(
319-
duplicateVariableGroups: VariableGroup[],
325+
duplicateVariableGroups: VariableGroup[] | VariableGroup,
320326
groupID: string,
321327
) {
322-
const variableGroupArrayLength: number = duplicateVariableGroups.length;
328+
const variableGroupArrayLength: number = Array.isArray(
329+
duplicateVariableGroups,
330+
)
331+
? duplicateVariableGroups.length
332+
: 1;
323333
let index = -1;
324334
for (let i = 0; i < variableGroupArrayLength; i++) {
325-
const currentVariableGroup = duplicateVariableGroups[i];
335+
const currentVariableGroup = Array.isArray(duplicateVariableGroups)
336+
? duplicateVariableGroups[i]
337+
: duplicateVariableGroups;
326338
if (currentVariableGroup['@_ID'] === groupID) {
327339
index = i;
328340
break;
329341
}
330342
}
331343
if (index > -1) {
332-
duplicateVariableGroups.splice(index, 1);
344+
if (Array.isArray(duplicateVariableGroups)) {
345+
duplicateVariableGroups.splice(index, 1);
346+
} else {
347+
return [];
348+
}
333349
}
334350
return duplicateVariableGroups;
335351
}
336352

337353
export function removeVariablesFromGroups(
338354
groupID: string,
339355
variableIDs: string[],
340-
duplicateVariableGroups: VariableGroup[],
356+
duplicateVariableGroups: VariableGroup[] | VariableGroup,
341357
) {
342-
const variableGroupArrayLength: number = duplicateVariableGroups.length;
358+
// If the variable groups are not an array (when there is only one variable group), we need to make it an array
359+
const variableGroupArrayLength: number = Array.isArray(
360+
duplicateVariableGroups,
361+
)
362+
? duplicateVariableGroups.length
363+
: 1;
343364
for (let i = 0; i < variableGroupArrayLength; i++) {
344-
const currentVariableGroup = duplicateVariableGroups[i];
365+
const currentVariableGroup = Array.isArray(duplicateVariableGroups)
366+
? duplicateVariableGroups[i]
367+
: duplicateVariableGroups;
345368
if (currentVariableGroup['@_ID'] === groupID) {
346369
const variableListAsArray: string[] =
347370
currentVariableGroup['@_var']?.split(' ') || [];

0 commit comments

Comments
 (0)