Skip to content

Instantly share code, notes, and snippets.

@rolandpeelen
Last active August 18, 2019 14:32
Show Gist options
  • Select an option

  • Save rolandpeelen/076cd9f4162c50bb4e757569e8e1cafc to your computer and use it in GitHub Desktop.

Select an option

Save rolandpeelen/076cd9f4162c50bb4e757569e8e1cafc to your computer and use it in GitHub Desktop.
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