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();
            console.log('user', user);
            if (!user) navigate('/login');
        } catch (error) {
            console.log('error', error);
            navigate('/login');
        }
    };

    return <AppRouter></AppRouter>;
}

export default App;
