|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import { useIntl } from '@edx/frontend-platform/i18n'; |
| 3 | +import debounce from 'lodash.debounce'; |
| 4 | +import { |
| 5 | + Container, DataTable, |
| 6 | +} from '@openedx/paragon'; |
| 7 | +import TableFooter from '@src/authz-module/components/TableFooter/TableFooter'; |
| 8 | +import { TABLE_DEFAULT_PAGE_SIZE } from '@src/authz-module/constants'; |
| 9 | +import AuthZLayout from '@src/authz-module/components/AuthZLayout'; |
| 10 | +import { useNavigate, useParams } from 'react-router-dom'; |
| 11 | +import { useUserAccount } from '@src/data/hooks'; |
| 12 | +import baseMessages from '@src/authz-module/messages'; |
| 13 | +import AddRoleButton from '@src/authz-module/components/AddRoleButton'; |
| 14 | +import { RoleCell } from '@src/authz-module/components/TableCells'; |
| 15 | +import { useQuerySettings } from '@src/authz-module/hooks/useQuerySettings'; |
| 16 | +import { useUserAssignedRoles } from '@src/authz-module/data/hooks'; |
| 17 | +import messages from './messages'; |
| 18 | +import { ViewAllPermissionsCell, ActionsCell, PermissionsCell } from './CustomCells'; |
| 19 | + |
| 20 | +const dummyData = [ |
| 21 | + { |
| 22 | + role: 'Course Admin', |
| 23 | + organization: 'edX', |
| 24 | + scope: 'Course: Demo Course', |
| 25 | + permissions: ['View', 'Edit', 'Delete'], |
| 26 | + }, |
| 27 | + { |
| 28 | + role: 'Course Auditor', |
| 29 | + organization: 'edX', |
| 30 | + scope: 'Course: Demo Course 2', |
| 31 | + permissions: ['View', 'Edit'], |
| 32 | + }, |
| 33 | + { |
| 34 | + role: 'Super Admin', |
| 35 | + organization: 'edX', |
| 36 | + scope: 'Course: Demo Course', |
| 37 | + permissions: ['View', 'Edit', 'Delete'], |
| 38 | + }, |
| 39 | + { |
| 40 | + role: 'Course Auditor', |
| 41 | + organization: 'edX', |
| 42 | + scope: 'Course: Demo Course 2', |
| 43 | + permissions: ['View', 'Edit'], |
| 44 | + }, |
| 45 | + { |
| 46 | + role: 'Global Staff', |
| 47 | + organization: 'edX', |
| 48 | + scope: 'Course: Demo Course', |
| 49 | + permissions: ['View', 'Edit', 'Delete'], |
| 50 | + }, |
| 51 | + { |
| 52 | + role: 'Course Auditor', |
| 53 | + organization: 'edX', |
| 54 | + scope: 'Course: Demo Course 2', |
| 55 | + permissions: ['View', 'Edit'], |
| 56 | + }, |
| 57 | + { |
| 58 | + role: 'Course Admin', |
| 59 | + organization: 'edX', |
| 60 | + scope: 'Course: Demo Course', |
| 61 | + permissions: ['View', 'Edit', 'Delete'], |
| 62 | + }, |
| 63 | + { |
| 64 | + role: 'Course Auditor', |
| 65 | + organization: 'edX', |
| 66 | + scope: 'Course: Demo Course 2', |
| 67 | + permissions: ['View', 'Edit'], |
| 68 | + }, |
| 69 | + { |
| 70 | + role: 'Course Admin', |
| 71 | + organization: 'edX', |
| 72 | + scope: 'Course: Demo Course', |
| 73 | + permissions: ['View', 'Edit', 'Delete'], |
| 74 | + }, |
| 75 | + { |
| 76 | + role: 'Course Auditor', |
| 77 | + organization: 'edX', |
| 78 | + scope: 'Course: Demo Course 2', |
| 79 | + permissions: ['View', 'Edit'], |
| 80 | + }, |
| 81 | + { |
| 82 | + role: 'Course Admin', |
| 83 | + organization: 'edX', |
| 84 | + scope: 'Course: Demo Course', |
| 85 | + permissions: ['View', 'Edit', 'Delete'], |
| 86 | + }, |
| 87 | + { |
| 88 | + role: 'Course Auditor', |
| 89 | + organization: 'edX', |
| 90 | + scope: 'Course: Demo Course 2', |
| 91 | + permissions: ['View', 'Edit'], |
| 92 | + }, |
| 93 | + { |
| 94 | + role: 'Course Admin', |
| 95 | + organization: 'edX', |
| 96 | + scope: 'Course: Demo Course', |
| 97 | + permissions: ['View', 'Edit', 'Delete'], |
| 98 | + }, |
| 99 | + { |
| 100 | + role: 'Course Auditor', |
| 101 | + organization: 'edX', |
| 102 | + scope: 'Course: Demo Course 2', |
| 103 | + permissions: ['View', 'Edit'], |
| 104 | + }, |
| 105 | +]; |
| 106 | + |
| 107 | +const AuditUserPage = () => { |
| 108 | + const { formatMessage } = useIntl(); |
| 109 | + const { username } = useParams(); |
| 110 | + const navigate = useNavigate(); |
| 111 | + const { isLoading: isLoadingUser, data: user } = useUserAccount(username ?? ''); |
| 112 | + const { querySettings, handleTableFetch } = useQuerySettings(); |
| 113 | + // TODO: use actual assigned roles data when API is ready, currently using dummy data for development purpose |
| 114 | + const { data: _userAssignedRoles } = useUserAssignedRoles(username ?? '', querySettings); |
| 115 | + const authzHomePath = '/authz'; |
| 116 | + if (!user && !isLoadingUser) { |
| 117 | + navigate(authzHomePath); |
| 118 | + } |
| 119 | + const navLinks = [ |
| 120 | + { |
| 121 | + label: formatMessage(baseMessages['authz.management.home.nav.link']), |
| 122 | + to: authzHomePath, |
| 123 | + }, |
| 124 | + ]; |
| 125 | + const additionalColumns = [ |
| 126 | + { |
| 127 | + id: 'view_permissions', |
| 128 | + Header: '', |
| 129 | + Cell: ViewAllPermissionsCell, |
| 130 | + }, |
| 131 | + { |
| 132 | + id: 'action', |
| 133 | + Header: formatMessage(messages['authz.user.table.action.column.header']), |
| 134 | + Cell: ActionsCell, |
| 135 | + }, |
| 136 | + ]; |
| 137 | + const columns = [ |
| 138 | + { |
| 139 | + Header: formatMessage(messages['authz.user.table.role.column.header']), |
| 140 | + accessor: 'role', |
| 141 | + Cell: RoleCell, |
| 142 | + }, |
| 143 | + { |
| 144 | + Header: formatMessage(messages['authz.user.table.organization.column.header']), |
| 145 | + accessor: 'organization', |
| 146 | + }, |
| 147 | + { |
| 148 | + Header: formatMessage(messages['authz.user.table.scope.column.header']), |
| 149 | + accessor: 'scope', |
| 150 | + disableFilters: true, |
| 151 | + }, |
| 152 | + { |
| 153 | + Header: formatMessage(messages['authz.user.table.permissions.column.header']), |
| 154 | + Cell: PermissionsCell, |
| 155 | + disableFilters: true, |
| 156 | + disableSortBy: true, |
| 157 | + }, |
| 158 | + ]; |
| 159 | + const pageCount = dummyData?.length ? Math.ceil(dummyData.length / TABLE_DEFAULT_PAGE_SIZE) : 1; |
| 160 | + |
| 161 | + const fetchData = useMemo(() => debounce(handleTableFetch, 500), [handleTableFetch]); |
| 162 | + |
| 163 | + return ( |
| 164 | + <div className="authz-module"> |
| 165 | + <AuthZLayout |
| 166 | + context={{ |
| 167 | + id: '', |
| 168 | + org: '', |
| 169 | + title: '', |
| 170 | + }} |
| 171 | + navLinks={navLinks} |
| 172 | + activeLabel={username || ''} |
| 173 | + pageTitle={user?.username || ''} |
| 174 | + pageSubtitle={user?.email || ''} |
| 175 | + actions={ |
| 176 | + [ |
| 177 | + <AddRoleButton presetUsername={user?.username} key="add-role-button" />, |
| 178 | + ] |
| 179 | + } |
| 180 | + > |
| 181 | + <Container className="bg-light-200 p-5"> |
| 182 | + <DataTable |
| 183 | + isPaginated |
| 184 | + manualPagination |
| 185 | + data={dummyData} |
| 186 | + fetchData={fetchData} |
| 187 | + itemCount={dummyData?.length || 0} |
| 188 | + pageCount={pageCount} |
| 189 | + initialState={{ pageSize: TABLE_DEFAULT_PAGE_SIZE }} |
| 190 | + additionalColumns={additionalColumns} |
| 191 | + columns={columns} |
| 192 | + > |
| 193 | + <DataTable.Table /> |
| 194 | + <TableFooter /> |
| 195 | + </DataTable> |
| 196 | + |
| 197 | + </Container> |
| 198 | + </AuthZLayout> |
| 199 | + </div> |
| 200 | + ); |
| 201 | +}; |
| 202 | + |
| 203 | +export default AuditUserPage; |
0 commit comments