Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,39 @@ function closeDialog() {
multipleSelection.value = []
multipleTableRef.value?.clearSelection()
}
function getResourcesByFolderId(treeData: any[], folderId: string): any[] {
const result: any[] = []
let target: any = null

function dfs(nodes: any[]) {
for (const node of nodes) {
if (node.id === folderId) {
target = node
return
}
if (node.children?.length) {
dfs(node.children)
if (target) return
}
}
}

function collect(node: any) {
if (!node?.children) return
for (const child of node.children) {
result.push(child)
collect(child)
}
}

dfs(treeData)

if (target) {
collect(target)
}

return result
}
function submitPermissions(value: string, row: any) {
const obj = [
{
Expand All @@ -409,9 +441,15 @@ function submitPermissions(value: string, row: any) {
}
return result
}

if (['VIEW', 'MANAGE', 'ROLE'].includes(value)) {
emitSubmitPermissions(props.data, [row.folder_id], obj)
}
if (['NOT_AUTH'].includes(value) && 'folder' == row.resource_type) {
getResourcesByFolderId(props.data, row.id).forEach((n) => {
obj.push({ target_id: n.id, permission: 'NOT_AUTH' })
})
}
emit('submitPermissions', obj)
}
const provider_list = ref<Array<Provider>>([])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code generally looks clean and efficient. However, there are a few areas of improvement:

  1. Return early from dfs function: Inside the dfs function, when you find the target node (matching folderId), you should immediately return to avoid unnecessary iterations over subsequent nodes.
if (node.id === folderId) {
  target = node;
  return; // This ensures that no more iterations happen if we found the node.
}

With this change, the function will not continue searching further after finding the matching node, which can improve performance.

  1. Simplifying conditional checks in submitPermissions:
    function submitPermissions(value: string, row: any) {
      const obj = [
        ...row.permissions || [], // Merge existing permissions with new ones to handle undefined case.
      ];
    
      if ('VIEW'.includes(value)) {
        obj.unshift({ permission: value });
        emitSubmitPermissions(...props.data);
        return;
      }
    
      const { id } = row;
      
      // Handle role management cases first
      ...
    
      // Not authorized case specifically for folders
      if ('NOT_AUTH' == row.resource_type && ['NOT_AUTH'].includes(value)) {
        getResourcesByFolderId(props.data, id).forEach((n) => {
          obj.push({ target_id: n.id, permission: 'NOT_AUTH' });
        });
      }
    
      emit('submitPermissions', obj)
    }

This version simplifies how roles are handled in addition to managing specific resource types (NOT_AUTH) for folders, making the function cleaner and potentially reducing boilerplate logic.

  1. Consistent naming conventions: Functions like getResourcesByFolderId, even though written as JavaScript, follow a convention typical in TypeScript or React components. It might be useful to maintain consistency depending on what other languages/technologies you use across your project.

Expand Down