Signup.js 10.8 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
이재연 committed
36
        console.log(user)
이재연's avatar
aa    
이재연 committed
37

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

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

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

        } else {
이재연's avatar
이재연 committed
59
60
61
            return true
        }
    }
이재연's avatar
..    
이재연 committed
62

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

박상호's avatar
0120    
박상호 committed
68
69
70
    return (
        <div>
            <Container className="my-5">
이재연's avatar
aa    
이재연 committed
71

박상호's avatar
0120    
박상호 committed
72
73
                <Row className="justify-content-center">
                    <Col md={6} xs={10} className="border" style={{ background: '#F7F3F3' }}>
박상호's avatar
q    
박상호 committed
74
                        <h2 className="text-center m-5">Sign Up</h2>
박상호's avatar
0120    
박상호 committed
75
76
77
78
79
80
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
                        <Form
                            noValidate validated={validated}
                            onSubmit={handleSubmit}
박상호's avatar
q    
박상호 committed
81
82
                            className="p-4">
                            <Form.Group as={Col} controlId="formBasicName" className="justify-content-end">
박상호's avatar
0120    
박상호 committed
83
84
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="id"> </Col>
박상호's avatar
q    
박상호 committed
85
                                    <Col sm={8} xs={6} as={Form.Control}
이재연's avatar
이재연 committed
86
                                        required type="text"
박상호's avatar
0120    
박상호 committed
87
88
89
90
91
                                        name="name"
                                        placeholder="Name"
                                        style={{ width: '160px' }}
                                        value={user.name}
                                        onChange={handleChange} />
박상호's avatar
q    
박상호 committed
92
                                    <Form.Control.Feedback type="invalid" >이름을 입력하세요. </Form.Control.Feedback>
박상호's avatar
0120    
박상호 committed
93
94
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
95
                            <Form.Group as={Col} controlId="formBasicNumber">
박상호's avatar
0120    
박상호 committed
96
97
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="number">주민등록번호</Col>
박상호's avatar
q    
박상호 committed
98
                                    <Col xs={3}>
박상호's avatar
0120    
박상호 committed
99
100
101
102
                                        <Form.Control
                                            required type="text"
                                            name="number1"
                                            maxlength="6"
박상호's avatar
q    
박상호 committed
103
                                            className="ml-1 mr-3 p-1" style={{ width: '80px' }}
박상호's avatar
0120    
박상호 committed
104
105
106
                                            value={user.number1}
                                            onChange={handleChange}>
                                        </Form.Control>
박상호's avatar
q    
박상호 committed
107
108
109
110
111
                                    </Col>
                                    <Col xs={1}>
                                        <div className='font-weight-bold d-flex align-items-center' style={{ text: 'center' }}>-</div>
                                    </Col>
                                    <Col xs={2}>
박상호's avatar
0120    
박상호 committed
112
113
114
115
116
117
118
119
                                        <Form.Control
                                            required type="text"
                                            name="number2"
                                            maxlength="1" className="mx-3 p-1"
                                            style={{ width: '30px' }}
                                            value={user.number2}
                                            onChange={handleChange}>
                                        </Form.Control>
박상호's avatar
q    
박상호 committed
120
121
122
                                    </Col>
                                        <div className='font-weight-bold d-flex align-items-center'>* * * * * * </div>
                                    <Form.Control.Feedback type="invalid">주민등록번호를 입력하세요.</Form.Control.Feedback>
박상호's avatar
0120    
박상호 committed
123
124
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
125
                            <Form.Group as={Col} controlId="formBasicId">
박상호's avatar
0120    
박상호 committed
126
127
128
129
130
131
132
133
134
135
136
137
138
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="id">아이디</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        required
                                        type="text"
                                        name="id"
                                        placeholder="ID"
                                        style={{ width: '160px' }}
                                        value={user.id}
                                        onChange={handleChange} />
                                    <Form.Control.Feedback type="invalid"> 아이디를 입력하세요.</Form.Control.Feedback>
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
139
                            <Form.Group as={Col} controlId="formBasicPassword">
박상호's avatar
0120    
박상호 committed
140
141
142
143
144
145
146
147
148
149
150
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="password">비밀번호</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        type="password"
                                        name="password"
                                        placeholder="Password"
                                        style={{ width: '160px' }}
                                        value={user.password}
                                        required
                                        onChange={handleChange}
                                    />
박상호's avatar
q    
박상호 committed
151
                                    <Form.Control.Feedback className="text-end" type="invalid">
박상호's avatar
0120    
박상호 committed
152
                                        비밀번호를 입력하세요.
이재연's avatar
이재연 committed
153
                                    </Form.Control.Feedback>
박상호's avatar
0120    
박상호 committed
154
155
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
156
                            <Form.Group as={Col} controlId="formBasicPassword2">
박상호's avatar
0120    
박상호 committed
157
158
159
160
161
162
163
164
165
166
167
168
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="password">비밀번호 확인</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        type="password"
                                        name="password2"
                                        placeholder="Password"
                                        style={{ width: '160px' }}
                                        value={user.password2}
                                        required
                                        onChange={handleChange}
                                    />
                                    <Form.Control.Feedback type="invalid"> 비밀번호를 한번  입력하세요.
이재연's avatar
이재연 committed
169
                                    </Form.Control.Feedback>
박상호's avatar
0120    
박상호 committed
170
171
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
172
                            <Form.Group as={Col} controlId="formBasicEmail">
박상호's avatar
0120    
박상호 committed
173
174
175
176
177
178
179
180
181
182
183
184
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="email">이메일</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        required type="email"
                                        name="email"
                                        placeholder="E-mail"
                                        style={{ width: '160px' }}
                                        value={user.email}
                                        onChange={handleChange} />
                                    <Form.Control.Feedback type="invalid"> 이메일 입력하세요. </Form.Control.Feedback>
                                </Form.Row>
                            </Form.Group>
박상호's avatar
q    
박상호 committed
185
                            <Form.Group as={Col} controlId="formBasicTel">
박상호's avatar
0120    
박상호 committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                                <Form.Row>
                                    <Col sm={4} xs={6} as={Form.Label} for="tel">휴대전화</Col>
                                    <Col sm={8} xs={12} style={{ width: '160px' }} className='p-0'>
                                        <Col
                                            as={Form.Control}
                                            required type="text"
                                            name="tel"
                                            placeholder="Telephone"
                                            className='p-1'
                                            value={user.tel}
                                            onChange={handleChange}>

                                        </Col>
                                        <div className='d-flex justify-content-end mt-1'><small >' - '  함께 입력해주세요^^</small></div>
                                    </Col>
                                    <Form.Control.Feedback type="invalid"> 휴대전화를 입력하세요. </Form.Control.Feedback>
                                </Form.Row>
박상호's avatar
q    
박상호 committed
203
204
205
206
                                <Form.Control.Feedback className="text-end" type="invalid">
                                    비밀번호를 입력하세요.
                                    </Form.Control.Feedback>
                            </Form.Group >
박상호's avatar
0120    
박상호 committed
207
208
209
210
211
                            <Button
                                style={{ background: '#91877F', borderColor: '#91877F' }} type="submit" block
                                onClick={checkPassword}
                            >
                                Sign Up
이재연's avatar
이재연 committed
212
                            </Button>
박상호's avatar
0120    
박상호 committed
213
214
215
216
                        </Form>
                    </Col>
                </Row>
            </Container>
박상호's avatar
q    
박상호 committed
217
        </div >
박상호's avatar
0120    
박상호 committed
218
    )
Kim, Subin's avatar
Kim, Subin committed
219
220
221
}

export default Signup