import { useEffect, useState } from 'react';

export const useData = () => {
	const [isLoading, setIsLoading] = useState(true);
	const [userData, setUserData] = useState(null);

	useEffect(() => {
		(async () => {
			const token = localStorage.getItem('token');

			const request = await fetch('/api/user', {
				method: 'GET',
				headers: {
					Authorization: token,
				},
			});

			const response = await request.json();

			const formattedResponse = {
				...response,
				createdat: new Date(response.createdat).toLocaleDateString(
					'fr-FR'
				),
			};

			setIsLoading(false);
			setUserData(formattedResponse);
		})();
	}, []);

	return {
		isLoading,
		userData,
	};
};
