Signup.js 8.81 KB
Newer Older
이재연's avatar
이재연 committed
1
import React, { useState } from 'react';
이재연's avatar
aa    
이재연 committed
2
import axios from 'axios'
이재연's avatar
이재연 committed
3
import { Form, Col, Container, Button, Row, Alert } from 'react-bootstrap'
이재연's avatar
aa    
이재연 committed
4
import catchErrors from '../utils/catchErrors'
이재연's avatar
이재연 committed
5
import { Redirect } from 'react-router-dom';
이재연's avatar
이재연 committed
6

이재연's avatar
이재연 committed
7
8
9
10
11
12
const INIT_USER = {
    name: '',
    number: '',
    id: '',
    password: '',
    password2: '',
박상호's avatar
0120    
박상호 committed
13
14
    tel: '',
    email: ''
이재연's avatar
이재연 committed
15
}
Kim, Subin's avatar
Kim, Subin committed
16
17

function Signup() {
18
    const [user, setUser] = useState(INIT_USER)
이재연's avatar
이재연 committed
19
    const [error, setError] = useState('')
이재연's avatar
이재연 committed
20
21
    const [success, setSuccess] = useState(false)
    const [validated, setValidated] = useState(false);
이재연's avatar
이재연 committed
22

이재연's avatar
이재연 committed
23
24
25
    function handleChange(event) {
        const { name, value } = event.target
        setUser({ ...user, [name]: value })
이재연's avatar
이재연 committed
26
    }
이재연's avatar
이재연 committed
27

이재연's avatar
이재연 committed
28
29
30
    async function handleSubmit(event) {
        event.preventDefault()
        const form = event.currentTarget;
이재연's avatar
이재연 committed
31
        if (form.checkValidity() === false) {
이재연's avatar
이재연 committed
32
33
            event.preventDefault();
            event.stopPropagation();
이재연's avatar
이재연 committed
34
35
        }
        setValidated(true);
이재연's avatar
aa    
이재연 committed
36

이재연's avatar
이재연 committed
37
38
        try {
            setError('')
이재연's avatar
aa    
이재연 committed
39
            const response = await axios.post('/api/users/signup', user)
이재연's avatar
이재연 committed
40
            setSuccess(true)
이재연's avatar
이재연 committed
41
        } catch (error) {
이재연's avatar
aa    
이재연 committed
42
            catchErrors(error, setError)
이재연's avatar
이재연 committed
43
        }
이재연's avatar
이재연 committed
44
    }
이재연's avatar
이재연 committed
45

이재연's avatar
이재연 committed
46
47
48
49
50
    function checkPassword(event) {
        const p1 = user.password
        const p2 = user.password2

        if (p1 !== p2) {
이재연's avatar
이재연 committed
51
52
53
54
            event.preventDefault();
            event.stopPropagation();
            alert('비밀번호가 일치하지 않습니다.')
            return false
이재연's avatar
이재연 committed
55
56

        } else {
이재연's avatar
이재연 committed
57
58
59
            return true
        }
    }
이재연's avatar
..    
이재연 committed
60

이재연's avatar
이재연 committed
61
62
    if (success) {
        alert('회원가입 되었습니다.')
박상호's avatar
0120    
박상호 committed
63
        return <Redirect to='/login' />
이재연's avatar
이재연 committed
64
65
    }

박상호's avatar
0120    
박상호 committed
66
    return (
67
68
69
70
71
72
73
74
75
76
77
        <Container className="my-5">
            <Row className="justify-content-center">
                <Col md={6} xs={10} className="border" style={{ background: '#F7F3F3' }}>
                    <h2 className="text-center pt-3 m-4">Sign Up</h2>
                    {error && <Alert variant='danger'>
                        {error}
                    </Alert>}
                    <Form
                        noValidate validated={validated}
                        onSubmit={handleSubmit}
                        className="p-4">
이재연's avatar
aa    
이재연 committed
78

79
80
81
82
83
84
85
86
87
88
89
90
91
                        <Form.Group as={Row} controlId="formBasicName">
                            <Form.Label column sm="4" for='name'>
                                     </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    required type="text"
                                    name="name"
                                    placeholder="Name"
                                    value={user.name}
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >이름을 입력하세요. </Form.Control.Feedback>
                            </Col>
                        </Form.Group>
박상호's avatar
0120    
박상호 committed
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
                        <Form.Group as={Row} controlId="formBasicNumber">
                            <Form.Label column sm="4" for='number'>
                                주민등록번호    </Form.Label>
                            <Col sm="4" xs='5'>
                                <Form.Control
                                    className='pr-0'
                                    required type="text"
                                    name="number1"
                                    maxlength="6"
                                    placeholder="생년월일"
                                    value={user.number1}
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >주민등록번호 입력하세요. </Form.Control.Feedback>
                            </Col>
                            <strong className='pt-2 d-flex align-items-flex-start'>-</strong>
                            <Col md="2" xs='3'>
                                <Form.Control
                                    className='pr-0'
                                    required type="text"
                                    name="number2"
                                    maxlength="1"
                                    value={user.number2}
                                    onChange={handleChange} />
                            </Col>
                            <strong className='pt-2 d-flex align-items-flex-start'>* * * * * *</strong>
                        </Form.Group>
                        <Form.Group as={Row} controlId="formBasicId">
                            <Form.Label column sm="4" for='id'>
                                아이디    </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    required type="text"
                                    name="id"
                                    placeholder="ID"
                                    value={user.id}
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >아이디를 입력하세요. </Form.Control.Feedback>
                            </Col>
                        </Form.Group>
                        <Form.Group as={Row} controlId="formBasicPassword">
                            <Form.Label column sm="4" for='password'>
                                비밀번호    </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    type="password"
                                    name="password"
                                    placeholder="Password"
                                    value={user.password}
                                    required
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >비밀번호를 입력하세요. </Form.Control.Feedback>
                            </Col>
                        </Form.Group>
                        <Form.Group as={Row} controlId="formBasicPassword2">
                            <Form.Label column sm="4" for='password'>
                                비밀번호 확인   </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    type="password"
                                    name="password2"
                                    placeholder="Password"
                                    value={user.password2}
                                    required
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >비밀번호를 한번  입력하세요.</Form.Control.Feedback>
                            </Col>
                        </Form.Group>
                        <Form.Group as={Row} controlId="formBasicEmail">
                            <Form.Label column sm="4" for='email'>
                                이메일   </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    required type="email"
                                    name="email"
                                    placeholder="E-mail"
                                    value={user.email}
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >이메일을 입력하세요.</Form.Control.Feedback>
                            </Col>
                        </Form.Group>
                        <Form.Group as={Row} controlId="formBasicTel">
                            <Form.Label column sm="4" for='tel'>
                                휴대전화   </Form.Label>
                            <Col sm="8">
                                <Form.Control
                                    required type="text"
                                    name="tel"
                                    placeholder="Telephone"
                                    value={user.tel}
                                    onChange={handleChange} />
                                <Form.Control.Feedback type="invalid" >휴대전화를 입력하세요.</Form.Control.Feedback>
                                <Row className='text-end pl-3 mt-1'><small >' - '  함께 입력해주세요^^</small></Row>
                            </Col>
                        </Form.Group>
                        <Button
                            style={{ background: '#91877F', borderColor: '#91877F', margin: 'auto' }} type="submit" block
                            onClick={checkPassword} >
                            Sign Up
이재연's avatar
이재연 committed
191
                            </Button>
192
193
194
195
                    </Form>
                </Col>
            </Row>
        </Container>
박상호's avatar
0120    
박상호 committed
196
    )
Kim, Subin's avatar
Kim, Subin committed
197
198
199
}

export default Signup