|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import { useParams } from 'react-router-dom'; |
| 3 | +import { useIntl } from '@edx/frontend-platform/i18n'; |
| 4 | +import { Container } from '@openedx/paragon'; |
| 5 | +import { ROUTES } from '@src/authz-module/constants'; |
| 6 | +import AuthZLayout from '../components/AuthZLayout'; |
| 7 | +import { useLibraryAuthZ } from './context'; |
| 8 | +import RoleCard from '../components/RoleCard'; |
| 9 | +import { useLibrary, useTeamMembers } from '../data/hooks'; |
| 10 | +import { buildPermissionsByRoleMatrix } from './utils'; |
| 11 | + |
| 12 | +import messages from './messages'; |
| 13 | + |
| 14 | +const LibrariesUserManager = () => { |
| 15 | + const intl = useIntl(); |
| 16 | + const { username } = useParams(); |
| 17 | + const { |
| 18 | + libraryId, permissions, roles, resources, |
| 19 | + } = useLibraryAuthZ(); |
| 20 | + const { data: library } = useLibrary(libraryId); |
| 21 | + const rootBreadcrumb = intl.formatMessage(messages['library.authz.breadcrumb.root']) || ''; |
| 22 | + const pageManageTitle = intl.formatMessage(messages['library.authz.manage.page.title']); |
| 23 | + |
| 24 | + const { data: teamMembers } = useTeamMembers(libraryId); |
| 25 | + const user = teamMembers?.find(member => member.username === username); |
| 26 | + const userRoles = useMemo(() => { |
| 27 | + const assignedRoles = roles.filter(role => user?.roles.includes(role.role)) |
| 28 | + .map(role => ({ |
| 29 | + ...role, |
| 30 | + permissions: buildPermissionsByRoleMatrix({ |
| 31 | + rolePermissions: role.permissions, permissions, resources, intl, |
| 32 | + }), |
| 33 | + })); |
| 34 | + return assignedRoles; |
| 35 | + }, [roles, user?.roles, permissions, resources, intl]); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="authz-libraries"> |
| 39 | + <AuthZLayout |
| 40 | + context={{ id: libraryId, title: library.title, org: library.org }} |
| 41 | + navLinks={[{ label: rootBreadcrumb }, { label: pageManageTitle, to: `/authz/${ROUTES.LIBRARIES_TEAM_PATH.replace(':libraryId', libraryId)}` }]} |
| 42 | + activeLabel={user?.username || ''} |
| 43 | + pageTitle={user?.username || ''} |
| 44 | + pageSubtitle={<p>{user?.email}</p>} |
| 45 | + actions={[]} |
| 46 | + > |
| 47 | + <Container className="bg-light-200 p-5"> |
| 48 | + {userRoles && userRoles.map(role => ( |
| 49 | + <RoleCard |
| 50 | + key={`${role}-${username}`} |
| 51 | + title={role.name} |
| 52 | + objectName={library.title} |
| 53 | + description={role.description} |
| 54 | + showDelete |
| 55 | + permissions={role.permissions as any[]} |
| 56 | + /> |
| 57 | + ))} |
| 58 | + </Container> |
| 59 | + </AuthZLayout> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +export default LibrariesUserManager; |
0 commit comments