blob: 4cb66a54d2b4da302edf97390ecb7e0eee55bb34 [file] [log] [blame]
San3yuan4d0e8032025-04-04 17:21:40 +08001import React, { useEffect } from 'react';
2import { useApi } from '@/hooks/request';
3import request from '@/utils/request';
San3yuan2534d422025-04-08 21:43:18 +08004import { userLogin } from '@/api/user';
San3yuan4d0e8032025-04-04 17:21:40 +08005import { useAppDispatch } from '@/hooks/store';
San3yuan2534d422025-04-08 21:43:18 +08006import { RootState } from '@/store';
7import style from './login.module.css';
8import { useSelector } from 'react-redux';
9import { useNavigate } from 'react-router';
San3yuan4d0e8032025-04-04 17:21:40 +080010
11const Login: React.FC = () => {
San3yuan4d0e8032025-04-04 17:21:40 +080012 const dispatch = useAppDispatch();
13 const { data, loading, error, refresh } = useApi(() => request.post(userLogin), false);
14
San3yuan2534d422025-04-08 21:43:18 +080015 const nav = useNavigate();
San3yuan4d0e8032025-04-04 17:21:40 +080016 const handleLogin = async () => {
San3yuan4d0e8032025-04-04 17:21:40 +080017 const res = await refresh();
18 console.log(res);
San3yuan4d0e8032025-04-04 17:21:40 +080019 dispatch({ type: "user/login", payload: res });
San3yuan2534d422025-04-08 21:43:18 +080020 nav('/');
21
San3yuan4d0e8032025-04-04 17:21:40 +080022 };
San3yuan2534d422025-04-08 21:43:18 +080023 const userName = useSelector((state: RootState) => state.user.userName);
24
San3yuan4d0e8032025-04-04 17:21:40 +080025 return (
San3yuan2534d422025-04-08 21:43:18 +080026
27 <div className={style.form}>
28 <img alt = "logo"></img>
29 <input type="email" className={style.email} placeholder="Enter your email" />
30 <input type="password" className={style.password} placeholder="Enter your password" />
31 <button className={style.submit} onClick={handleLogin}>Login</button>
San3yuan4d0e8032025-04-04 17:21:40 +080032 <button>Register</button>
33 </div>
34 );
San3yuan2534d422025-04-08 21:43:18 +080035};
San3yuan4d0e8032025-04-04 17:21:40 +080036
37export default Login;