Look at the image:
I'mtryingtocloneapageusingimmutableconcepts.
EachpagehassomecharacteristicsandthecharacteristicthatImustpreparebeforesavingthepageinthestateisthe"localId"
By clicking the "clone" link the following code is called:
export const clonePageByLocalId = (state:any, sourcePageLocalId:string, position:number) => {
// get the source page and clone it
let sourcePageNode = getPageByLocalId( state, sourcePageLocalId )
sourcePageNode = sourcePageNode.set('localId', v4() )
let sourcePageNodeModify = prepareLocalId(sourcePageNode )
let sourcePageNodeModify = sourcePageNodeModify.set('title', sourcePageNodeModify.get('title') + '(clone)');
sourcePageNodeModify = sourcePageNodeModify.set('title', sourcePageNodeModify.get('title') + '(clone)');
// ---> insert the cloned page into new location
const newState = insertPage(state, sourcePageLocalId, position + 1, sourcePageNodeModify )
return newState
}
Prepare localIds:
const prepareLocalId = (sourcePageNode:any) => {
let sourcePageNodeModify = sourcePageNode.set('localId', v4() )
if ( sourcePageNodeModify.get('pages') && sourcePageNodeModify.get('pages').size > 0) {
sourcePageNodeModify.get('pages').forEach(function (page) {
prepareLocalId( page )
});
}
return sourcePageNode
}
The clone is happening, but by clicking on "page 2 (clone)" and trying to expand the "Page Two B" page that has effect is "page 2". This is happening because the localId is not actually being changed.
Well, could you help me find a better way that I can iterate over the pages and set a value on your "localId" property?.
Hugs.