-
-
Notifications
You must be signed in to change notification settings - Fork 23.1k
feat: Add sorting to role assigned users table (workspaces asc, users asc) #5486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,8 @@ import { | |
| useTheme, | ||
| Typography, | ||
| Button, | ||
| Drawer | ||
| Drawer, | ||
| TableSortLabel | ||
| } from '@mui/material' | ||
|
|
||
| // project imports | ||
|
|
@@ -185,6 +186,8 @@ function ShowRoleRow(props) { | |
| const [openViewPermissionsDrawer, setOpenViewPermissionsDrawer] = useState(false) | ||
| const [selectedRoleId, setSelectedRoleId] = useState('') | ||
| const [assignedUsers, setAssignedUsers] = useState([]) | ||
| const [order, setOrder] = useState('asc') | ||
| const [orderBy, setOrderBy] = useState('workspace') | ||
|
|
||
| const theme = useTheme() | ||
| const customization = useSelector((state) => state.customization) | ||
|
|
@@ -196,19 +199,53 @@ function ShowRoleRow(props) { | |
| setSelectedRoleId(roleId) | ||
| } | ||
|
|
||
| const handleRequestSort = (property) => { | ||
| const isAsc = orderBy === property && order === 'asc' | ||
| setOrder(isAsc ? 'desc' : 'asc') | ||
| setOrderBy(property) | ||
| } | ||
|
|
||
| const sortedAssignedUsers = [...assignedUsers].sort((a, b) => { | ||
| let comparison = 0 | ||
|
|
||
| if (orderBy === 'workspace') { | ||
| const workspaceA = (a.workspace?.name || '').toLowerCase() | ||
| const workspaceB = (b.workspace?.name || '').toLowerCase() | ||
| comparison = workspaceA.localeCompare(workspaceB) | ||
| if (comparison === 0) { | ||
| const userA = (a.user?.name || a.user?.email || '').toLowerCase() | ||
| const userB = (b.user?.name || b.user?.email || '').toLowerCase() | ||
| comparison = userA.localeCompare(userB) | ||
| } | ||
| } else if (orderBy === 'user') { | ||
| const userA = (a.user?.name || a.user?.email || '').toLowerCase() | ||
| const userB = (b.user?.name || b.user?.email || '').toLowerCase() | ||
| comparison = userA.localeCompare(userB) | ||
| if (comparison === 0) { | ||
| const workspaceA = (a.workspace?.name || '').toLowerCase() | ||
| const workspaceB = (b.workspace?.name || '').toLowerCase() | ||
| comparison = workspaceA.localeCompare(workspaceB) | ||
| } | ||
| } | ||
|
|
||
| return order === 'asc' ? comparison : -comparison | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| if (getAllUsersByRoleIdApi.data) { | ||
| setAssignedUsers(getAllUsersByRoleIdApi.data) | ||
| } | ||
| }, [getAllUsersByRoleIdApi.data]) | ||
|
|
||
| useEffect(() => { | ||
| if (open && selectedRoleId) { | ||
| if (openAssignedUsersDrawer && selectedRoleId) { | ||
| getAllUsersByRoleIdApi.request(selectedRoleId) | ||
| } else { | ||
| setOpenAssignedUsersDrawer(false) | ||
| setSelectedRoleId('') | ||
| setAssignedUsers([]) | ||
| setOrder('asc') | ||
| setOrderBy('workspace') | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [openAssignedUsersDrawer]) | ||
|
|
@@ -301,12 +338,28 @@ function ShowRoleRow(props) { | |
| }} | ||
| > | ||
| <TableRow> | ||
| <StyledTableCell sx={{ width: '50%' }}>User</StyledTableCell> | ||
| <StyledTableCell sx={{ width: '50%' }}>Workspace</StyledTableCell> | ||
| <StyledTableCell sx={{ width: '50%' }}> | ||
| <TableSortLabel | ||
| active={orderBy === 'user'} | ||
| direction={orderBy === 'user' ? order : 'asc'} | ||
| onClick={() => handleRequestSort('user')} | ||
| > | ||
| User | ||
| </TableSortLabel> | ||
| </StyledTableCell> | ||
| <StyledTableCell sx={{ width: '50%' }}> | ||
| <TableSortLabel | ||
| active={orderBy === 'workspace'} | ||
| direction={orderBy === 'workspace' ? order : 'asc'} | ||
| onClick={() => handleRequestSort('workspace')} | ||
| > | ||
| Workspace | ||
| </TableSortLabel> | ||
| </StyledTableCell> | ||
| </TableRow> | ||
| </TableHead> | ||
| <TableBody> | ||
| {assignedUsers.map((item, index) => ( | ||
| {sortedAssignedUsers.map((item, index) => ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the array index as a For example, if |
||
| <TableRow key={index}> | ||
| <StyledTableCell>{item.user.name || item.user.email}</StyledTableCell> | ||
| <StyledTableCell>{item.workspace.name}</StyledTableCell> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sorting logic is a bit repetitive. You can simplify it to be more concise and adhere to the DRY (Don't Repeat Yourself) principle. By determining the primary and secondary sorting properties based on
orderByfirst, you can avoid the duplicatedif (comparison === 0)blocks and make the code easier to maintain.