LoginPage.js 6.57 KB
Newer Older
Ha YeaJin's avatar
Ha YeaJin committed
1
import React, { useState, useEffect, useRef } from 'react';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
2
import styled from 'styled-components';
CHAERIN KIM's avatar
CHAERIN KIM committed
3
import { Link, Redirect } from 'react-router-dom';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
4
5
import { Formik } from 'formik';
import * as Yup from 'yup';
CHAERIN KIM's avatar
CHAERIN KIM committed
6
7
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css';
Ha YeaJin's avatar
Ha YeaJin committed
8
import Logo from '../icon.png';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
9

Ha YeaJin's avatar
Ha YeaJin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const Asd = styled.div`
    background-color: #7B031D;

    &.web {
        display : flex;
        align-items: center;
    }

    &.mobile {
        height : 20vh;
        display : flex;

    }
    
    & .mob-head {
        display: flex;
        flex-direction: row;
        height : 100%;
        width: 100%;
        justify-content: center;
        align-content: center;
    }

    & .mob-img {
        max-width:100%;
        max-height:100%;
        }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
37
38
`

Ha YeaJin's avatar
Ha YeaJin committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const Asdf = styled.div`
    background-color: rgb(239, 218, 200);
    a {
        color : #7B031D;
    }

    &.mob-formik {
        height : 80vh;
        width: 100%;
        padding: 0;
        display: flex; 
        justify-content: center;
        align-items: center; 
    }

    &.web-formik {
        height: 100%;
        display: flex; 
        align-items: center; 
        justify-content: center;
    }

    & .mobb {
        height: 30vh;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
    }

    & .qwer {
        display: flex;
        justify-content: space-between;
        height: 12vh;
        width: 30vw;
        margin-bottom: 25px;
    }

    & .web-input-form {
        width: 80%;
        justify-content: space-between;
        align-content: space-around;
        display: flex;
        flex-direction: column;
    }

    & .mob-input-form {
      
    }
`
Ha YeaJin's avatar
pages    
Ha YeaJin committed
88
89

function Login() {
Ha YeaJin's avatar
Ha YeaJin committed
90
91
92
93
94
95
96
97
98
99
    const [state, setState] = useState(false);
    const [mobile, setMobile] = useState(false);

    useEffect(() => {
        if (window.innerWidth < 960) {
            setMobile(true)
        } else {
            setMobile(false)
        }
    }, []);
CHAERIN KIM's avatar
CHAERIN KIM committed
100

Ha YeaJin's avatar
Ha YeaJin committed
101
102
103
    if (state) {
        return <Redirect to="/home" />;
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
104
    return (
Ha YeaJin's avatar
Ha YeaJin committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        <div className="row vw-100 vh-100">
            <Asd className={"col-md-4 px-0" + (mobile ? " mobile" : " web")}>
                <div className={mobile ? " mob-head" : ""}>
                    <img className={mobile ? " mob-img" : "img-fluid"} src={Logo} />
                    <div className="d-flex justify-content-center">
                        <h1 className="font-weight-bold text-white align-self-center">고려대학교<br />대관 서비스</h1>
                    </div>
                </div>
            </Asd>
            <Asdf className={"col-md-8 col-12" + (mobile ? " mob-formik" : " web-formik")}>
                <Formik
                    initialValues={{ id: '', password: '' }}
                    validationSchema={Yup.object({
                        id: Yup.string()
                            .required('학번을 입력해주세요.'),
                        password: Yup.string()
                            .required('비밀번호를 입력해주세요.')
                            .min(8, '8자 이상 입력해주세요.'),
                    })}
                    onSubmit={(values, { setSubmitting }) => {
                        axios({
                            method: 'post',
                            url: '/login',
                            data: values,
                        }).then(res => {
                            if (res.status === 404) return alert(res.data.error)

                            localStorage.setItem('token', res.data.token);
                            localStorage.setItem('id', res.data.users._id);
                            setState(true);
                        })
                            .catch(err => {
CHAERIN KIM's avatar
CHAERIN KIM committed
137
                                alert(err.error)
Ha YeaJin's avatar
Ha YeaJin committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
                            });

                        setTimeout(() => {
                            setSubmitting(false);
                        }, 400);  // finish the cycle in handler
                    }}
                >
                    {({
                        errors,
                        touched,
                        handleSubmit,
                        getFieldProps,  // contain values, handleChange, handleBlur
                        isSubmitting,
                    }) => (
                            <form onSubmit={handleSubmit} className={mobile ? "w-90 h-50vh" : ""}>
                                <div className={mobile ? "mobb" : "qwer"}>
                                    <div className={(mobile ? "mob-" : "web-") + "input-form"}>
                                        <div className={"form-group m-0" + (mobile ? " mb-3" : "")}>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
156
                                            <input
CHAERIN KIM's avatar
CHAERIN KIM committed
157
                                                className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
CHAERIN KIM's avatar
CHAERIN KIM committed
158
                                                type="number"
CHAERIN KIM's avatar
CHAERIN KIM committed
159
160
                                                name="id"
                                                {...getFieldProps('id')}
CHAERIN KIM's avatar
CHAERIN KIM committed
161
                                                placeholder="Input Student Id"
Ha YeaJin's avatar
pages    
Ha YeaJin committed
162
163
                                            />
                                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
164
                                        <div className="form-group m-0">
Ha YeaJin's avatar
pages    
Ha YeaJin committed
165
166
167
168
169
170
171
172
                                            <input
                                                className={(touched.password && errors.password ? 'form-control is-invalid' : "form-control")}
                                                type="password"
                                                name="password"
                                                {...getFieldProps('password')}
                                                placeholder="Input Password"
                                            />
                                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
173
                                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
174
175
                                    <button type="submit" className={"btn btn-dark" + (mobile ? " w-100" : " w-20")} disabled={isSubmitting}>
                                        Login
176
                                        </button>
Ha YeaJin's avatar
Ha YeaJin committed
177
178
179
180
181
                                    <button><Link to="/home"></Link></button>
                                    <div></div>
                                    <Link to="/find">비밀번호를 잊으셨나요?</Link>
                                    <div></div>
                                    <Link to="/signup">회원이 아니신가요?</Link>
Ha YeaJin's avatar
Ha YeaJin committed
182
183
184
185
186
187
                                </div>
                            </form>
                        )}
                </Formik>
            </Asdf>
        </div >
Ha YeaJin's avatar
pages    
Ha YeaJin committed
188
189
190
191
    )
}

export default Login