2025-02-27 23:20:17 -05:00
|
|
|
import { Link, useNavigate } from 'react-router-dom';
|
2024-03-29 20:08:34 -04:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2025-04-04 16:24:46 -04:00
|
|
|
import { TextField, Button, CircularProgress } from '@mui/material';
|
|
|
|
|
import LoginContainer from 'src/components/LoginContainer/LoginContainer';
|
2024-03-29 20:08:34 -04:00
|
|
|
import ApiService from '../../../services/ApiService';
|
2025-04-04 16:24:46 -04:00
|
|
|
import 'bootstrap/dist/css/bootstrap.min.css';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const Login: React.FC = () => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [connectionError, setConnectionError] = useState<string>('');
|
2025-04-04 16:24:46 -04:00
|
|
|
const [isConnecting, setIsConnecting] = useState<boolean>(false);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
2025-04-04 16:24:46 -04:00
|
|
|
// Cleanup if needed
|
2024-03-29 20:08:34 -04:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const login = async () => {
|
2025-04-04 16:24:46 -04:00
|
|
|
setIsConnecting(true);
|
|
|
|
|
try {
|
|
|
|
|
const result = await ApiService.login(email, password);
|
|
|
|
|
if (typeof result === "string") {
|
|
|
|
|
setConnectionError(result);
|
|
|
|
|
} else {
|
|
|
|
|
navigate("/teacher/Dashboard");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
setIsConnecting(false);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-27 23:43:58 -05:00
|
|
|
const handleReturnKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
if (e.key === 'Enter' && email && password) {
|
2025-02-27 23:20:17 -05:00
|
|
|
login();
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return (
|
2025-04-04 16:24:46 -04:00
|
|
|
<LoginContainer title='Login' error={connectionError}>
|
|
|
|
|
{/* Email Field */}
|
2024-03-29 20:08:34 -04:00
|
|
|
<TextField
|
|
|
|
|
label="Email"
|
|
|
|
|
variant="outlined"
|
2025-04-04 16:24:46 -04:00
|
|
|
className="mb-3 w-100"
|
2024-03-29 20:08:34 -04:00
|
|
|
value={email}
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
2025-01-10 13:20:40 -05:00
|
|
|
placeholder="Adresse courriel"
|
2025-04-04 16:24:46 -04:00
|
|
|
fullWidth
|
|
|
|
|
onKeyDown={handleReturnKey}
|
2024-03-29 20:08:34 -04:00
|
|
|
/>
|
|
|
|
|
|
2025-04-04 16:24:46 -04:00
|
|
|
{/* Password Field */}
|
2024-03-29 20:08:34 -04:00
|
|
|
<TextField
|
|
|
|
|
label="Mot de passe"
|
|
|
|
|
variant="outlined"
|
|
|
|
|
type="password"
|
2025-04-04 16:24:46 -04:00
|
|
|
className="mb-3 w-100"
|
2024-03-29 20:08:34 -04:00
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
2025-01-10 13:20:40 -05:00
|
|
|
placeholder="Mot de passe"
|
2025-04-04 16:24:46 -04:00
|
|
|
fullWidth
|
|
|
|
|
onKeyDown={handleReturnKey}
|
2024-03-29 20:08:34 -04:00
|
|
|
/>
|
|
|
|
|
|
2025-04-04 16:24:46 -04:00
|
|
|
{/* Login Button */}
|
|
|
|
|
<Button
|
2024-03-29 20:08:34 -04:00
|
|
|
variant="contained"
|
2025-04-04 16:24:46 -04:00
|
|
|
className={`w-100 mb-${connectionError ? '4' : '3'}`}
|
|
|
|
|
onClick={login}
|
|
|
|
|
disabled={!email || !password || isConnecting}
|
|
|
|
|
startIcon={isConnecting ? <CircularProgress size={20} /> : null}
|
2024-03-29 20:08:34 -04:00
|
|
|
>
|
|
|
|
|
Login
|
2025-04-04 16:24:46 -04:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
{/* Links Section */}
|
|
|
|
|
<div className="d-flex flex-column align-items-center pt-3">
|
|
|
|
|
<Link
|
|
|
|
|
to="/teacher/resetPassword"
|
|
|
|
|
className="mb-2 text-decoration-none text-primary"
|
|
|
|
|
>
|
2024-03-29 20:08:34 -04:00
|
|
|
Réinitialiser le mot de passe
|
|
|
|
|
</Link>
|
2025-04-04 16:24:46 -04:00
|
|
|
<Link
|
|
|
|
|
to="/teacher/register"
|
|
|
|
|
className="text-decoration-none text-primary"
|
|
|
|
|
>
|
2024-03-29 20:08:34 -04:00
|
|
|
Créer un compte
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
</LoginContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-04 16:24:46 -04:00
|
|
|
export default Login;
|