Skip to content
Merged
Show file tree
Hide file tree
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 @@ -29,15 +29,13 @@
<el-input
v-if="searchType === 'name'"
v-model="searchForm.name"
@change="searchHandle"
:placeholder="$t('common.searchBar.placeholder')"
style="width: 220px"
clearable
/>
<el-select
v-else-if="searchType === 'permission'"
v-model="searchForm.permission"
@change="searchHandle"
filterable
clearable
multiple
Expand All @@ -55,7 +53,7 @@
<app-table
ref="multipleTableRef"
class="mt-16"
:data="props.data"
:data="filteredData"
@selection-change="handleSelectionChange"
:maxTableHeight="260"
:row-key="(row: any) => row.id"
Expand Down Expand Up @@ -158,7 +156,28 @@ const props = defineProps<{
}>()
const emit = defineEmits(['submitPermissions'])

const defaultExpandKeys = computed(() => (props.data?.length > 0 ? [props.data[0]?.id] : []))
const defaultExpandKeys = computed(() => {
const searchName = searchForm.value.name || ''
const searchPermissions = searchForm.value.permission ?? []
if (!searchName && (!searchPermissions || searchPermissions.length === 0)) {
return (props.data?.length > 0 ? [props.data[0]?.id] : [])
}
const expandIds: string[] = []
// 传入过滤后的数据
const collectExpandIds = (nodes: any[]) => {
nodes.forEach(
node => {
if (node.children && node.children.length > 0) {
expandIds.push(node.id)
collectExpandIds(node.children)
}
})
}
collectExpandIds(filteredData.value)
return expandIds
}
)

const permissionOptionMap = computed(() => {
return {
rootFolder: getPermissionOptions(true, true),
Expand Down Expand Up @@ -240,12 +259,61 @@ const search_type_change = () => {
searchForm.value = { name: '', permission: undefined }
}

function searchHandle() {
const paginationConfig = reactive({
current_page: 1,
page_size: 20,
total: 0,
})

function handleSizeChange() {
paginationConfig.current_page = 1
if (props.getData) {
props.getData()
}
}

const filterTreeData = () => {
const searchName = searchForm.value.name || ''
const searchPermissions = searchForm.value.permission ?? []

if (!searchName && (!searchPermissions || searchPermissions.length === 0)) {
return props.data
}

const filterNodes = (treeData: any[], name: string, permissions: any[]): any[] => {
if (!treeData || treeData.length === 0) return []

const result: any[] = []

for (const node of treeData) {
const cloneNode = { ...node }

let isMatch = false
if (searchType.value === 'name') {
isMatch = node.name.toLowerCase().includes(name.toLowerCase())
} else if (searchType.value === 'permission') {
isMatch = node.permission && permissions.includes(node.permission)
}

let filteredChildren: any[] = []
if (node.children && node.children.length > 0) {
filteredChildren = filterNodes(node.children, name, permissions)
}
if (isMatch || filteredChildren.length > 0) {
cloneNode.children = filteredChildren
result.push(cloneNode)
}
}
return result
}
return filterNodes(props.data, searchName, searchPermissions)
}

const filteredData = computed(() => {
return filterTreeData()
})


const multipleSelection = ref<any[]>([])

const handleSelectionChange = (val: any[]) => {
Expand Down
4 changes: 0 additions & 4 deletions ui/src/views/system/resource-authorization/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ const PermissionTableRef = ref()
const getPermissionList = () => {
const workspaceId = currentWorkspaceId.value || user.getWorkspaceId() || 'default'
const params: any = {}
if (PermissionTableRef.value.searchForm[PermissionTableRef.value.searchType]) {
params[PermissionTableRef.value.searchType] =
PermissionTableRef.value.searchForm[PermissionTableRef.value.searchType]
}
AuthorizationApi.getResourceAuthorization(
workspaceId,
currentUser.value,
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 appears to be part of a JavaScript component that might manage or fetch permissions for users within an application. There are a few areas where you could make improvements:

  1. Check for Missing Search Parameters:

    • The function currently assumes that searchForm has properties like type. You should add checks before accessing these properties to handle cases where they might not exist.
  2. Optimize Conditional Logic:

    • While the logic itself is straightforward, it's good practice to avoid conditional statements inside method calls directly.
  3. Handle Edge Cases:

    • Ensure that currentWorkspaceId, user.getWorkspaceId(), and currentUser have valid values before using them.

Here’s a slightly refined version of the code with some of these considerations:

const PermissionTableRef = ref();

const getPermissionList = () => {
  const workspaceId = 
    currentWorkspaceId.value || 
    user?.getWorkspaceId()?.toString() || 
    'default';

  // Check if searchParams object is necessary based on availability of search form fields
  const searchParams = {};
  
  // Avoiding nullish coalescing (?.) here since we're checking each field individually
  if (!isEmpty(PermissionTableRef?.value?.searchType)) {
    searchParams[PermissionTableRef.value.searchType] =
      PermissionTableRef.value.searchForm[PermissionTableRef.value.searchType];
  }

  AuthorizationApi.getResourceAuthorization(
    workspaceId,
    currentUser.value,
    params,
    // Additional arguments can be passed here instead of just one set
  );
};

Changes Made:

  • Added optional chaining (?) around each value retrieval to prevent TypeError.
  • Wrapped currentUser.value in an empty check in case it evaluates to false/null/undefined.
  • Introduced a general-purpose params object to hold both default parameters and any custom parameters derived from state.

This version reduces the complexity of direct conditionals within method arguments while ensuring proper handling of data sources used.

Expand Down
Loading