LoginPage.js 6.45 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
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
const Asd = styled.div`
    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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const Asdf = styled.div`
    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 (
107
108
109
110
111
112
        <div className="row vw-100 vh-100 m-0">
            <Asd 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">고려대학교<br />대관 서비스</h1>
Ha YeaJin's avatar
Ha YeaJin committed
113
114
115
                    </div>
                </div>
            </Asd>
116
            <Asdf className={"col-md-8 col-12" + (mobile ? " mob-formik p-0" : " web-formik")}>
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
                <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);
CHAERIN KIM's avatar
CHAERIN KIM committed
135
                            localStorage.setItem('_id', res.data.users._id);
136
                            localStorage.setItem('name', res.data.users.name);
Ha YeaJin's avatar
Ha YeaJin committed
137
138
139
                            setState(true);
                        })
                            .catch(err => {
CHAERIN KIM's avatar
CHAERIN KIM committed
140
                                alert(err.error)
Ha YeaJin's avatar
Ha YeaJin committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
                            });

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

                                <div><Link to="/find">비밀번호를 잊으셨나요?</Link></div>
                                <div><Link to="/signup">회원이 아니신가요?</Link></div>

Ha YeaJin's avatar
Ha YeaJin committed
183
184
                            </form>
                        )}
185

Ha YeaJin's avatar
Ha YeaJin committed
186
187
188
                </Formik>
            </Asdf>
        </div >
Ha YeaJin's avatar
pages    
Ha YeaJin committed
189
190
191
192
    )
}

export default Login