This repository has no description
1.7 kB
52 lines
1import { useMutation, useQueryClient } from '@tanstack/react-query';
2import { removeCardFromCollection } from '../dal';
3import { collectionKeys } from '@/features/collections/lib/collectionKeys';
4import { sembleKeys } from '@/features/semble/lib/sembleKeys';
5
6export default function useRemoveCardFromCollections() {
7 const queryClient = useQueryClient();
8
9 const mutation = useMutation({
10 mutationFn: ({
11 cardId,
12 collectionIds,
13 }: {
14 cardId: string;
15 collectionIds: string[];
16 }) => {
17 return removeCardFromCollection({ cardId, collectionIds });
18 },
19
20 onSuccess: (_data, variables) => {
21 queryClient.invalidateQueries({ queryKey: collectionKeys.infinite() });
22 queryClient.invalidateQueries({ queryKey: collectionKeys.mine() });
23 queryClient.invalidateQueries({ queryKey: collectionKeys.all() });
24 queryClient.invalidateQueries({ queryKey: sembleKeys.all() });
25 // Invalidate all URL metadata queries with stats to update tab counts
26 queryClient.invalidateQueries({
27 predicate: (query): boolean => {
28 const key = query.queryKey as unknown[];
29 return !!(
30 key[0] === 'cards' &&
31 key[1] === 'metadata' &&
32 key[3] &&
33 typeof key[3] === 'object' &&
34 'includeStats' in key[3] &&
35 key[3].includeStats === true
36 );
37 },
38 });
39
40 variables.collectionIds.forEach((id) => {
41 queryClient.invalidateQueries({
42 queryKey: collectionKeys.collection(id),
43 });
44 queryClient.invalidateQueries({
45 queryKey: collectionKeys.infinite(id),
46 });
47 });
48 },
49 });
50
51 return mutation;
52}