Skip to content

Commit 0e5b53f

Browse files
committed
feat: create a toggle component to make the library public read
1 parent 4ed28ad commit 0e5b53f

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

src/authz-module/libraries-manager/LibrariesTeamManager.test.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { screen, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { renderWrapper } from '@src/setupTest';
44
import { initializeMockApp } from '@edx/frontend-platform/testing';
5-
import { useLibrary } from '@src/authz-module/data/hooks';
5+
import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
66
import { useLibraryAuthZ } from './context';
77
import LibrariesTeamManager from './LibrariesTeamManager';
88

@@ -18,6 +18,7 @@ const mockedUseLibraryAuthZ = useLibraryAuthZ as jest.Mock;
1818

1919
jest.mock('@src/authz-module/data/hooks', () => ({
2020
useLibrary: jest.fn(),
21+
useUpdateLibrary: jest.fn(),
2122
}));
2223

2324
jest.mock('./components/TeamTable', () => ({
@@ -46,16 +47,24 @@ jest.mock('../components/RoleCard', () => ({
4647
}));
4748

4849
describe('LibrariesTeamManager', () => {
50+
const libraryData = {
51+
id: 'lib-001',
52+
title: 'Test Library',
53+
org: 'Test Org',
54+
allowPublicRead: false,
55+
};
56+
const mutate = jest.fn();
57+
4958
beforeEach(() => {
5059
initializeMockApp({
5160
authenticatedUser: {
5261
username: 'admin',
5362
},
5463
});
5564
mockedUseLibraryAuthZ.mockReturnValue({
56-
libraryId: 'lib-001',
57-
libraryName: 'Mock Library',
58-
libraryOrg: 'MockOrg',
65+
libraryId: libraryData.id,
66+
libraryName: libraryData.title,
67+
libraryOrg: libraryData.org,
5968
username: 'mockuser',
6069
roles: [
6170
{
@@ -74,10 +83,10 @@ describe('LibrariesTeamManager', () => {
7483
});
7584

7685
(useLibrary as jest.Mock).mockReturnValue({
77-
data: {
78-
title: 'Test Library',
79-
org: 'Test Org',
80-
},
86+
data: libraryData,
87+
});
88+
(useUpdateLibrary as jest.Mock).mockReturnValue({
89+
mutate,
8190
});
8291
});
8392

@@ -134,4 +143,18 @@ describe('LibrariesTeamManager', () => {
134143
expect(matrixScope.getByText('edit')).toBeInTheDocument();
135144
expect(matrixScope.getByText('view')).toBeInTheDocument();
136145
});
146+
147+
it('renders allow public library read toggle and change the value by user interaction', async () => {
148+
const user = userEvent.setup();
149+
150+
renderWrapper(<LibrariesTeamManager />);
151+
152+
const readPublicToggle = await screen.findByRole('switch', { name: /Allow public read/i });
153+
154+
await user.click(readPublicToggle);
155+
expect(mutate).toHaveBeenCalledWith({
156+
libraryId: 'lib-001',
157+
updatedData: { allowPublicRead: !libraryData.allowPublicRead },
158+
});
159+
});
137160
});

src/authz-module/libraries-manager/LibrariesTeamManager.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { AddNewTeamMemberTrigger } from './components/AddNewTeamMemberModal';
1414
import { buildPermissionMatrixByResource, buildPermissionMatrixByRole } from './utils';
1515

1616
import messages from './messages';
17+
import PublicReadToggle from './components/PublicReadToggle';
1718

1819
const LibrariesTeamManager = () => {
1920
const intl = useIntl();
@@ -47,7 +48,8 @@ const LibrariesTeamManager = () => {
4748
pageSubtitle={libraryId}
4849
actions={
4950
canManageTeam
50-
? [<AddNewTeamMemberTrigger libraryId={libraryId} />]
51+
? [<PublicReadToggle libraryId={libraryId} key="allow-public-read" />,
52+
<AddNewTeamMemberTrigger libraryId={libraryId} key="add-new-member" />]
5153
: []
5254
}
5355
>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useIntl } from '@edx/frontend-platform/i18n';
2+
import { Form } from '@openedx/paragon';
3+
import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
4+
5+
import messages from './messages';
6+
7+
type PublicReadToggleProps = {
8+
libraryId: string;
9+
};
10+
11+
const PublicReadToggle = ({ libraryId }:PublicReadToggleProps) => {
12+
const intl = useIntl();
13+
const { data: library } = useLibrary(libraryId);
14+
const { mutate: updateLibrary } = useUpdateLibrary();
15+
16+
const onChangeToggle = () => updateLibrary({
17+
libraryId,
18+
updatedData: { allowPublicRead: !library.allowPublicRead },
19+
});
20+
21+
return (
22+
<Form.Switch
23+
checked={library.allowPublicRead}
24+
onChange={onChangeToggle}
25+
helperText={
26+
<span>{intl.formatMessage(messages['libraries.authz.public.read.toggle.subtext'])}</span>
27+
}
28+
>
29+
{intl.formatMessage(messages['libraries.authz.public.read.toggle.label'])}
30+
</Form.Switch>
31+
);
32+
};
33+
34+
export default PublicReadToggle;

src/authz-module/libraries-manager/components/messages.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ const messages = defineMessages({
6161
defaultMessage: 'Remove',
6262
description: 'Libraries AuthZ remove button title',
6363
},
64+
'libraries.authz.public.read.toggle.label': {
65+
id: 'libraries.authz.public.read.toggle.label',
66+
defaultMessage: 'Allow public read',
67+
description: 'Library label toggle to allow public read',
68+
},
69+
'libraries.authz.public.read.toggle.subtext': {
70+
id: 'libraries.authz.public.read.toggle.subtext',
71+
defaultMessage: 'Allows reuse of library content in courses.',
72+
description: 'Library description toggle to allow public read',
73+
},
6474
});
6575

6676
export default messages;

0 commit comments

Comments
 (0)