How to use the object-path-immutable.insert function in object-path-immutable

To help you get started, we’ve selected a few object-path-immutable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elastic / kibana / x-pack / plugins / canvas / public / state / reducers / pages.js View on Github external
const pageIndex = getPageIndexById(workpadState, id);
      const newIndex = pageIndex + position;

      // TODO: do something better when given an invalid page id
      if (pageIndex < 0) {
        return workpadState;
      }

      // don't move pages past the first or last position
      if (newIndex < 0 || newIndex >= workpadState.pages.length) {
        return workpadState;
      }

      // remove and re-insert the page
      const page = { ...workpadState.pages[pageIndex] };
      let newState = insert(del(workpadState, `pages.${pageIndex}`), 'pages', page, newIndex);

      // adjust the selected page index and return the new state
      const selectedId = workpadState.pages[workpadState.page].id;
      const newSelectedIndex = newState.pages.findIndex(page => page.id === selectedId);
      newState = set(newState, 'page', newSelectedIndex);

      // changes to the page require navigation
      const router = routerProvider();
      router.navigateTo('loadWorkpad', { id: newState.id, page: newState.page + 1 });

      return newState;
    },
github elastic / kibana / x-pack / plugins / canvas / public / state / reducers / pages.js View on Github external
function addPage(workpadState, payload, srcIndex = workpadState.pages.length - 1) {
  return insert(workpadState, 'pages', payload || getDefaultPage(), srcIndex + 1);
}

object-path-immutable

Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.

MIT
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis