This repository has no description
1.0 kB
23 lines
1import { useMutation, useQueryClient } from '@tanstack/react-query';
2import { createCollection } from '../dal';
3import { collectionKeys } from '../collectionKeys';
4
5export default function useCreateCollection() {
6 const queryClient = useQueryClient();
7
8 const mutation = useMutation({
9 mutationFn: (newCollection: { name: string; description: string }) => {
10 return createCollection(newCollection);
11 },
12
13 // Do things that are absolutely necessary and logic related (like query invalidation) in the useMutation callbacks
14 // Do UI related things like redirects or showing toast notifications in mutate callbacks. If the user navigated away from the current screen before the mutation finished, those will purposefully not fire
15 // https://tkdodo.eu/blog/mastering-mutations-in-react-query#some-callbacks-might-not-fire
16 onSuccess: () => {
17 queryClient.invalidateQueries({ queryKey: collectionKeys.infinite() });
18 queryClient.refetchQueries({ queryKey: collectionKeys.mine() });
19 },
20 });
21
22 return mutation;
23}