1
Look at the picture:
I’m trying to clone a page using the concepts of immutable.
Each page has some features and the feature I must prepare before saving the page in the state(state) is "localId"
By clicking on 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 the 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, however when clicking on "page 2 (clone)" and trying to expand the page "Page Two B" who suffers effect is "page 2". This is happening because localId is not actually being changed.
Guys, could you help me find a better way that I can iterate over the pages and set a value on your "localId property"?.
Hugs.
Hello guys. I was able to find a way to perform this iteration.
– Felipe Araujo