LoginPage.js 7.16 KB
Newer Older
1
import React, { useState, useEffect } 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
import axios from 'axios';
Ha YeaJin's avatar
Ha YeaJin committed
7
import Logo from '../icon.png';
Ha YeaJin's avatar
Ha YeaJin committed
8
import { Container, Row } from 'react-bootstrap';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
9

Ha YeaJin's avatar
Ha YeaJin committed
10
const Col_1 = styled.div`
Ha YeaJin's avatar
Ha YeaJin committed
11
12
13
14
15
16
17
18
19
20
    background-color: #7B031D;

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

    &.mobile {
        height : 20vh;
        display : flex;
21
        padding:0;
Ha YeaJin's avatar
Ha YeaJin committed
22
23
24
25
26
27
28
    }
    
    & .mob-head {
        display: flex;
        flex-direction: row;
        height : 100%;
        width: 100%;
29
30
        justify-content: space-evenly;

Ha YeaJin's avatar
Ha YeaJin committed
31
32
33
    }

    & .mob-img {
34
35
        max-width: 30vw;
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
36
37
`

Ha YeaJin's avatar
Ha YeaJin committed
38
const Col_2 = styled.div`
Ha YeaJin's avatar
Ha YeaJin committed
39
    background-color: rgb(239, 218, 200);
Ha YeaJin's avatar
Ha YeaJin committed
40

Ha YeaJin's avatar
Ha YeaJin committed
41
42
43
44
    a {
        color : #7B031D;
    }

Ha YeaJin's avatar
Ha YeaJin committed
45
    & .mob-formik {
Ha YeaJin's avatar
Ha YeaJin committed
46
47
48
49
50
51
52
        height : 80vh;
        width: 100%;
        display: flex; 
        justify-content: center;
        align-items: center; 
    }

Ha YeaJin's avatar
Ha YeaJin committed
53
    & .web-form {
Ha YeaJin's avatar
Ha YeaJin committed
54
55
56
57
58
59
        height: 100%;
        display: flex; 
        align-items: center; 
        justify-content: center;
    }

Ha YeaJin's avatar
Ha YeaJin committed
60
    & .mob-container {
61
        height: 35vh;
Ha YeaJin's avatar
Ha YeaJin committed
62
63
64
65
66
        display: flex;
        flex-direction: column;
        justify-content: space-around;
    }

67
68
69
70
    & .webb {
        flex-direction: column;
    }

Ha YeaJin's avatar
Ha YeaJin committed
71
    & .web-container {
Ha YeaJin's avatar
Ha YeaJin committed
72
73
74
75
76
77
78
79
80
81
82
83
        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;
        flex-direction: column;
84
        display: flex;
Ha YeaJin's avatar
Ha YeaJin committed
85
86
87
    }

    & .mob-input-form {
Ha YeaJin's avatar
Ha YeaJin committed
88
89
90
91
        display: flex;
        flex-direction: column;
        justify-content: space-around;

Ha YeaJin's avatar
Ha YeaJin committed
92
93
    }
`
Ha YeaJin's avatar
pages    
Ha YeaJin committed
94
95

function Login() {
Ha YeaJin's avatar
Ha YeaJin committed
96
97
98
99
100
101
102
103
104
105
    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
106

Ha YeaJin's avatar
Ha YeaJin committed
107
108
109
    if (state) {
        return <Redirect to="/home" />;
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
110
    return (
Ha YeaJin's avatar
Ha YeaJin committed
111
        <Container fluid className="p-0">
Ha YeaJin's avatar
Ha YeaJin committed
112
            <Row className="vw-100 vh-100 m-0 " >
Ha YeaJin's avatar
Ha YeaJin committed
113
114
115
116
117
118
                <Col_1 className={"col-md-4 col-12" + (mobile ? " mobile" : " web")}>
                    <div className={mobile ? "mob-head" : ""}>
                        <img className={mobile ? "mob-img" : "img-fluid"} src={Logo} />
                        <div className={"d-flex " + (mobile ? "align-items-center" : "justify-content-center")}>
                            <h1 className="font-weight-bold text-white text-center">고려대학교<br />대관 서비스</h1>
                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
119
                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
120
                </Col_1>
Ha YeaJin's avatar
Ha YeaJin committed
121
                <Col_2 className="col-md-8 col-12" >
Ha YeaJin's avatar
Ha YeaJin committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                    <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);
                                localStorage.setItem('name', res.data.users.name);
                                setState(true);
                            })
                                .catch(err => {
                                    alert(err.error)
                                });

                            setTimeout(() => {
                                setSubmitting(false);
                            }, 400);  // finish the cycle in handler
                        }}
                    >
                        {({
                            errors,
                            touched,
                            handleSubmit,
                            getFieldProps,  // contain values, handleChange, handleBlur
                            isSubmitting,
                        }) => (
Ha YeaJin's avatar
Ha YeaJin committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                                <div className={mobile ? " mob-formik p-0" : " web-form"}>
                                    <form onSubmit={handleSubmit} className={mobile ? "w-75 h-50vh" : "d-flex webb"}>
                                        <div className={mobile ? "mob-container" : "web-container"}>
                                            <div className={mobile ? "mob-input-form h-100" : "web-input-form"}>
                                                <div className={"form-group m-0" + (mobile ? " mb-2" : "")}>
                                                    <input
                                                        className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
                                                        type="number"
                                                        name="id"
                                                        {...getFieldProps('id')}
                                                        placeholder="Input Student Id"
                                                    />
                                                </div>
                                                <div className="form-group h-80 m-0">
                                                    <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
182
                                            </div>
Ha YeaJin's avatar
Ha YeaJin committed
183
                                            <button type="submit" className={"btn btn-dark" + (mobile ? " w-100" : " w-20")} disabled={isSubmitting}> Login </button>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
184
                                        </div>
185

Ha YeaJin's avatar
Ha YeaJin committed
186
187
                                        <div><Link to="/find">비밀번호를 잊으셨나요?</Link></div>
                                        <div><Link to="/signup">회원이 아니신가요?</Link></div>
188

Ha YeaJin's avatar
Ha YeaJin committed
189
190
                                    </form>
                                </div>
Ha YeaJin's avatar
Ha YeaJin committed
191
192
193
194
195
                            )}
                    </Formik>
                </Col_2>
            </Row>
        </Container>
196

Ha YeaJin's avatar
pages    
Ha YeaJin committed
197
198
199
    )
}

Lee Jin Ju's avatar
Lee Jin Ju committed
200
export default Login;