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

한규민's avatar
한규민 committed
7
const Login = () => {
한규민's avatar
한규민 committed
8
    const {login, 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
push    
한규민 committed
13
14
        id: '',
        password: ''
15
    });
한규민's avatar
한규민 committed
16
    const [error, setError] = useState("");
한규민's avatar
한규민 committed
17
    const [success, setSuccess] = useState(false);
18

한규민's avatar
한규민 committed
19
    const [guest, setGuset] = useState({
한규민's avatar
push    
한규민 committed
20
21
22
23
        guestName: '',
        gusetBirthday: '',
        gusetMbnum: '',
        guestPassword: ''
24
25
26
    })

    //input태그에 걸려있는 onchange에서 실행할 함수설정
한규민's avatar
push    
한규민 committed
27
    const handleLoginOnChange = (e) => {
28
29
        // ... 전개 연산자
        // 현재 state에 방금 변화한 값을 다시 저장함
한규민's avatar
한규민 committed
30
31
        setUser({
            ...user,
한규민's avatar
push    
한규민 committed
32
            [e.target.name]: e.target.value
33
34
35
        })
    };

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

한규민's avatar
context    
한규민 committed
43
    const requestServer = async (data) => {
한규민's avatar
한규민 committed
44
        if(data === user){
한규민's avatar
한규민 committed
45
46
47
48
49
50
            const success = await login(data);
            if(success){
                setSuccess(true);
                alert('로그인이 완료되었습니다.')
            }
            }else{
한규민's avatar
한규민 committed
51
        }
한규민's avatar
한규민 committed
52
53
    }

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

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

한규민's avatar
한규민 committed
74
    return (
한규민's avatar
한규민 committed
75
        <div className={`d-flex flex-column col-md-5 col-10`}>
한규민's avatar
한규민 committed
76
            <span className={styles.title}>로그인</span>
한규민's avatar
한규민 committed
77
            <ul className="nav nav-fill nav-tabs w-100" id="loginTab" role="tablist">
한규민's avatar
login    
한규민 committed
78
                <li className="nav-item fs-6" role="presentation">
한규민's avatar
push    
한규민 committed
79
80
81
                    <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
82
83
                </li>
                <li className="nav-item" role="presentation">
한규민's avatar
login    
한규민 committed
84
                    <button className={`nav-link px-2 ${styles.fontSize}`}
한규민's avatar
push    
한규민 committed
85
86
                        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
87
88
89
                </li>
            </ul>
            <div className="tab-content w-100" id="myTabContent">
한규민's avatar
한규민 committed
90
91
                {/* 로그인 */}
                <div className="tab-pane fade show active" id="login" role="tabpanel" aria-labelledby="login-tab">
한규민's avatar
push    
한규민 committed
92
93
                    <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
94
                        <input className={styles.input} type="password" name="password" placeholder="Password" onChange={handleLoginOnChange} maxLength="11" required />
한규민's avatar
push    
한규민 committed
95
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="Login" disabled={loading} />
한규민's avatar
한규민 committed
96
                        <span><a href="./signup" className={styles.intoSignupPage}>회원이 아니십니까?</a></span>
한규민's avatar
한규민 committed
97
                    </form>
한규민's avatar
한규민 committed
98
99
100
                </div>
                {/* 비회원예매 학인 */}
                <div className="tab-pane fade" id="guest" role="tabpanel" aria-labelledby="guest-tab">
한규민's avatar
context    
한규민 committed
101
                    <form className="d-flex flex-column" onSubmit={handleOnSummit}>
한규민's avatar
한규민 committed
102
                        <input className={styles.input} type="text" name="guestName" id="guestName" placeholder="이름" onChange={handleGuestOnChange} maxLength="5" required />
한규민's avatar
한규민 committed
103
                        <input className={styles.input} type="email" name="guestEmail" id="guestEmail" placeholder="이메일" onChange={handleGuestOnChange} maxLength="16" required />
한규민's avatar
한규민 committed
104
105
                        <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
106
                        <input className={styles.input} type="password" name="guestPassword" id="password" placeholder="비밀번호" onChange={handleGuestOnChange} maxLength="11" required />
한규민's avatar
한규민 committed
107
108

                        <p className={`text-white ${styles.fontSizeTwo}`}>
한규민's avatar
한규민 committed
109
110
                             비회원 정보  입력  예매 내역 확인/취소  티켓 발권이 어려울  있으니 다시 한번 확인해 주시기 바랍니다.
                        </p>
한규민's avatar
push    
한규민 committed
111
                        <input className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} type="submit" value="비회원 예매 확인" disabled={loading} />
한규민's avatar
한규민 committed
112
                    </form>
한규민's avatar
한규민 committed
113
                </div>
한규민's avatar
login    
한규민 committed
114
115
116
117
118
119
            </div>
        </div>
    )
}

export default Login