Login.js 6.07 KB
Newer Older
한규민's avatar
한규민 committed
1
2
import { useState } from "react";
import { Redirect } from "react-router-dom";
한규민's avatar
한규민 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 = () => {
한규민's avatar
한규민 committed
8
    const { login, guestLogin, loading } = useAuth();
9
10
    //useState를 이용해서 각 state 생성 및 초기값 저장
    const [state, setState] = useState(true); // 이 줄은 css에 해당하는 state
한규민's avatar
한규민 committed
11
    //state변수 지정 하지만 이 변수는 react에 의해 없어지지 않음, 그리고 그 다음 변수는 state변수를 갱신해주는 함수
한규민's avatar
한규민 committed
12
    const [user, setUser] = useState({
한규민's avatar
한규민 committed
13
14
        id: "",
        password: ""
15
    });
한규민's avatar
한규민 committed
16
    const [error, setError] = useState("");
한규민's avatar
한규민 committed
17
    const [success, setSuccess] = useState(false);
한규민's avatar
한규민 committed
18
    const [guest, setGuset] = useState({
한규민's avatar
한규민 committed
19
20
21
22
23
        guestName: "",
        guestEmail: "",
        guestBirthday: "",
        guestMbnum: "",
        guestPassword: ""
24
25
    })

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

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

한규민's avatar
context    
한규민 committed
40
    const requestServer = async (data) => {
한규민's avatar
한규민 committed
41
42
43
44
45
46
47
48
49
50
51
52
53
        try {
            if (data === user) {
                const success = await login(data);
                if (success) {
                    setSuccess("member");
                    alert('로그인이 완료되었습니다.')
                }
            } else {
                const success = await guestLogin(data);
                if (success) {
                    setSuccess("guest");
                    alert('로그인이 완료되었습니다.')
                }
한규민's avatar
한규민 committed
54
            }
한규민's avatar
한규민 committed
55
56
        } catch (error) {
            catchErrors(error, setError);
한규민's avatar
한규민 committed
57
        }
한규민's avatar
한규민 committed
58
59
    }

한규민's avatar
push    
한규민 committed
60
    const handleOnSummit = async (e) => {
한규민's avatar
한규민 committed
61
        e.preventDefault();
한규민's avatar
push    
한규민 committed
62
        try {
한규민's avatar
한규민 committed
63
            setError("");
한규민's avatar
push    
한규민 committed
64
            if (e.target.name === "login") {
한규민's avatar
한규민 committed
65
                requestServer(user);
한규민's avatar
push    
한규민 committed
66
67
68
69
70
            }
            else {
                requestServer(guest);
            }
        } catch (error) {
한규민's avatar
한규민 committed
71
72
73
74
            catchErrors(error, setError);
        }
    }

한규민's avatar
한규민 committed
75
    if (success === "member") {
한규민's avatar
한규민 committed
76
        return <Redirect to="/" />;
한규민's avatar
한규민 committed
77
78
    } else if (success === "guest"){
        return <Redirect to="/guest" />;
한규민's avatar
한규민 committed
79
    }
한규민's avatar
push    
한규민 committed
80

한규민's avatar
한규민 committed
81
    return (
한규민's avatar
한규민 committed
82
        <div className={`d-flex flex-column col-md-5 col-10`}>
한규민's avatar
한규민 committed
83
            <span className={styles.title}>로그인</span>
한규민's avatar
한규민 committed
84
            <ul className="nav nav-fill nav-tabs w-100" id="loginTab" role="tablist">
한규민's avatar
login    
한규민 committed
85
                <li className="nav-item fs-6" role="presentation">
한규민's avatar
push    
한규민 committed
86
87
88
                    <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
89
90
                </li>
                <li className="nav-item" role="presentation">
한규민's avatar
login    
한규민 committed
91
                    <button className={`nav-link px-2 ${styles.fontSize}`}
한규민's avatar
push    
한규민 committed
92
93
                        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
94
95
96
                </li>
            </ul>
            <div className="tab-content w-100" id="myTabContent">
한규민's avatar
한규민 committed
97
                <div className="tab-pane fade show active" id="login" role="tabpanel" aria-labelledby="login-tab">
한규민's avatar
push    
한규민 committed
98
                    <form className="d-flex flex-column" name="login" onSubmit={handleOnSummit}>
한규민's avatar
한규민 committed
99
100
                        <input className={styles.input} type="text" name="id" placeholder="ID" onChange={handleLoginOnChange} maxLength="10"/>
                        <input className={styles.input} type="password" name="password" placeholder="Password" onChange={handleLoginOnChange} maxLength="11"/>
한규민's avatar
push    
한규민 committed
101
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="Login" disabled={loading} />
한규민's avatar
한규민 committed
102
                        <span><a href="./signup" className={styles.intoSignupPage}>회원이 아니십니까?</a></span>
한규민's avatar
한규민 committed
103
                    </form>
한규민's avatar
한규민 committed
104
105
                </div>
                <div className="tab-pane fade" id="guest" role="tabpanel" aria-labelledby="guest-tab">
한규민's avatar
context    
한규민 committed
106
                    <form className="d-flex flex-column" onSubmit={handleOnSummit}>
한규민's avatar
한규민 committed
107
108
109
110
111
                        <input className={styles.input} type="text" name="guestName" id="guestName" placeholder="이름" onChange={handleGuestOnChange} maxLength="10"/>
                        <input className={styles.input} type="email" name="guestEmail" id="guestEmail" placeholder="이메일" onChange={handleGuestOnChange} maxLength="20"/>
                        <input className={styles.input} type="number" name="guestBirthday" id="guestBirthday" placeholder="생년월일" onChange={handleGuestOnChange}  min="0" max="999999" />
                        <input className={styles.input} type="number" name="guestMbnum" id="guestMbnum" placeholder="휴대폰 번호" onChange={handleGuestOnChange}  min="0" max="99999999999" />
                        <input className={styles.input} type="password" name="guestPassword" id="guestPassword" placeholder="비밀번호" onChange={handleGuestOnChange} maxLength="20"/>
한규민's avatar
한규민 committed
112
                        <p className={`text-white ${styles.fontSizeTwo}`}>
한규민's avatar
한규민 committed
113
114
                             비회원 정보  입력  예매 내역 확인/취소  티켓 발권이 어려울  있으니 다시 한번 확인해 주시기 바랍니다.
                        </p>
한규민's avatar
push    
한규민 committed
115
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="비회원 예매 확인" disabled={loading} />
한규민's avatar
한규민 committed
116
                    </form>
한규민's avatar
한규민 committed
117
                </div>
한규민's avatar
login    
한규민 committed
118
119
120
121
122
123
            </div>
        </div>
    )
}

export default Login