I have an infinitely deep folders tree-array. Each folder have a "hasPermission" boolean. What I want to achieve is when a parent folder's (that can be located anywhere in the tree) "hasPermission" changes, all it's children "hasPermission" will also change - of course, the grandparent folders "hasPermission" will not change.
For example, in the folder tree below. If folderId: '2.1' changes permission, folderId: '3.1' will also change; the others will stay the same.
const example = [
{
folderId: '1',
hasPermission: false,
children: [
{
folderId: '2.1',
hasPermission: false,
children: [
{
folderId: '3.1',
hasPermission: false,
children: [],
},
],
},
{
folderId: '2.2',
hasPermission: false,
children: [],
},
],
},
];
This function changes the "hasPermission" recursively.
const changeChildrenPermission = (
folders
) => {
return folders.map(
({ id, children, hasPermission }) => ({
id,
hasPermission: (hasPermission === false && true) || false,
children: changeChildrenPermission(children),
})
);
};
Here is what I have so far, I'm trying to look up the tree for the selectedId, if it matches, then call the "changeChildrenPermission" function.
const setPermissionChange = (
folders,
selectedId
) => {
const a = folders.map((folder) => {
if (folder.id === selectedId) {
return {
id: folder.folderId,
hasPermission: (hasPermission === false && true) || false,
children: changeChildrenPermission(folder.children),
};
});
};
Not sure what to do afterwards.