import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { AxiosError } from "axios"; import { get_my_user_tests, get_user_test, post_start_random_test, post_start_test } from "./_client"; export const useMyTests = () => { return useQuery({ queryKey: ['user-tests'], queryFn: async () => { const response = await get_my_user_tests(); return response.data; } }) } export const useUserTest = (id: number) => { return useQuery({ queryKey: ['user-test', id], queryFn: async () => { const response = await get_user_test(id); return response.data; } }) } interface RandomUserTestAttrs { min: number, max: number, category_id: number } export const useRandomUserTestMutation = () => { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ min, max, category_id }: RandomUserTestAttrs) => { const response = await post_start_random_test(min, max, category_id); queryClient.invalidateQueries({ queryKey: ["user-tests"] }); return response.data; } }) } interface StartTestAttrs { test_id: number } export const useStartTestMutation = () => { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ test_id }: StartTestAttrs) => { const response = await post_start_test(test_id); queryClient.invalidateQueries({ queryKey: ["user-tests"] }); return response.data; } }) }