ApplyPage.js 12.9 KB
Newer Older
Ha YeaJin's avatar
pages    
Ha YeaJin committed
1
import React, { useState, useEffect } from 'react';
CHAERIN KIM's avatar
CHAERIN KIM committed
2
import { Formik, Field, ErrorMessage, FieldArray } from 'formik';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
3
import Menu from '../Components/Menu';
4
import axios from 'axios';
CHAERIN KIM's avatar
CHAERIN KIM committed
5
6
import * as Yup from 'yup';
import { Redirect } from 'react-router-dom';
Kim, Subin's avatar
Kim, Subin committed
7
import { Col, Container, Row } from 'react-bootstrap';
CHAERIN KIM's avatar
CHAERIN KIM committed
8
9

function Apply(props) {
CHAERIN KIM's avatar
CHAERIN KIM committed
10
    const [state, setState] = useState({ok:""});
CHAERIN KIM's avatar
CHAERIN KIM committed
11
    const [user, setUser] = useState({ name: "" });
Kim, Subin's avatar
Kim, Subin committed
12
    const [room_Num, setRoom_Num] = useState({ "9-116": 5, "7-234": 7, "25-101": 10 });
CHAERIN KIM's avatar
CHAERIN KIM committed
13
14
15
16

    useEffect(() => {
        getUser();
    }, [])
17
    
CHAERIN KIM's avatar
CHAERIN KIM committed
18
19
    if (state.ok==="no") return <Redirect to="/" />;
    if (state.ok==="ok") {
CHAERIN KIM's avatar
CHAERIN KIM committed
20
21
22
23
24
25
        return <Redirect to={{
            pathname: `/check/${props.match.params.id}`,
            state: { id: props.match.params.id },
        }} />;
    }

26
    function time(starttime) {
Ha YeaJin's avatar
Ha YeaJin committed
27
        if (starttime == 21) {
Kim, Subin's avatar
Kim, Subin committed
28
29
            return (<Field as="select" name="usetime" className="col-12">
                <option value="">이용시간선택</option>
Ha YeaJin's avatar
Ha YeaJin committed
30
31
32
33
                <option value="1">1시간</option>
            </Field>)
        }
        if (starttime == 20) {
Kim, Subin's avatar
Kim, Subin committed
34
35
            return (<Field as="select" name="usetime" className="col-12">
                <option value="">이용시간선택</option>
36
37
38
                <option value="1">1시간</option>
                <option value="2">2시간</option>
            </Field>)
Ha YeaJin's avatar
Ha YeaJin committed
39
        }
Kim, Subin's avatar
Kim, Subin committed
40
41
        return (<Field as="select" name="usetime" className="col-12">
            <option value="">이용시간선택</option>
Ha YeaJin's avatar
Ha YeaJin committed
42
43
44
45
            <option value="1">1시간</option>
            <option value="2">2시간</option>
            <option value="3">3시간</option>
        </Field>)
46
47
    }

CHAERIN KIM's avatar
CHAERIN KIM committed
48
49
50
51
52
53
54
    function getUser() {
        axios.get(`/users/${props.match.params.id}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
                if (res.status !== 201) {
                    alert(res.data.error);
CHAERIN KIM's avatar
CHAERIN KIM committed
55
56
                    localStorage.clear();
                    setState({ok:"no"});
CHAERIN KIM's avatar
CHAERIN KIM committed
57
58
59
60
61
62
63
64
                }
                console.log(res.data);
                setUser(res.data);
            })
            .catch(err => {
                alert(err.error)
            });
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
65

Kim, Subin's avatar
Kim, Subin committed
66
67
68
69
70
71
72
73
    function addRoomInfo(values) {
        for (let room in room_Num) {
            if (room === values.room) {
                values.roomInfo = room_Num[room]
            }
        };
    }

Ha YeaJin's avatar
pages    
Ha YeaJin committed
74
75
76
    return (
        <div>
            <Menu />
Kim, Subin's avatar
Kim, Subin committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
            <Container fluid className="mt-3">
                <Row className="justify-content-center">
                    <Col md={5}>
                        <Formik
                            initialValues={{
                                _id: `${props.match.params.id}`,
                                date: '',
                                starttime: '',
                                usetime: '',
                                room: '',
                                reason: '',
                                students: [
                                    {
                                        member: '',
                                    },
                                ],
                            }}
                            validationSchema={Yup.object({
                                date: Yup.string()
                                    .required('날짜를 입력해주세요.'),
                                reason: Yup.string()
                                    .required('대관목적을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
Kim, Subin's avatar
Kim, Subin committed
101
                                addRoomInfo(values);
Kim, Subin's avatar
Kim, Subin committed
102
103
104
105
106
107
108
109
110
111
                                axios({
                                    method: 'post',
                                    url: '/reserves',
                                    data: values
                                }).then(res => {
                                    if (res.status === 404) {
                                        alert(res.data.error)
                                        return window.location.reload();
                                    }
                                    alert("신청이 완료되었습니다!");
CHAERIN KIM's avatar
CHAERIN KIM committed
112
                                    setState({ok:"ok"});
Kim, Subin's avatar
Kim, Subin committed
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
                                    console.log("res.data", res.data)
                                })
                                    .catch(err => {
                                        alert(err.error)
                                    });

                                setTimeout(() => {
                                    setSubmitting(false);
                                }, 400);  // finish the cycle in handler
                            }}
                        >
                            {({
                                errors,
                                touched,
                                values,
                                handleSubmit,
                                getFieldProps,
                                isSubmitting,
                            }) => (
                                    <form onSubmit={handleSubmit} className="d-flex flex-column">
                                        <h3 className="form-group font-weight-bold">
                                            <label className="pr-2">대표자 :</label>{user.name}
                                        </h3>

                                        <div className="form-group mb-4">
                                            <div className={touched.date && errors.date ? "text-danger" : ""}>신청날짜</div>
                                            <input
                                                className={(touched.date && errors.date ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="date"
                                                {...getFieldProps('date')}
                                                placeholder="yyyy-mm-dd"
                                            />
                                        </div>
CHAERIN KIM's avatar
CHAERIN KIM committed
147

Kim, Subin's avatar
Kim, Subin committed
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
                                        <Row className="form-group mb-4">
                                            <div className="col-6 pr-0">
                                                <label>이용시작시간</label>
                                                <div>
                                                    <Field as="select" name="starttime" className="col-12">
                                                        <option value="">이용시작시간</option>
                                                        <option value="09">9</option>
                                                        <option value="10">10</option>
                                                        <option value="11">11</option>
                                                        <option value="12">12</option>
                                                        <option value="13">13</option>
                                                        <option value="14">14</option>
                                                        <option value="15">15</option>
                                                        <option value="16">16</option>
                                                        <option value="17">17</option>
                                                        <option value="18">18</option>
                                                        <option value="19">19</option>
                                                        <option value="20">20</option>
                                                        <option value="21">21</option>
                                                    </Field>
                                                </div>
                                            </div>
                                            <div className="col-6 pl-0">
                                                <label>이용시간</label>
                                                <div>
                                                    {time(values.starttime)}
                                                </div>
                                            </div>
                                        </Row>

                                        <div className="form-group mb-4">
                                            <div className={touched.room && errors.room ? "text-danger" : ""}>강의실</div>
                                            <Field as="select" name="room" className="col-6">
                                                <option value="">강의실 선택</option>
                                                <option value="9-116">9-116</option>
                                                <option value="7-234">7-234</option>
                                                <option value="25-101">25-101</option>
                                            </Field>
                                        </div>

                                        <div className="form-group mb-4">
                                            <div className={touched.reason && errors.reason ? "text-danger" : ""}>대관 목적</div>
                                            <input
                                                className={(touched.reason && errors.reason ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="reason"
                                                {...getFieldProps('reason')}
                                                placeholder="대관목적을 입력해 주세요."
                                            />
                                        </div>

                                        <div className="form-group mb-4">
                                            <FieldArray name="students">
                                                {({ insert, remove, push }) => (
                                                    <div>
                                                        <div className={touched.date && errors.date ? "text-danger" : ""}>이용자</div>
                                                        {values.students.map((student, index) => (
                                                            <div key={index}>
CHAERIN KIM's avatar
CHAERIN KIM committed
206
207
                                                                <Field
                                                                    name={`students.${index}.member`}
Kim, Subin's avatar
Kim, Subin committed
208
                                                                    placeholder="이용자 이름 입력"
CHAERIN KIM's avatar
CHAERIN KIM committed
209
                                                                    type="text"
Kim, Subin's avatar
Kim, Subin committed
210
                                                                    className="col-6 mr-1"
CHAERIN KIM's avatar
CHAERIN KIM committed
211
212
213
214
215
216
217
218
219
220
                                                                />
                                                                <ErrorMessage
                                                                    name={`friends.${index}.name`}
                                                                    component="div"
                                                                    className="field-error"
                                                                />
                                                                <button
                                                                    type="button"
                                                                    className="secondary"
                                                                    onClick={() => remove(index)}
Kim, Subin's avatar
Kim, Subin committed
221
                                                                >X</button>
CHAERIN KIM's avatar
CHAERIN KIM committed
222
                                                            </div>
Kim, Subin's avatar
Kim, Subin committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                                                        ))}
                                                        <button
                                                            type="button"
                                                            className="btn btn-primary"
                                                            onClick={() => push({ member: '' })}
                                                        >추가</button>
                                                    </div>
                                                )}
                                            </FieldArray>
                                        </div>
                                        <button type="submit" className="btn btn-dark" disabled={isSubmitting}>
                                            신청하기
                                        </button>
                                    </form>
                                )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
242
243
244
245
        </div>
    )
}

Kim, Subin's avatar
Kim, Subin committed
246
export default Apply