ApplyPage.js 11.5 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
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'bootstrap/dist/css/bootstrap.css';
import * as Yup from 'yup';
import { Redirect } from 'react-router-dom';

function Apply(props) {
    const [state, setState] = useState();
    const [user, setUser] = useState({ name: "" });

    useEffect(() => {
        getUser();
    }, [])

    if (state) {
        return <Redirect to={{
            pathname: `/check/${props.match.params.id}`,
            state: { id: props.match.params.id },
        }} />;
    }

24
    function time(starttime) {
CHAERIN KIM's avatar
CHAERIN KIM committed
25
26
27
28
29
30
31
        if (starttime == 21) {
            return (<Field as="select" name="usetime">
                <option value="">이용시간을 선택하세요</option>
                <option value="1">1시간</option>
            </Field>)
        }
        if (starttime == 20) {
32
33
34
35
36
            return (<Field as="select" name="usetime">
                <option value="">이용시간을 선택하세요</option>
                <option value="1">1시간</option>
                <option value="2">2시간</option>
            </Field>)
CHAERIN KIM's avatar
CHAERIN KIM committed
37
38
39
40
41
42
43
44
        }
        return (<Field as="select" name="usetime">
            <option value="">이용시간을 선택하세요</option>
            <option value="1">1시간</option>
            <option value="2">2시간</option>
            <option value="3">3시간</option>
        </Field>)

45
46
    }

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

    return (
        <div>
            <Menu />
66
            <Formik
CHAERIN KIM's avatar
CHAERIN KIM committed
67
68
69
                initialValues={{
                    _id: `${props.match.params.id}`,
                    date: '',
70
71
                    starttime: '',
                    usetime: '',
CHAERIN KIM's avatar
CHAERIN KIM committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                    room: '',
                    reason: '',
                    students: [
                        {
                            member: '',
                        },
                    ],
                }}
                validationSchema={Yup.object({
                    date: Yup.string()
                        .required('날짜를 입력해주세요.'),
                    room: Yup.string()
                        .required('강의실 번호를 입력해주세요.'),
                    reason: Yup.string()
                        .required('대관목적을 입력해주세요.'),
                })}
                onSubmit={(values, { setSubmitting }) => {
                    console.log(values)
                    axios({
                        method: 'post',
                        url: '/reserves',
                        data: values
                    }).then(res => {
CHAERIN KIM's avatar
CHAERIN KIM committed
95
96
97
98
                        if (res.status === 404) {
                            alert(res.data.error)
                            return window.location.reload();
                        }
CHAERIN KIM's avatar
CHAERIN KIM committed
99
100
                        alert("신청이 완료되었습니다!");
                        setState(true);
CHAERIN KIM's avatar
CHAERIN KIM committed
101
                        console.log("res.data", res.data)
CHAERIN KIM's avatar
CHAERIN KIM committed
102
103
104
105
106
                    })
                        .catch(err => {
                            alert(err.error)
                        });

CHAERIN KIM's avatar
CHAERIN KIM committed
107
108
109
                    setTimeout(() => {
                        setSubmitting(false);
                    }, 400);  // finish the cycle in handler
CHAERIN KIM's avatar
CHAERIN KIM committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                }}
            >
                {({
                    errors,
                    touched,
                    values,
                    handleSubmit,
                    getFieldProps,
                    isSubmitting,
                }) => (
                        <div className="row justify-content-center align-items-center">
                            <form onSubmit={handleSubmit} className="col-sm-3">
                                <div className="form-group mb-4">
                                    <label>대표자</label>
                                    <div>{user.name}</div>
                                </div>
                                <div className="form-group mb-4">
                                    <label>신청날짜</label>
                                    <input
                                        className={(touched.date && errors.date ? 'form-control is-invalid' : "form-control")}
130
                                        type="text"
CHAERIN KIM's avatar
CHAERIN KIM committed
131
132
                                        name="date"
                                        {...getFieldProps('date')}
133
                                        placeholder="yyyy-mm-dd"
CHAERIN KIM's avatar
CHAERIN KIM committed
134
135
136
137
138
                                    />
                                    {touched.date && errors.date ? (
                                        <div className="invalid-feedback text-left">{errors.date}</div>
                                    ) : null}
                                </div>
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                                <div className="form-group mb-4">
                                    <label>이용시작시간</label>
                                    <Field as="select" name="starttime">
                                        <option value="">이용시작시간</option>
                                        <option value="9">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>
CHAERIN KIM's avatar
CHAERIN KIM committed
158
159
                                <div className="form-group mb-4">
                                    <label>이용시간</label>
160
                                    {time(values.starttime)}
CHAERIN KIM's avatar
CHAERIN KIM committed
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                                </div>
                                <div className="form-group mb-4">
                                    <label>강의실</label>
                                    <input
                                        className={(touched.room && errors.room ? 'form-control is-invalid' : "form-control")}
                                        type="text"
                                        name="room"
                                        {...getFieldProps('room')}
                                        placeholder="bn-nnn"
                                    />
                                    {touched.room && errors.room ? (
                                        <div className="invalid-feedback text-left">{errors.room}</div>
                                    ) : null}
                                </div>
                                <div className="form-group mb-4">
                                    <label>대관목적</label>
                                    <input
                                        className={(touched.reason && errors.reason ? 'form-control is-invalid' : "form-control")}
                                        type="text"
                                        name="reason"
                                        {...getFieldProps('reason')}
                                        placeholder="대관목적을 입력해 주세요."
                                    />
                                    {touched.reason && errors.reason ? (
                                        <div className="invalid-feedback text-left">{errors.reason}</div>
                                    ) : null}
                                </div>
                                <div className="form-group mb-4">
                                    <FieldArray name="students">
                                        {({ insert, remove, push }) => (
                                            <div>
                                                <label>이용자</label>
                                                {
                                                    values.students.map((student, index) => (
                                                        <div className="row" key={index}>
                                                            <div className="col">
                                                                <Field
                                                                    name={`students.${index}.member`}
                                                                    placeholder="이용자 성함을 입력하세요."
                                                                    type="text"
                                                                />
                                                                <ErrorMessage
                                                                    name={`friends.${index}.name`}
                                                                    component="div"
                                                                    className="field-error"
                                                                />
                                                                <button
                                                                    type="button"
                                                                    className="secondary"
                                                                    onClick={() => remove(index)}
                                                                >
                                                                    X
                                                                        </button>

                                                            </div>
                                                        </div>
                                                    ))}
                                                <button
                                                    type="button"
                                                    className="btn btn-primary"
                                                    onClick={() => push({ member: '' })}
                                                >
                                                    추가
                                                </button>
                                            </div>
                                        )}
                                    </FieldArray>
                                </div>
                                <button type="submit" className="btn btn-dark" disabled={isSubmitting}>
                                    신청하기
231
                                </button>
CHAERIN KIM's avatar
CHAERIN KIM committed
232
233
234
235
                            </form>
                        </div>
                    )}
            </Formik>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
236
237
238
239
240
        </div>
    )
}

export default Apply