import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { setPageTitle } from '../../store/themeConfigSlice';
import { LoginForm } from '../../features/auth/components/LoginForm';
import { useUser } from '../../lib/auth';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { paths } from '../../config/paths';

const Login = () => {
    const dispatch = useDispatch();
    useEffect(() => {
        dispatch(setPageTitle('Login'));
    });

    const user = useUser();
    const [searchParams] = useSearchParams();
    const redirectTo = searchParams.get('redirectTo');

    const navigate = useNavigate();

    useEffect(() => {
        if (user.data) {
            navigate(redirectTo ? redirectTo : paths.dashboard.getHref(), {
                replace: true,
            });
        }
    }, [user.data, navigate, redirectTo]);

    return <LoginForm />;
};

export default Login;
