|
1 | | -import { useQuery, useSuspenseQuery } from '@tanstack/react-query'; |
| 1 | +import { |
| 2 | + useMutation, useQuery, useQueryClient, useSuspenseQuery, |
| 3 | +} from '@tanstack/react-query'; |
2 | 4 | import { appId } from '@src/constants'; |
3 | | -import { LibraryMetadata, TeamMember } from '@src/types'; |
4 | | -import { getLibrary, getTeamMembers } from './api'; |
| 5 | +import { LibraryMetadata, TeamMember, TeamRole } from '@src/types'; |
| 6 | +import { |
| 7 | + addTeamMembers, AddTeamMembersRequest, getLibrary, getTeamMembers, getTeamRoles, |
| 8 | +} from './api'; |
5 | 9 |
|
6 | 10 | const authzQueryKeys = { |
7 | 11 | all: [appId, 'authz'] as const, |
8 | 12 | teamMembers: (object: string) => [...authzQueryKeys.all, 'teamMembers', object] as const, |
| 13 | + teamRoles: (libraryId: string) => [...authzQueryKeys.all, 'teamRoles', libraryId] as const, |
9 | 14 | library: (libraryId: string) => [...authzQueryKeys.all, 'library', libraryId] as const, |
10 | 15 | }; |
11 | 16 |
|
@@ -40,3 +45,42 @@ export const useLibrary = (libraryId: string) => useSuspenseQuery<LibraryMetadat |
40 | 45 | queryFn: () => getLibrary(libraryId), |
41 | 46 | retry: false, |
42 | 47 | }); |
| 48 | + |
| 49 | +/** |
| 50 | + * React Query hook to add new team members to a specific library. |
| 51 | + * It provides a mutation function to add users with specified roles to the library's team. |
| 52 | + * |
| 53 | + * @example |
| 54 | + * ```tsx |
| 55 | + * const { mutate: addTeamMember, isPending } = useAddTeamMember(); |
| 56 | + * addTeamMember({ data: { libraryId: 'lib:123', users: ['jdoe'], role: 'editor' } }); |
| 57 | + * ``` |
| 58 | + */ |
| 59 | +export const useAddTeamMember = () => { |
| 60 | + const queryClient = useQueryClient(); |
| 61 | + return useMutation({ |
| 62 | + mutationFn: async ({ data }: { |
| 63 | + data: AddTeamMembersRequest |
| 64 | + }) => addTeamMembers(data), |
| 65 | + onSettled: (_data, _error, { data: { scope } }) => { |
| 66 | + queryClient.invalidateQueries({ queryKey: authzQueryKeys.teamMembers(scope) }); |
| 67 | + }, |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * React Query hook to fetch all roles available for a specific library. |
| 73 | + * It retrieves the list of roles that can be assigned to team members within the given library. |
| 74 | + * |
| 75 | + * @param libraryId - The unique identifier of the library |
| 76 | + * |
| 77 | + * @example |
| 78 | + * ```tsx |
| 79 | + * const { data: teamRoles, isLoading, isError } = useTeamRoles('lib:123'); |
| 80 | + * ``` |
| 81 | + */ |
| 82 | +export const useTeamRoles = (libraryId: string) => useSuspenseQuery<TeamRole[], Error>({ |
| 83 | + queryKey: authzQueryKeys.teamRoles(libraryId), |
| 84 | + queryFn: () => getTeamRoles(libraryId), |
| 85 | + staleTime: 1000 * 60 * 60 * 24, // refetch after 24 hours |
| 86 | +}); |
0 commit comments