Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
},
function addPage(workpadState, payload, srcIndex = workpadState.pages.length - 1) {
return insert(workpadState, 'pages', payload || getDefaultPage(), srcIndex + 1);
}