···11+import { ApiClient } from '@/api-client/ApiClient';
22+import { getAccessToken } from '@/services/auth';
33+import { useMutation, useQueryClient } from '@tanstack/react-query';
44+55+export default function useCreateCollection() {
66+ const apiClient = new ApiClient(
77+ process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3000',
88+ () => getAccessToken(),
99+ );
1010+1111+ const queryClient = useQueryClient();
1212+1313+ const mutation = useMutation({
1414+ mutationFn: (newCollection: { name: string; description: string }) => {
1515+ return apiClient.createCollection(newCollection);
1616+ },
1717+1818+ // Do things that are absolutely necessary and logic related (like query invalidation) in the useMutation callbacks
1919+ // 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
2020+ // https://tkdodo.eu/blog/mastering-mutations-in-react-query#some-callbacks-might-not-fire
2121+ onSuccess: () => {
2222+ queryClient.invalidateQueries({ queryKey: ['collections'] });
2323+ },
2424+ });
2525+2626+ return mutation;
2727+}