2025-01-21 20:33:05 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
2025-01-31 21:04:21 +03:00
|
|
|
import { AxiosError } from 'axios';
|
2025-01-29 21:30:41 +03:00
|
|
|
|
2025-01-30 21:00:59 +03:00
|
|
|
import { authApi, IUserLoginDTO } from './api';
|
2025-01-21 20:33:05 +03:00
|
|
|
|
|
|
|
export const useLogin = () => {
|
2025-01-23 19:41:31 +03:00
|
|
|
const client = useQueryClient();
|
2025-01-21 20:33:05 +03:00
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: ['login'],
|
|
|
|
mutationFn: authApi.login,
|
2025-01-29 21:30:41 +03:00
|
|
|
onSettled: () => client.invalidateQueries({ queryKey: [authApi.baseKey] }),
|
2025-01-31 21:04:21 +03:00
|
|
|
onSuccess: () => client.resetQueries()
|
2025-01-21 20:33:05 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-01-31 21:04:21 +03:00
|
|
|
login: (data: IUserLoginDTO, onSuccess?: () => void, onError?: (error: AxiosError) => void) =>
|
|
|
|
mutation.mutate(data, { onSuccess, onError }),
|
2025-01-21 20:33:05 +03:00
|
|
|
isPending: mutation.isPending,
|
|
|
|
error: mutation.error,
|
|
|
|
reset: mutation.reset
|
|
|
|
};
|
|
|
|
};
|