import React, { useState } from "react"; import { Mail, Lock, AlertCircle, CheckCircle, Info, X } from "lucide-react"; import { useNavigate, Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; import axios from "axios"; import { useAuth } from "../AuthContext"; import { API_BASE_URL } from "../config"; function Login() { const { t } = useTranslation(); const [formData, setFormData] = useState({ email: "", password: "", }); const [alert, setAlert] = useState({ show: false, type: "", // 'success', 'error', 'info', 'warning' message: "", }); const { login } = useAuth(); const navigate = useNavigate(); const handleChange = (e) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); if (alert.show) setAlert({ ...alert, show: false }); }; const showAlert = (type, message) => { setAlert({ show: true, type, message, }); // Auto-hide success and info alerts after 5 seconds if (type === 'success' || type === 'info') { setTimeout(() => { setAlert(prev => ({ ...prev, show: false })); }, 5000); } }; const handleSubmit = async (e) => { e.preventDefault(); setAlert({ show: false, type: "", message: "" }); try { // Afficher un message de chargement showAlert("info", t('auth.login.loading')); const response = await axios.post(`${API_BASE_URL}/login`, formData, { headers: { "Content-Type": "application/json", }, }); const data = response.data; if (data.token) { showAlert("success", t('auth.login.success')); login(data.token); // Court délai pour montrer le message de succès avant la redirection setTimeout(() => { navigate("/"); }, 1000); } else { showAlert("error", t('auth.login.missingToken')); } } catch (error) { console.error("Erreur lors de la connexion", error); if (error.response) { if (error.response.status === 401) { showAlert("error", t('auth.login.incorrectAuth')); } else if (error.response.status === 422) { showAlert("error", t('auth.login.invalidData')); } else if (error.response.status >= 500) { showAlert("error", t('auth.login.serverError')); } else { showAlert("error", error.response.data.message || t('auth.login.genericError')); } } else if (error.request) { showAlert("error", t('auth.login.networkError')); } else { showAlert("error", t('auth.login.genericError')); } } }; // Configuration des alertes selon le type const alertConfig = { success: { bgColor: "bg-green-50", borderColor: "border-green-200", textColor: "text-green-700", icon: }, error: { bgColor: "bg-red-50", borderColor: "border-red-200", textColor: "text-red-700", icon: }, info: { bgColor: "bg-blue-50", borderColor: "border-blue-200", textColor: "text-blue-700", icon: }, warning: { bgColor: "bg-yellow-50", borderColor: "border-yellow-200", textColor: "text-yellow-700", icon: } }; return (

{t('auth.login.title')}

{/* Système d'alertes */} {alert.show && (
{alertConfig[alert.type].icon} {alert.message}
)}
{/* Email */}
{/* Mot de passe */}
{/* Bouton de connexion */}
{/* Lien vers la page d'inscription */}

{t('auth.login.noAccount')} {t('auth.login.signupLink')}

); } export default Login;