LoginPage.js 7.01 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
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
37
38
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { Link, Redirect } from 'react-router-dom';
import { Formik } from 'formik';
import * as Yup from 'yup';
import axios from 'axios';
import Logo from '../icon.png';
import { Container, Row, Button } from 'react-bootstrap';

const Col_1 = styled.div`
    background-color: #7B031D;

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

    &.mobile {
        height : 20vh;
        display : flex;
        padding:0;
    }
    
    & .mob-head {
        display: flex;
        flex-direction: row;
        height : 100%;
        width: 100%;
        justify-content: space-evenly;
    }

    & .mob-img {
        max-width: 30vw;
    }
`

const Col_2 = 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-form {
        height: 100%;
        display: flex; 
        align-items: center; 
        justify-content: center;
    }

    & .mob-container {
        display: flex;
        flex-direction: column;
    }

    & .webb {
        flex-direction: column;
    }

    & .web-container {
        display: flex;
        height: 12vh;
        width: 30vw;
    }

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

    & .mob-input-form {
        display: flex;
        flex-direction: column;
        justify-content: space-around;

    }
`

function Login() {
    const [state, setState] = useState(false);
    const [mobile, setMobile] = useState(false);

    useEffect(() => {
        if (window.innerWidth < 960) {
            setMobile(true)
        } else {
            setMobile(false)
        }
    }, []);

    if (state) {
        return <Redirect to="/home" />;
    }
    return (
        <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>
                    </div>
                </Col_1>
                <Col_2 className="col-md-8 col-12" >
                    <Formik
                        initialValues={{ id: '', password: '' }}
                        validationSchema={Yup.object({
                            id: Yup.string()
                                .required('학번을 입력해주세요.'),
                            password: Yup.string()
                                .required('비밀번호를 입력해주세요.')
                                .min(8, '8자 이상 입력해주세요.'),
                        })}
                        onSubmit={(values, { setSubmitting }) => {
                            axios({
                                method: 'post',
Yoon, Daeki's avatar
Yoon, Daeki committed
128
                                url: '/api/login',
Yoon, Daeki's avatar
Yoon, Daeki committed
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                                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,
                        }) => (
                                <div className={mobile ? " mob-formik p-0" : " web-form"}>
                                    <form onSubmit={handleSubmit} className={mobile ? "w-75" : "d-flex webb"}>
                                        <div className={mobile ? "mob-container" : "web-container"}>
                                            <div className={mobile ? "mob-input-form" : "web-input-form mr-2"}>
                                                <div className={"form-group m-0" + (mobile ? " mb-2" : " ")}>
                                                    <input
                                                        className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
                                                        type="text"
                                                        name="id"
                                                        {...getFieldProps('id')}
                                                        placeholder="Input Student Id"
                                                    />
                                                </div>
                                                <div className={"form-group m-0"+ (mobile ? " mb-2" : " ")}>
                                                    <input
                                                        className={(touched.password && errors.password ? 'form-control is-invalid' : "form-control")}
                                                        type="password"
                                                        name="password"
                                                        {...getFieldProps('password')}
                                                        placeholder="Input Password"
                                                    />
                                                </div>
                                            </div>
                                            <Button type="submit" variant="dark" className={mobile ? " w-100" : " w-20"} disabled={isSubmitting}> Login </Button>
                                        </div>
                                        <div><Link to="/find">비밀번호를 잊으셨나요?</Link></div>
                                        <div><Link to="/signup">회원이 아니신가요?</Link></div>
                                    </form>
                                </div>
                            )}
                    </Formik>
                </Col_2>
            </Row>
        </Container>
    )
}

export default Login