Skip to content

Commit eaef4bd

Browse files
UIQM-735: Return also sub permissions in useUserTenantPermissions hook.
1 parent a4c1649 commit eaef4bd

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change history for ui-quick-marc
22

3+
## [8.0.2] (IN PROGRESS)
4+
5+
* [UIQM-735](https://issues.folio.org/browse/UIQM-735) Return also sub permissions in `useUserTenantPermissions` hook.
6+
37
## [8.0.1] (https://github.com/folio-org/ui-quick-marc/tree/v8.0.1) (2024-04-18)
48

59
* [UIQM-641](https://issues.folio.org/browse/UIQM-641) Call `cleanBytesFields` function with correct arguments to fix 008 field.

src/MarcRoute/MarcRoute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const MarcRoute = ({
4444
});
4545

4646
const checkCentralTenantPerm = useCallback((perm) => {
47-
return centralTenantPermissions.some(({ permissionName }) => permissionName === perm);
47+
return centralTenantPermissions.has(perm);
4848
}, [centralTenantPermissions]);
4949

5050
if (isCentralTenantPermissionsLoading) {

src/queries/useUserTenantPermissions/useUserTenantPermissions.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useMemo } from 'react';
12
import { useQuery } from 'react-query';
23

34
import {
@@ -48,10 +49,26 @@ const useUserTenantPermissions = (
4849
},
4950
);
5051

52+
const centralTenantPermissions = data.permissionNames || INITIAL_DATA;
53+
54+
const flattenCentralTenantPermissions = useMemo(() => {
55+
const permSet = new Set();
56+
57+
centralTenantPermissions.forEach(perm => {
58+
permSet.add(perm.permissionName);
59+
60+
perm.subPermissions?.forEach(subPermission => {
61+
permSet.add(subPermission);
62+
});
63+
});
64+
65+
return permSet;
66+
}, [centralTenantPermissions]);
67+
5168
return ({
5269
isFetching,
5370
isLoading,
54-
userPermissions: data.permissionNames || INITIAL_DATA,
71+
userPermissions: flattenCentralTenantPermissions,
5572
totalRecords: data.totalRecords,
5673
});
5774
};

src/queries/useUserTenantPermissions/useUserTenantPermissions.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ const wrapper = ({ children }) => (
1818
);
1919

2020
const response = {
21-
permissionNames: [],
21+
permissionNames: [
22+
{
23+
permissionName: 'permissionName1',
24+
subPermissions: ['subPermissions1', 'subPermissions2'],
25+
},
26+
{
27+
permissionName: 'permissionName2',
28+
subPermissions: ['subPermissions1', 'subPermissions3'],
29+
},
30+
],
2231
totalRecords: 0,
2332
};
2433

@@ -54,4 +63,22 @@ describe('useUserTenantPermissions', () => {
5463
expect(setHeaderMock).toHaveBeenCalledWith('X-Okapi-Tenant', options.tenantId);
5564
expect(getMock).toHaveBeenCalledWith(`perms/users/${options.userId}/permissions`, expect.objectContaining({}));
5665
});
66+
67+
it('should consider sub permissions without duplicates', async () => {
68+
const options = {
69+
userId: 'userId',
70+
tenantId: 'tenantId',
71+
};
72+
const { result } = renderHook(() => useUserTenantPermissions(options), { wrapper });
73+
74+
await act(async () => !result.current.isLoading);
75+
76+
expect([...result.current.userPermissions]).toEqual([
77+
'permissionName1',
78+
'subPermissions1',
79+
'subPermissions2',
80+
'permissionName2',
81+
'subPermissions3',
82+
]);
83+
});
5784
});

0 commit comments

Comments
 (0)