A social knowledge tool for researchers built on ATProto
at development 37 lines 1.1 kB view raw
1import { ApiClient } from '@/api-client/ApiClient'; 2import { createClientTokenManager } from '@/services/auth'; 3import { useMutation, useQueryClient } from '@tanstack/react-query'; 4 5export default function useAddCardToCollection() { 6 const apiClient = new ApiClient( 7 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3000', 8 createClientTokenManager(), 9 ); 10 11 const queryClient = useQueryClient(); 12 13 const mutation = useMutation({ 14 mutationFn: ({ 15 cardId, 16 collectionIds, 17 }: { 18 cardId: string; 19 collectionIds: string[]; 20 }) => { 21 return apiClient.addCardToCollection({ cardId, collectionIds }); 22 }, 23 24 onSuccess: (_data, variables) => { 25 queryClient.invalidateQueries({ queryKey: ['card', variables.cardId] }); 26 queryClient.invalidateQueries({ queryKey: ['my cards'] }); 27 queryClient.invalidateQueries({ queryKey: ['home'] }); 28 queryClient.invalidateQueries({ queryKey: ['collections'] }); 29 30 variables.collectionIds.forEach((id) => { 31 queryClient.invalidateQueries({ queryKey: ['collection', id] }); 32 }); 33 }, 34 }); 35 36 return mutation; 37}