Login.js 5.44 KB
Newer Older
한규민's avatar
한규민 committed
1
2
import { useState } from "react";
import { Redirect } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
import { useAuth } from "../../context/auth_context.js";
한규민's avatar
한규민 committed
4
import catchErrors from "../../utils/catchErrors";
Kim, Subin's avatar
Kim, Subin committed
5
import styles from "./login.module.scss";
한규민's avatar
login    
한규민 committed
6

한규민's avatar
한규민 committed
7
const Login = () => {
Kim, Subin's avatar
Kim, Subin committed
8
9
    const { login, loading } = useAuth();
    const [state, setState] = useState(true);
한규민's avatar
한규민 committed
10
    const [user, setUser] = useState({
한규민's avatar
push    
한규민 committed
11
12
        id: '',
        password: ''
13
    });
한규민's avatar
한규민 committed
14
    const [error, setError] = useState("");
한규민's avatar
한규민 committed
15
    const [success, setSuccess] = useState(false);
한규민's avatar
한규민 committed
16
    const [guest, setGuset] = useState({
한규민's avatar
push    
한규민 committed
17
18
19
20
        guestName: '',
        gusetBirthday: '',
        gusetMbnum: '',
        guestPassword: ''
21
22
    })

한규민's avatar
push    
한규민 committed
23
    const handleLoginOnChange = (e) => {
한규민's avatar
한규민 committed
24
25
        setUser({
            ...user,
한규민's avatar
push    
한규민 committed
26
            [e.target.name]: e.target.value
27
28
29
        })
    };

한규민's avatar
push    
한규민 committed
30
31
32
33
    const handleGuestOnChange = (e) => {
        setGuset({
            ...guest,
            [e.target.name]: e.target.value
34
35
        })
    }
한규민's avatar
한규민 committed
36

한규민's avatar
context    
한규민 committed
37
    const requestServer = async (data) => {
Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
        if (data === user) {
            const success = await login(data);
            if (success) {
                setSuccess(true);
                alert('로그인이 완료되었습니다.')
            }
한규민's avatar
한규민 committed
44
        }
한규민's avatar
한규민 committed
45
46
    }

한규민's avatar
push    
한규민 committed
47
    const handleOnSummit = async (e) => {
한규민's avatar
한규민 committed
48
        e.preventDefault();
한규민's avatar
push    
한규민 committed
49
        try {
한규민's avatar
한규민 committed
50
            setError("");
한규민's avatar
push    
한규민 committed
51
            if (e.target.name === "login") {
한규민's avatar
한규민 committed
52
                requestServer(user);
한규민's avatar
push    
한규민 committed
53
54
55
56
57
58
            }
            else {
                requestServer(guest);
                alert('로그인이 완료되었습니다.')
            }
        } catch (error) {
한규민's avatar
한규민 committed
59
60
61
62
63
64
65
            catchErrors(error, setError);
        }
    }

    if (success) {
        return <Redirect to="/" />;
    }
한규민's avatar
push    
한규민 committed
66

한규민's avatar
한규민 committed
67
    return (
한규민's avatar
한규민 committed
68
        <div className={`d-flex flex-column col-md-5 col-10`}>
한규민's avatar
한규민 committed
69
            <span className={styles.title}>로그인</span>
한규민's avatar
한규민 committed
70
            <ul className="nav nav-fill nav-tabs w-100" id="loginTab" role="tablist">
한규민's avatar
login    
한규민 committed
71
                <li className="nav-item fs-6" role="presentation">
한규민's avatar
push    
한규민 committed
72
73
74
                    <button className={`nav-link active px-2 ${styles.fontSize}`} style={{ color: state ? "black" : "#FEDC00", backgroundColor: state ? "#FEDC00" : "black" }}
                        id="login-tab" data-bs-toggle="tab" data-bs-target="#login" type="button" role="tab" aria-controls="login" aria-selected="true"
                        onClick={() => setState(true)}>로그인</button>
한규민's avatar
한규민 committed
75
76
                </li>
                <li className="nav-item" role="presentation">
한규민's avatar
login    
한규민 committed
77
                    <button className={`nav-link px-2 ${styles.fontSize}`}
한규민's avatar
push    
한규민 committed
78
79
                        id="guest-tab" data-bs-toggle="tab" data-bs-target="#guest" type="button" role="tab" aria-controls="guest" aria-selected="false"
                        onClick={() => setState(false)} style={{ color: state ? "#FEDC00" : "black", backgroundColor: state ? "black" : "#FEDC00" }}>비회원 예매 확인</button>
한규민's avatar
한규민 committed
80
81
82
                </li>
            </ul>
            <div className="tab-content w-100" id="myTabContent">
한규민's avatar
한규민 committed
83
                <div className="tab-pane fade show active" id="login" role="tabpanel" aria-labelledby="login-tab">
한규민's avatar
push    
한규민 committed
84
85
                    <form className="d-flex flex-column" name="login" onSubmit={handleOnSummit}>
                        <input className={styles.input} type="text" name="id" placeholder="ID" onChange={handleLoginOnChange} maxLength="10" required />
한규민's avatar
context    
한규민 committed
86
                        <input className={styles.input} type="password" name="password" placeholder="Password" onChange={handleLoginOnChange} maxLength="11" required />
한규민's avatar
push    
한규민 committed
87
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="Login" disabled={loading} />
한규민's avatar
한규민 committed
88
                        <span><a href="./signup" className={styles.intoSignupPage}>회원이 아니십니까?</a></span>
한규민's avatar
한규민 committed
89
                    </form>
한규민's avatar
한규민 committed
90
91
                </div>
                <div className="tab-pane fade" id="guest" role="tabpanel" aria-labelledby="guest-tab">
한규민's avatar
context    
한규민 committed
92
                    <form className="d-flex flex-column" onSubmit={handleOnSummit}>
한규민's avatar
한규민 committed
93
                        <input className={styles.input} type="text" name="guestName" id="guestName" placeholder="이름" onChange={handleGuestOnChange} maxLength="5" required />
한규민's avatar
한규민 committed
94
                        <input className={styles.input} type="email" name="guestEmail" id="guestEmail" placeholder="이메일" onChange={handleGuestOnChange} maxLength="16" required />
한규민's avatar
한규민 committed
95
96
                        <input className={styles.input} type="number" name="gusetBirthday" id="gusetBirthday" placeholder="생년월일" onChange={handleGuestOnChange} maxLength="6" required />
                        <input className={styles.input} type="number" name="gusetMbnum" id="gusetMbnum" placeholder="휴대폰 번호" onChange={handleGuestOnChange} maxLength="11" required />
한규민's avatar
context    
한규민 committed
97
                        <input className={styles.input} type="password" name="guestPassword" id="password" placeholder="비밀번호" onChange={handleGuestOnChange} maxLength="11" required />
한규민's avatar
한규민 committed
98
                        <p className={`text-white ${styles.fontSizeTwo}`}>
한규민's avatar
한규민 committed
99
100
                             비회원 정보  입력  예매 내역 확인/취소  티켓 발권이 어려울  있으니 다시 한번 확인해 주시기 바랍니다.
                        </p>
한규민's avatar
push    
한규민 committed
101
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="비회원 예매 확인" disabled={loading} />
한규민's avatar
한규민 committed
102
                    </form>
한규민's avatar
한규민 committed
103
                </div>
한규민's avatar
login    
한규민 committed
104
105
106
107
108
109
            </div>
        </div>
    )
}

export default Login