Skip to content

Commit 42ab990

Browse files
committed
make getAllVariableUUIDsByEntity more robust
1 parent 5ba8415 commit 42ab990

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/components/sections/Form/withFieldsHandlers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const mapStateToProps = (state, { form, entity, type }) => {
2121
const component = formSelector(state, 'component');
2222
const createNewVariable = formSelector(state, '_createNewVariable');
2323
const isNewVariable = !!createNewVariable;
24-
2524
const existingVariables = getVariablesForSubject(state, { entity, type });
2625
const variableOptions = getVariableOptionsForSubject(state, { entity, type })
2726
// If not a variable with corresponding component, we can't use it here.
@@ -74,7 +73,7 @@ const fieldsHandlers = withHandlers({
7473
if (variableType !== typeForComponent) {
7574
changeField(form, 'validation', null);
7675
changeField(form, 'options', null);
77-
// Special case for boolean, where BooleanChoice has options but Toggle doesn't
76+
// Special case for boolean, where BooleanChoice has options but Toggle doesn't
7877
} else if (variableType === 'boolean') {
7978
changeField(form, 'options', null);
8079
}

src/selectors/codebook/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, find } from 'lodash';
1+
import { get, find, isObject } from 'lodash';
22
import { getCodebook } from '../protocol';
33
import { asOptions } from '../utils';
44
import { makeOptionsWithIsUsed } from './isUsed';
@@ -23,24 +23,36 @@ const getType = (state, subject) => {
2323
*/
2424
const getVariablesForSubject = (state, subject) => get(getType(state, subject), 'variables', {});
2525

26-
const getAllVariablesByUUID = ({ node: nodeTypes = {}, edge: edgeTypes = {}, ego = {} }) => {
26+
const getAllVariablesByUUID = (codebook) => {
27+
if (!codebook) { throw new Error('Codebook not found'); }
28+
29+
const { node: nodeTypes = {}, edge: edgeTypes = {}, ego = {} } = codebook;
2730
const flattenedVariables = {};
2831

2932
const addVariables = (variables) => {
33+
if (!variables) { return; }
34+
if (!isObject(variables)) { throw new Error('Variables must be an object'); }
35+
3036
Object.keys(variables).forEach((variable) => {
3137
flattenedVariables[variable] = variables[variable];
3238
});
3339
};
3440

35-
Object.values(nodeTypes).forEach((nodeType) => {
36-
addVariables(nodeType.variables);
37-
});
41+
if (nodeTypes && nodeTypes.variables) {
42+
Object.values(nodeTypes).forEach((nodeType) => {
43+
addVariables(nodeType.variables);
44+
});
45+
}
3846

39-
Object.values(edgeTypes).forEach((edgeType) => {
40-
addVariables(edgeType.variables);
41-
});
47+
if (edgeTypes && edgeTypes.variables) {
48+
Object.values(edgeTypes).forEach((edgeType) => {
49+
addVariables(edgeType.variables);
50+
});
51+
}
4252

43-
addVariables(ego.variables);
53+
if (ego.variables) {
54+
addVariables(ego.variables);
55+
}
4456
return flattenedVariables;
4557
};
4658

0 commit comments

Comments
 (0)