-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from codaco/fix/refactor-narrative-selector
Refactor narrative selectors to defer to codebook selectors
- Loading branch information
Showing
4 changed files
with
33 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 21 additions & 46 deletions
67
src/components/StageEditor/sections/NarrativePresets/selectors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,38 @@ | ||
import { get, map, reduce } from 'lodash'; | ||
import { map } from 'lodash'; | ||
import { getCodebook } from '../../../../selectors/protocol'; | ||
import { getVariableOptionsForNodeType } from '../../../../selectors/codebook'; | ||
|
||
// TODO: refactor this to use new selector | ||
export const getVariablesForNodeType = (state, props) => { | ||
const nodeType = props.nodeType; | ||
const codebook = getCodebook(state); | ||
return get(codebook, ['node', nodeType, 'variables'], {}); | ||
}; | ||
export const getLayoutVariablesForNodeType = (state, nodeType) => { | ||
const variables = getVariableOptionsForNodeType(state, nodeType); | ||
|
||
const asOption = (value, key) => | ||
({ | ||
label: get(value, 'name', ''), | ||
value: key, | ||
color: get(value, 'color', ''), | ||
}); | ||
|
||
const filterAsOption = rule => | ||
(memo, item, id) => { | ||
if (!rule(item)) { return memo; } | ||
return [ | ||
...memo, | ||
asOption(item, id), | ||
]; | ||
}; | ||
|
||
export const getLayoutVariablesForNodeType = (state, props) => { | ||
const variables = getVariablesForNodeType(state, props); | ||
|
||
return reduce( | ||
variables, | ||
filterAsOption(item => item.type === 'layout'), | ||
[], | ||
); | ||
return variables.filter(item => item.type === 'layout'); | ||
}; | ||
|
||
export const getHighlightVariablesForNodeType = (state, props) => { | ||
const variables = getVariablesForNodeType(state, props); | ||
export const getHighlightVariablesForNodeType = (state, nodeType) => { | ||
const variables = getVariableOptionsForNodeType(state, nodeType); | ||
|
||
return reduce( | ||
variables, | ||
filterAsOption(item => item.type === 'boolean'), | ||
[], | ||
); | ||
return variables.filter(item => item.type === 'boolean'); | ||
}; | ||
|
||
export const getGroupVariablesForNodeType = (state, props) => { | ||
const variables = getVariablesForNodeType(state, props); | ||
export const getGroupVariablesForNodeType = (state, nodeType) => { | ||
const variables = getVariableOptionsForNodeType(state, nodeType); | ||
|
||
const options = reduce( | ||
variables, | ||
filterAsOption(item => item.type === 'categorical'), | ||
[{ label: '\u2014 None \u2014', value: '' }], | ||
const categoricalOptions = variables.filter( | ||
item => item.type === 'categorical', | ||
); | ||
|
||
return options; | ||
return [ | ||
{ label: '\u2014 None \u2014', value: '' }, | ||
...categoricalOptions, | ||
]; | ||
}; | ||
|
||
export const getEdgesForNodeType = (state) => { | ||
const codebook = getCodebook(state); | ||
|
||
return map(codebook.edge, asOption); | ||
return map(codebook.edge, (edge, edgeId) => ({ | ||
label: edge.name, | ||
color: edge.color, | ||
value: edgeId, | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters