LoginPage.js 6.83 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    background-color: rgb(239, 218, 200);
    a {
        color : #7B031D;
    }

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

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

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

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

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

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

function Login() {
Ha YeaJin's avatar
Ha YeaJin committed
92
93
94
95
96
97
98
99
100
101
    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
102

Ha YeaJin's avatar
Ha YeaJin committed
103
104
105
    if (state) {
        return <Redirect to="/home" />;
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
106
    return (
Ha YeaJin's avatar
Ha YeaJin committed
107
108
109
110
111
112
113
114
        <Container fluid className="p-0">
            <Row className="vw-100 vh-100 m-0" >
                <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
115
                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
116
117
118
119
120
121
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                </Col_1>
                <Col_2 className={"col-md-8 col-12" + (mobile ? " mob-formik p-0" : " 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);
                                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,
                        }) => (
                                <form onSubmit={handleSubmit} className={mobile ? "w-75 h-50vh" : "d-flex webb"}>
                                    <div className={mobile ? "mobb" : "qwer"}>
                                        <div className={(mobile ? "mob-" : "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 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
pages    
Ha YeaJin committed
177
                                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
178
                                        <button type="submit" className={"btn btn-dark" + (mobile ? " w-100" : " w-20")} disabled={isSubmitting}> Login </button>
Ha YeaJin's avatar
Ha YeaJin committed
179
                                    </div>
180

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

Ha YeaJin's avatar
Ha YeaJin committed
184
185
186
187
188
189
                                </form>
                            )}
                    </Formik>
                </Col_2>
            </Row>
        </Container>
190

Ha YeaJin's avatar
pages    
Ha YeaJin committed
191
192
193
    )
}

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