RegisterContainer.tsx
11.4 KB
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
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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import React, { useState, useEffect } from "react";
import { RouteComponentProps } from 'react-router-dom';
import { useRecoilValue, useRecoilState } from "recoil";
import * as recoilUtil from '../../util/recoilUtil';
import validator from 'validator';
import * as Alert from '../../util/alertMessage';
import Header from '../../components/Header';
import RegisterPresenter from "./RegisterPresenter";
import { authApi } from '../../api';
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface RegisterProps extends RouteComponentProps {}
const RegisterContainer = (props : RegisterProps) => {
const token = useRecoilValue(recoilUtil.token);
const [loading, setLoading] = useRecoilState(recoilUtil.loading);
const [registerForm, setRegisterForm] = useState<any>({
userId : '',
password : '',
passwordCheck : '',
info : {
doctorLicense : File,
hospitalNm : '',
hospitalAddr : '',
contact : '',
doctorType : '',
doctorNm : '',
},
});
const [page, setPage] = useState<number>(1);
const [error, setError] = useState<string | null>(null);
const [searchHospital, setSearchHospital] = useState<boolean>(false);
const [hospitalNm, setHospitalNm] = useState<string>('');
const [hospitalSearchPage, setHospitalSearchPage] = useState<number>(1);
const [hospitalSearchPageList, setHospitalSearchPageList] = useState<number[]>([1]);
const [hospitalList, setHospitalList] = useState<any[]>([]);
const [selectHospital, setSelectHospital] = useState<any>(null);
const fetchData = async() => {
if(token && token.length) {
const result = await authApi.verifyToken(token);
if (result.statusText === 'OK') {
props.history.push('/');
}
}
};
const onCancleRegister = () => {
Alert.onCheck('회원가입을 취소하시겠습니까?',
() => props.history.push('/login'),
() => null);
};
const onGoBackPage = () => {
if(page > 1) {
setPage(page - 1);
}
};
const validateRegisterForm = () => {
if(page === 1) {
if (!validator.isEmail(registerForm.userId)) {
setError('회원 가입 ID는 이메일이어야 합니다.');
} else if(registerForm.password === registerForm.password.toLowerCase()
|| !/\d/.test(registerForm.password)
) {
setError('비밀번호는 최소 8자 이상이어야 하고, 대문자 및 숫자를 1개 이상 포함하고 있어야 합니다.');
} else if(registerForm.password !== registerForm.passwordCheck) {
setError('비밀번호가 일치하지 않습니다.')
} else setError(null);
} else if(page === 2) {
if(!registerForm.info.doctorLicense.length &&
!validator.isAlphanumeric(registerForm.info.doctorLicense)) {
setError('의사 자격 번호를 입력해야 합니다.');
} else if(registerForm.info.doctorNm.length < 2) {
setError('의사 이름을 올바르게 입력해야 합니다.');
} else if(!registerForm.info.contact) {
setError('연락처를 입력해주세요.');
} else setError(null);
} else if(page === 3) {
if(!registerForm.info.doctorType.length) {
setError('전문 분야를 입력해주세요.');
} else if(!registerForm.info.hospitalNm || !registerForm.info.hospitalAddr) {
setError('올바른 병원 정보를 입력해주세요');
} else setError(null);
}
};
const onSetUserId = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
userId : e.target.value,
});
};
const onSetPassword = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
password : e.target.value,
});
};
const onSetPasswordCheck = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
passwordCheck : e.target.value,
});
};
const onSetDoctorLicense = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
doctorLicense : e.target.value,
},
});
};
const onSetHospitalNm = (e : React.ChangeEvent<HTMLInputElement>) => {
setHospitalNm(e.target.value);
};
const onSetContact = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
contact : e.target.value,
},
});
};
const onSetDoctorType = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
doctorType : e.target.value,
},
});
};
const onSetDoctorNm = (e : React.ChangeEvent<HTMLInputElement>) => {
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
doctorNm : e.target.value,
},
});
};
const onSearchHospital = async () => {
try {
setLoading(true);
setSearchHospital(true);
const result = await authApi.searchHospital(hospitalNm, hospitalSearchPage);
if(result.statusText === 'OK') {
setLoading(false);
setHospitalSearchPageList(new Array(result.data.totalPage).fill(null).map((item : null, index : number) => index + 1));
setHospitalList(result.data.hospitalList.length ? result.data.hospitalList : [result.data.hospitalList]);
}
} catch(e : any) {
setLoading(false);
Alert.onError('알 수 없는 에러로 검색에 실패했습니다.', () => null);
}
};
const onSetSearchPrevPage = () => {
//set Prev Page
const pageSlice = 5;
if(hospitalSearchPage > pageSlice) {
setHospitalSearchPage(Math.floor((hospitalSearchPage - 1) / pageSlice) * pageSlice);
}
};
const onSetSearchNextPage = () => {
//set Next Page
const pageSlice = 5;
if(hospitalSearchPage <= Math.floor((hospitalSearchPageList.length - 1) / pageSlice) * pageSlice) {
setHospitalSearchPage(Math.ceil(hospitalSearchPage / pageSlice) * pageSlice + 1);
}
};
const onConfirmSelectHospital = () => {
setSearchHospital(false);
setHospitalSearchPage(1);
setHospitalSearchPageList([1]);
setHospitalList([]);
};
const onCancelSelectHospital = () => {
Alert.onCheck('병원 등록이 취소됩니다. 계속하시겠습니까?', () => {
setSearchHospital(false);
setHospitalNm('');
setHospitalSearchPage(1);
setHospitalSearchPageList([1]);
setHospitalList([]);
setSelectHospital(null);
}, () => null);
};
const onSubmitButton = () => {
if(error) {
Alert.onError(error, () => null);
return;
}
if(page === 1) {
setPage(2);
} else if(page === 2) {
setPage(3);
} else if(page === 3) {
const Data = new FormData();
Data.append('userId', registerForm.userId);
Data.append('password', registerForm.password);
Data.append('passwordCheck', registerForm.passwordCheck);
Data.append('hospitalNm', registerForm.info.hospitalNm);
Data.append('hospitalAddr', registerForm.info.hospitalAddr);
Data.append('contact', registerForm.info.contact);
Data.append('doctorNm', registerForm.info.doctorNm);
Data.append('doctorType', registerForm.info.doctorType);
Data.append('doctorInfoFile', registerForm.info.doctorLicense[0]);
const onRegisterDoctor = async () => {
try {
const result = await authApi.registerDoctor(Data);
if(result.data === 'Created') {
Alert.onSuccess('회원가입 성공, 관리자의 승인을 대기하세요.', () => props.history.push('/login'));
}
} catch(e : any) {
Alert.onError(e.response.data.error, () => null);
}
};
if(selectHospital) {
Alert.onCheck('입력하신 정보로 회원가입을 진행하시겠습니까?', onRegisterDoctor, () => null);
} else {
Alert.onError('검색 버튼을 눌러 병원을 선택해주세요.', () => null);
}
}
};
useEffect(() => {
validateRegisterForm();
}, [registerForm, page]);
useEffect(() => {
if(selectHospital) {
setHospitalNm(selectHospital.yadmNm);
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
hospitalNm : selectHospital.yadmNm,
hospitalAddr : selectHospital.addr,
},
});
} else {
setHospitalNm('');
setRegisterForm({
...registerForm,
info : {
...registerForm.info,
hospitalNm : '',
hospitalAddr : '',
},
});
}
}, [selectHospital]);
useEffect(() => {
if(searchHospital) onSearchHospital();
}, [hospitalSearchPage]);
useEffect(() => {
fetchData();
validateRegisterForm();
}, []);
return (
<>
<Header {...props}/>
<RegisterPresenter
registerForm = {registerForm}
page = {page}
error = {error}
onGoBackPage = {onGoBackPage}
onCancleRegister = {onCancleRegister}
onSetUserId = {onSetUserId}
onSetPassword = {onSetPassword}
onSetPasswordCheck = {onSetPasswordCheck}
onSetDoctorLicense = {onSetDoctorLicense}
hospitalNm = {hospitalNm}
onSetHospitalNm = {onSetHospitalNm}
onSetContact = {onSetContact}
onSetDoctorType = {onSetDoctorType}
onSetDoctorNm = {onSetDoctorNm}
onSubmitButton = {onSubmitButton}
searchHospital = {searchHospital}
setSearchHospital = {setSearchHospital}
onSearchHospital = {onSearchHospital}
hospitalSearchPage = {hospitalSearchPage}
setHospitalSearchPage = {setHospitalSearchPage}
hospitalSearchPageList = {hospitalSearchPageList}
onSetSearchPrevPage = {onSetSearchPrevPage}
onSetSearchNextPage = {onSetSearchNextPage}
onConfirmSelectHospital = {onConfirmSelectHospital}
onCancelSelectHospital = {onCancelSelectHospital}
hospitalList = {hospitalList}
selectHospital = {selectHospital}
setSelectHospital = {setSelectHospital}
/>
</>
)
};
export default RegisterContainer;