83 lines
3.3 KiB
TypeScript
83 lines
3.3 KiB
TypeScript
import { useQuery, useMutation, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
import { useInject } from '@/lib/di/hooks/useInject';
|
|
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
|
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
|
import { ApiError } from '@/lib/api/base/ApiError';
|
|
import type { LeagueAdminRosterJoinRequestViewModel } from '@/lib/view-models/LeagueAdminRosterJoinRequestViewModel';
|
|
import type { LeagueAdminRosterMemberViewModel } from '@/lib/view-models/LeagueAdminRosterMemberViewModel';
|
|
import type { MembershipRole } from '@/lib/types/MembershipRole';
|
|
|
|
export function useLeagueRosterJoinRequests(
|
|
leagueId: string,
|
|
options?: Omit<UseQueryOptions<LeagueAdminRosterJoinRequestViewModel[], ApiError>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
const queryResult = useQuery({
|
|
queryKey: ['leagueRosterJoinRequests', leagueId],
|
|
queryFn: () => leagueService.getAdminRosterJoinRequests(leagueId),
|
|
...options,
|
|
});
|
|
|
|
return enhanceQueryResult(queryResult);
|
|
}
|
|
|
|
export function useLeagueRosterMembers(
|
|
leagueId: string,
|
|
options?: Omit<UseQueryOptions<LeagueAdminRosterMemberViewModel[], ApiError>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
const queryResult = useQuery({
|
|
queryKey: ['leagueRosterMembers', leagueId],
|
|
queryFn: () => leagueService.getAdminRosterMembers(leagueId),
|
|
...options,
|
|
});
|
|
|
|
return enhanceQueryResult(queryResult);
|
|
}
|
|
|
|
export function useApproveJoinRequest(
|
|
options?: Omit<UseMutationOptions<{ success: boolean }, ApiError, { leagueId: string; joinRequestId: string }>, 'mutationFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
return useMutation<{ success: boolean }, ApiError, { leagueId: string; joinRequestId: string }>({
|
|
mutationFn: ({ leagueId, joinRequestId }) => leagueService.approveJoinRequest(leagueId, joinRequestId),
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useRejectJoinRequest(
|
|
options?: Omit<UseMutationOptions<{ success: boolean }, ApiError, { leagueId: string; joinRequestId: string }>, 'mutationFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
return useMutation<{ success: boolean }, ApiError, { leagueId: string; joinRequestId: string }>({
|
|
mutationFn: ({ leagueId, joinRequestId }) => leagueService.rejectJoinRequest(leagueId, joinRequestId),
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useUpdateMemberRole(
|
|
options?: Omit<UseMutationOptions<{ success: boolean }, ApiError, { leagueId: string; driverId: string; role: MembershipRole }>, 'mutationFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
return useMutation<{ success: boolean }, ApiError, { leagueId: string; driverId: string; role: MembershipRole }>({
|
|
mutationFn: ({ leagueId, driverId, role }) => leagueService.updateMemberRole(leagueId, driverId, role),
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useRemoveMember(
|
|
options?: Omit<UseMutationOptions<{ success: boolean }, ApiError, { leagueId: string; driverId: string }>, 'mutationFn'>
|
|
) {
|
|
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
|
|
|
return useMutation<{ success: boolean }, ApiError, { leagueId: string; driverId: string }>({
|
|
mutationFn: ({ leagueId, driverId }) => leagueService.removeMember(leagueId, driverId),
|
|
...options,
|
|
});
|
|
}
|