forked from openedx/frontend-app-admin-console
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.ts
More file actions
86 lines (81 loc) · 2.91 KB
/
hooks.ts
File metadata and controls
86 lines (81 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
useMutation, useQuery, useQueryClient, useSuspenseQuery,
} from '@tanstack/react-query';
import { appId } from '@src/constants';
import { LibraryMetadata, TeamMember } from '@src/types';
import {
assignTeamMembersRole,
AssignTeamMembersRoleRequest,
getLibrary, getPermissionsByRole, getTeamMembers, PermissionsByRole,
} from './api';
const authzQueryKeys = {
all: [appId, 'authz'] as const,
teamMembers: (object: string) => [...authzQueryKeys.all, 'teamMembers', object] as const,
permissionsByRole: (scope: string) => [...authzQueryKeys.all, 'permissionsByRole', scope] as const,
library: (libraryId: string) => [...authzQueryKeys.all, 'library', libraryId] as const,
};
/**
* React Query hook to fetch all team members for a specific object/scope.
* It retrieves the full list of members who have access to the given scope.
*
* @param object - The unique identifier of the object/scope
*
* @example
* ```tsx
* const { data: teamMembers, isLoading, isError } = useTeamMembers('lib:123');
* ```
*/
export const useTeamMembers = (object: string) => useQuery<TeamMember[], Error>({
queryKey: authzQueryKeys.teamMembers(object),
queryFn: () => getTeamMembers(object),
staleTime: 1000 * 60 * 30, // refetch after 30 minutes
});
/**
* React Query hook to fetch all the roles for the specific object/scope.
* It retrieves the full list of roles with the corresponding permissions.
*
* @param scope - The unique identifier of the object/scope
*
* @example
* ```tsx
* const { data: roles } = usePermissionsByRole('lib:123');
* ```
*/
export const usePermissionsByRole = (scope: string) => useSuspenseQuery<PermissionsByRole[], Error>({
queryKey: authzQueryKeys.permissionsByRole(scope),
queryFn: () => getPermissionsByRole(scope),
retry: false,
});
/**
* React Query hook to retrieve the information of the current library.
*
* @param libraryId - The unique ID of the library.
*
* @example
* const { data } = useLibrary('lib:123',);
*
*/
export const useLibrary = (libraryId: string) => useSuspenseQuery<LibraryMetadata, Error>({
queryKey: authzQueryKeys.library(libraryId),
queryFn: () => getLibrary(libraryId),
retry: false,
});
/**
* React Query hook to add new team members to a specific scope or manage the corresponding roles.
* It provides a mutation function to add users with specified roles to the team or assign new roles.
*
* @example
* const { mutate: assignTeamMembersRole } = useAssignTeamMembersRole();
* assignTeamMembersRole({ data: { libraryId: 'lib:123', users: ['jdoe'], role: 'editor' } });
*/
export const useAssignTeamMembersRole = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ data }: {
data: AssignTeamMembersRoleRequest
}) => assignTeamMembersRole(data),
onSettled: (_data, _error, { data: { scope } }) => {
queryClient.invalidateQueries({ queryKey: authzQueryKeys.teamMembers(scope) });
},
});
};