Last active
August 18, 2019 14:32
-
-
Save rolandpeelen/076cd9f4162c50bb4e757569e8e1cafc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { lensPath, lensProp, compose } from 'ramda'; | |
| // Some new helper functions | |
| const append = item => array => [...(array || []), item]; | |
| const findById = id => array => array.find(item => item._id === id); | |
| const sceneLens = lensProp('scene'); | |
| const structureLens = lensProp('structure'); | |
| const objectsLens = lensProp('objects'); | |
| const groupsLens = lensProp('groups'); | |
| const structureObjectLens = compose(sceneLens, structureLens, objectsLens); | |
| const structureGroupLens = compose(sceneLens, structureLens, groupsLens); | |
| switch(state = defaultState, action) { | |
| case ADD_OBJECT_TO_STRUCTURE: return over(structureObjectLens, append({ name: 'New Object' }), state); | |
| case ADD_OBJECT_TO_GROUP: { | |
| const oldGroup = view(structureGroupLens, state).find(findById(action.data.id)); | |
| const newGroup = over(objectLens, append({ name: 'New Object' }), group); | |
| const groups = view(structureGroupLens, state).length > 0 ? view(structureGroupLens, state).map(replaceFully(oldGroup, newGroup)) : [newGroup]; | |
| return set(structureGroupLens, groups, state); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment