import { useDispatch, useSelector } from 'react-redux';
import AppRouter from './router/router';
import { checkAuthThunk } from './store/auth/auth.thunk';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { login } from './store/auth/auth.slice';

function App() {
    const navigate = useNavigate();
    const dispatch = useDispatch();

    const { user } = useSelector((state) => state.auth);

    useEffect(() => {
        handleCheckAuth();
    }, [dispatch]);

    const handleCheckAuth = async () => {
        console.log('app handleCheckAuth');
        try {
            await dispatch(checkAuthThunk()).unwrap();
            if (user) navigate('/');
        } catch (error) {}
    };

    return <AppRouter></AppRouter>;
}

export default App;
