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 @@ -319,7 +319,6 @@ const select = (val: any[], active: any) => {
selectObj[active.id] = 0
}
if (selectObj[active.id] % 2 == 0) {
console.log(TreeToFlatten([active]))
TreeToFlatten([active])
.filter((item: any) => item.id != active.id)
.forEach((item: any) => {
Expand Down Expand Up @@ -371,6 +370,27 @@ function submitPermissions(value: string, row: any) {
permission: value,
},
]
const emitSubmitPermissions = (treeData: any[], ids: Array<string>, result: Array<any>) => {
if (!treeData || treeData.length === 0) return []
for (const node of treeData) {
const isRecursion = node.permission == 'NOT_AUTH' && ids.includes(node.id)
if (node.children && node.children.length > 0 && !isRecursion) {
emitSubmitPermissions(node.children, ids, result)
}
const isMatch = node.permission == 'NOT_AUTH' && ids.includes(node.id)
if (isMatch) {
ids.push(node.folder_id)
result.push({
target_id: node.id,
permission: 'VIEW',
})
}
}
return result
}
if (['VIEW', 'MANAGE', 'ROLE'].includes(value)) {
emitSubmitPermissions(props.data, [row.folder_id], obj)
}
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 code looks well structured but has some optimizations and improvements that could enhance performance and readability.

  1. Optimization:

    • In the emitSubmitPermissions function, you can use array methods like .map() and recursion directly without converting to a flat array with unnecessary steps. Consider using nested loops for more efficient manipulation of children arrays.
  2. Avoid Global State:

    • The variables obj, permissionValue, and rowFolderId are defined within different scopes (submitPermissions vs. props) but used across both. This might lead to unexpected side effects if they aren't properly scoped or initialized.
  3. Variable Naming:

    • Names such as TreeToFlatten and emitterSubmitPermissions don't clearly state what they represent. It's better to have descriptive names.
  4. Return Statement in submitPermissions:

    • The return null; statement is not needed after emitting the event because it doesn't affect the flow. Removing it can make the code cleaner.

Here’s an improved version based on these points:

function submitPermissions(value: string, row: any) {
  const permissionObject: { [id: number]: number } = {}
  if (value !== 'NOT_AUTH') {
    Object.assign(permissionObject, {[active.id]: 0})
  }

  const filteredItems = TreeToFlatten([active])
    .filter(item => item.id !== active.id)

  let resultArr = []

  if (['VIEW', 'MANAGE', 'ROLE'].includes(value)) {
    const emitSubmitPermissions = (treeData: any[], ids: Array<string>, result: Array<any>): void => {
      for (const node of treeData) {
        const isMatch = node.permission === 'NOT_AUTH' && ids.includes(node.id)
        if (isMatch) {
          ids.push(node.folder_id)
          result.push({target_id: node.id, permission: 'VIEW'})
        }
        if (node.children?.length > 0) {
          emitSubmitPermissions(node.children, ids.slice(), [...result]);
        }
      }
    };

    emitSubmitPermissions(props.data, [row.folder_id], []);
  }

  emit('submitPermissions', permissionObject);
}

Key Changes:

  • Used Object.assign instead of initializing with {} and assignment.
  • Renamed functions and parameters for clarity.
  • Removed unneeded variables.
  • Improved loop structure for emitSubmitPermissions.
  • Ensured the event emission logic remains simple and clear.

Expand Down
Loading