2025-01-21 20:33:05 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-01-29 21:30:41 +03:00
|
|
|
import { libraryApi } from '@/backend/library/api';
|
|
|
|
|
2025-01-21 20:33:05 +03:00
|
|
|
import { authApi } from './api';
|
|
|
|
|
|
|
|
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] }),
|
|
|
|
onSuccess: () => client.removeQueries({ queryKey: [libraryApi.baseKey] })
|
2025-01-21 20:33:05 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-01-23 19:41:31 +03:00
|
|
|
login: (
|
|
|
|
username: string, //
|
|
|
|
password: string,
|
|
|
|
onSuccess?: () => void
|
|
|
|
) => mutation.mutate({ username, password }, { onSuccess }),
|
2025-01-21 20:33:05 +03:00
|
|
|
isPending: mutation.isPending,
|
|
|
|
error: mutation.error,
|
|
|
|
reset: mutation.reset
|
|
|
|
};
|
|
|
|
};
|