|
| 1 | +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' |
| 2 | + |
| 3 | +import { |
| 4 | + teamInvitationsService, |
| 5 | + type SendInvitation, |
| 6 | + type Team, |
| 7 | + type TeamInvitation, |
| 8 | +} from '@/shared/lib/api/team-invitations-service' |
| 9 | +import type { ApiError } from '@/shared/lib/api/types' |
| 10 | + |
| 11 | +import { teamsKeys } from './use-teams' |
| 12 | + |
| 13 | +export const teamInvitationsKeys = { |
| 14 | + all: ['teams', 'invitations'] as const, |
| 15 | + teamLists: () => [...teamInvitationsKeys.all, 'team-list'] as const, |
| 16 | + teamList: (teamId: string) => [...teamInvitationsKeys.teamLists(), teamId] as const, |
| 17 | + send: (teamId: string) => [...teamInvitationsKeys.all, 'send', teamId] as const, |
| 18 | + revoke: (teamId: string) => [...teamInvitationsKeys.all, 'revoke', teamId] as const, |
| 19 | + myLists: () => [...teamInvitationsKeys.all, 'my-list'] as const, |
| 20 | + myList: () => [...teamInvitationsKeys.myLists()] as const, |
| 21 | + accept: () => [...teamInvitationsKeys.all, 'accept'] as const, |
| 22 | + decline: () => [...teamInvitationsKeys.all, 'decline'] as const, |
| 23 | +} as const |
| 24 | + |
| 25 | +export const useTeamInvitations = (teamId: string) => { |
| 26 | + return useQuery({ |
| 27 | + queryKey: teamInvitationsKeys.teamList(teamId), |
| 28 | + queryFn: () => teamInvitationsService.getTeamInvitations(teamId), |
| 29 | + enabled: Boolean(teamId), |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +export const useSendTeamInvitation = (teamId: string) => { |
| 34 | + const queryClient = useQueryClient() |
| 35 | + |
| 36 | + return useMutation<TeamInvitation, ApiError, SendInvitation>({ |
| 37 | + mutationKey: teamInvitationsKeys.send(teamId || 'mutation'), |
| 38 | + mutationFn: (data) => teamInvitationsService.sendInvitation(teamId, data), |
| 39 | + onSuccess: () => |
| 40 | + queryClient.invalidateQueries({ |
| 41 | + queryKey: teamInvitationsKeys.teamList(teamId), |
| 42 | + }), |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +export const useRevokeTeamInvitation = (teamId: string) => { |
| 47 | + const queryClient = useQueryClient() |
| 48 | + |
| 49 | + return useMutation<TeamInvitation, ApiError, string>({ |
| 50 | + mutationKey: teamInvitationsKeys.revoke(teamId || 'mutation'), |
| 51 | + mutationFn: (invitationId) => |
| 52 | + teamInvitationsService.revokeInvitation(teamId, invitationId), |
| 53 | + onSuccess: () => |
| 54 | + queryClient.invalidateQueries({ |
| 55 | + queryKey: teamInvitationsKeys.teamList(teamId), |
| 56 | + }), |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +export const useMyInvitations = () => { |
| 61 | + return useQuery({ |
| 62 | + queryKey: teamInvitationsKeys.myList(), |
| 63 | + queryFn: teamInvitationsService.getMyInvitations, |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +export const useAcceptInvitation = () => { |
| 68 | + const queryClient = useQueryClient() |
| 69 | + |
| 70 | + return useMutation<Team, ApiError, string>({ |
| 71 | + mutationKey: teamInvitationsKeys.accept(), |
| 72 | + mutationFn: teamInvitationsService.acceptInvitation, |
| 73 | + onSuccess: async (team) => { |
| 74 | + queryClient.setQueryData(teamsKeys.detail(team.id), team) |
| 75 | + |
| 76 | + await Promise.all([ |
| 77 | + queryClient.invalidateQueries({ queryKey: teamsKeys.lists() }), |
| 78 | + queryClient.invalidateQueries({ queryKey: teamInvitationsKeys.myList() }), |
| 79 | + ]) |
| 80 | + }, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +export const useDeclineInvitation = () => { |
| 85 | + const queryClient = useQueryClient() |
| 86 | + |
| 87 | + return useMutation<TeamInvitation, ApiError, string>({ |
| 88 | + mutationKey: teamInvitationsKeys.decline(), |
| 89 | + mutationFn: teamInvitationsService.declineInvitation, |
| 90 | + onSuccess: () => |
| 91 | + queryClient.invalidateQueries({ |
| 92 | + queryKey: teamInvitationsKeys.myList(), |
| 93 | + }), |
| 94 | + }) |
| 95 | +} |
0 commit comments