All files / src/hooks/mutations useBlogTagMutations.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 8/8
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                4x   4x   3x 2x     2x         1x           3x   3x   2x 1x     1x         1x        
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { notify } from "../../lib/notify";
import { proApi } from "../../lib/api";
import { adminApi } from "../../lib/api/admin";
import { queryKeys } from "../../lib/query-keys";
import type { BlogTag } from "../../lib/api/blogs";
 
export function useCreateBlogTag(proId: string) {
	const queryClient = useQueryClient();
 
	return useMutation({
		mutationFn: async (name: string) => {
			const response = await proApi.createBlogTag(proId, { name });
			return response.data as BlogTag;
		},
		onSuccess: () => {
			queryClient.invalidateQueries({
				queryKey: queryKeys.blogs.tags(proId),
			});
		},
		onError: () => {
			notify.error("Failed to create tag. Please try again.");
		},
	});
}
 
export function useAdminCreateBlogTag() {
	const queryClient = useQueryClient();
 
	return useMutation({
		mutationFn: async (name: string) => {
			const response = await adminApi.createBlogTag({ name });
			return response.data as BlogTag;
		},
		onSuccess: () => {
			queryClient.invalidateQueries({
				queryKey: queryKeys.admin.blogs.tags(),
			});
		},
		onError: () => {
			notify.error("Failed to create tag. Please try again.");
		},
	});
}