import { useLocation, useNavigate } from 'react-router-dom';

interface IUseRedirectToLoginPage {
	toLoginPage: () => void
}

const loginPagePath = '/login';

export default function useRedirectToLoginPage(): IUseRedirectToLoginPage {
	const navigate = useNavigate();
	const location = useLocation();

	const toLoginPage = () => {
		if (location.pathname != loginPagePath){
			// console.log('to login');
			navigate(loginPagePath, { state: { from: location }, replace: true});
		}
		// else console.log('not need to redirect');
	};
	
	return { toLoginPage };
}