LoginPage.js 7.01 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, Button } 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 {
Ha YeaJin's avatar
Ha YeaJin committed
61
62
63
64
        display: flex;
        flex-direction: column;
    }

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

Ha YeaJin's avatar
Ha YeaJin committed
69
    & .web-container {
Ha YeaJin's avatar
Ha YeaJin committed
70
71
72
73
74
75
76
        display: flex;
        height: 12vh;
        width: 30vw;
    }

    & .web-input-form {
        width: 80%;
77
        display: flex;
Ha YeaJin's avatar
Ha YeaJin committed
78
79
        flex-direction: column;
        justify-content: space-around;
Ha YeaJin's avatar
Ha YeaJin committed
80
81
82
    }

    & .mob-input-form {
Ha YeaJin's avatar
Ha YeaJin committed
83
84
85
86
        display: flex;
        flex-direction: column;
        justify-content: space-around;

Ha YeaJin's avatar
Ha YeaJin committed
87
88
    }
`
Ha YeaJin's avatar
pages    
Ha YeaJin committed
89
90

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

Ha YeaJin's avatar
Ha YeaJin committed
102
103
104
    if (state) {
        return <Redirect to="/home" />;
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
105
    return (
Ha YeaJin's avatar
Ha YeaJin committed
106
        <Container fluid className="p-0">
Ha YeaJin's avatar
Ha YeaJin committed
107
            <Row className="vw-100 vh-100 m-0 " >
Ha YeaJin's avatar
Ha YeaJin committed
108
109
110
111
112
113
                <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
114
                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
115
                </Col_1>
Ha YeaJin's avatar
Ha YeaJin committed
116
                <Col_2 className="col-md-8 col-12" >
Ha YeaJin's avatar
Ha YeaJin committed
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
                    <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
155
                                <div className={mobile ? " mob-formik p-0" : " web-form"}>
Ha YeaJin's avatar
Ha YeaJin committed
156
                                    <form onSubmit={handleSubmit} className={mobile ? "w-75" : "d-flex webb"}>
Ha YeaJin's avatar
Ha YeaJin committed
157
                                        <div className={mobile ? "mob-container" : "web-container"}>
Ha YeaJin's avatar
Ha YeaJin committed
158
159
                                            <div className={mobile ? "mob-input-form" : "web-input-form mr-2"}>
                                                <div className={"form-group m-0" + (mobile ? " mb-2" : " ")}>
Ha YeaJin's avatar
Ha YeaJin committed
160
161
162
163
164
165
166
167
                                                    <input
                                                        className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
                                                        type="number"
                                                        name="id"
                                                        {...getFieldProps('id')}
                                                        placeholder="Input Student Id"
                                                    />
                                                </div>
Ha YeaJin's avatar
Ha YeaJin committed
168
                                                <div className={"form-group m-0"+ (mobile ? " mb-2" : " ")}>
Ha YeaJin's avatar
Ha YeaJin committed
169
170
171
172
173
174
175
176
                                                    <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
177
                                            </div>
Ha YeaJin's avatar
Ha YeaJin committed
178
                                            <Button type="submit" variant="dark" className={mobile ? " w-100" : " w-20"} disabled={isSubmitting}> Login </Button>
Ha YeaJin's avatar
pages    
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
                                    </form>
                                </div>
Ha YeaJin's avatar
Ha YeaJin committed
186
187
188
189
190
                            )}
                    </Formik>
                </Col_2>
            </Row>
        </Container>
191

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

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