MyInfo.js 16.6 KB
Newer Older
한규민's avatar
한규민 committed
1
2
import { useState, useEffect } from "react";
import authApi from "../../apis/auth.api";
한규민's avatar
한규민 committed
3
import catchErrors from "../../utils/catchErrors.js";
Kim, Subin's avatar
Kim, Subin committed
4
import styles from "./my-info.module.scss";
한규민's avatar
한규민 committed
5
6

const MyInfo = () => {
한규민's avatar
한규민 committed
7
    const [userNickName, setUserNickName] = useState("사용자");
한규민's avatar
한규민 committed
8
9
    const [img, setImg] = useState("");
    const [profile, setProfile] = useState("");
10
    const [startTime, setStartTime] = useState("");
한규민's avatar
한규민 committed
11
    const [page, setPage] = useState(true);
한규민's avatar
한규민 committed
12
13
    const [presentPw, setPresentPw] = useState("");
    const [loading, setLoading] = useState(false);
한규민's avatar
한규민 committed
14
    const [userRe, setUserRe] = useState({
15
        userName: "",
한규민's avatar
한규민 committed
16
17
18
19
20
21
        userEmail: "",
        userNickName: "",
        userMbnum: "",
        userPassword: "",
        userRePassword: ""
    })
22
23
    const [confirmMb, setConfirmMb] = useState(false);
    const [number, setNumber] = useState(null);
Kim, Subin's avatar
Kim, Subin committed
24
    const [mbError, setMbError] = useState(false);
한규민's avatar
한규민 committed
25
26
    const [error, setError] = useState("");
    const [errorMsg, setErrorMsg] = useState({
27
        errorName: false,
한규민's avatar
한규민 committed
28
29
30
31
32
33
        errorEmail: false,
        errorNickName: false,
        errorMbnum: false,
        errorPassword: false,
    })

한규민's avatar
한규민 committed
34
    useEffect(() => {
한규민's avatar
한규민 committed
35
        getMember();
한규민's avatar
한규민 committed
36
37
    }, [])

Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
    const getMember = async () => {
        const member = await authApi.getMember();
        setUserNickName(member.nickname);
        setProfile(member.img);
    }

한규민's avatar
한규민 committed
44
45
46
    const handlePwOnChange = (e) => {
        setPresentPw(e.target.value)
    }
한규민's avatar
한규민 committed
47
48
49
50
51
52
53
54
55
56
57
58
59

    const handleUserOnChange = (e) => {
        setUserRe({
            ...userRe,
            [e.target.name]: e.target.value
        })
        if (e.target.name === "userMbnum") {
            setUserRe({
                ...userRe,
                [e.target.name]: String(e.target.value)
            })
        }
    }
한규민's avatar
한규민 committed
60
61
62
63
64
    const enterKey = (e) => {
        if (e.key === "Enter") {
            handleOnSummitVerify(e);
        }
    }
한규민's avatar
한규민 committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    const handleOnChange = (e) => {
        setImg(e.target.files[0]);
    }

    const handleOnSummitForm = async (e) => {
        e.preventDefault();
        try {
            setError("")
            const formData = new FormData();
            formData.append("image", img);
            const image = await authApi.profile(formData);
            setProfile(image.img);
        } catch (error) {
            catchErrors(error, setError);
        }
    }
한규민's avatar
한규민 committed
82
83
84
    const handleOnSummitVerify = async (e) => {
        e.preventDefault();
        try {
한규민's avatar
한규민 committed
85
86
            setError("");
            setLoading(true);
한규민's avatar
한규민 committed
87
88
89
            const pw = presentPw;
            const confirmPw = await authApi.comparePw(pw);
            if (confirmPw) {
한규민's avatar
한규민 committed
90
                setPage(false);
한규민's avatar
한규민 committed
91
                setPresentPw("");
한규민's avatar
한규민 committed
92
93
94
95
96
97
98
99
100
            } else {
                alert("비밀번호가 일치하지 않습니다.");
            }
        } catch (error) {
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }
한규민's avatar
한규민 committed
101

102
103
104
105
106
107
108
109
    const handleOnClickMbnum = async (e) => {
        e.preventDefault();
        try {
            setStartTime("");
            setError("");
            setLoading(true);
            const phone = userRe.userMbnum;
            const message = await authApi.confirmMbnum(phone);
Kim, Subin's avatar
Kim, Subin committed
110
111
112
            if (message.isSuccess) {
                setMbError("보냄");
                setStartTime(message.startTime);
113
114
            }
        } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
115
116
            catchErrors(error, setError);
        } finally {
117
118
119
120
121
122
123
124
125
126
127
128
129
            setLoading(false);
        }
    }

    const handleOnChangeMb = (e) => {
        setNumber(String(e.target.value));
    }

    const handleOnClickMbConfirm = async (e) => {
        e.preventDefault();
        try {
            setError("");
            setLoading(true);
Kim, Subin's avatar
Kim, Subin committed
130
            const confirmNum = { userMbnum: userRe.userMbnum, number: number, startTime: startTime };
131
132
            const message = await authApi.confirmNum(confirmNum);
            setMbError(message);
Kim, Subin's avatar
Kim, Subin committed
133
            if (message === "성공") {
134
135
136
137
138
                setConfirmMb(true);
                console.log("인증완료");
            }
        } catch (error) {
            catchErrors(error, setError);
Kim, Subin's avatar
Kim, Subin committed
139
        } finally {
140
141
142
143
            setLoading(false);
        }
    }

한규민's avatar
한규민 committed
144
    const validationPw = () => {
Kim, Subin's avatar
Kim, Subin committed
145
146
        if (userRe.userPassword !== userRe.userRePassword) return false;
        else return true;
한규민's avatar
한규민 committed
147
    }
Kim, Subin's avatar
Kim, Subin committed
148

한규민's avatar
한규민 committed
149
150
151
    const handleOnSummit = async (e) => {
        e.preventDefault();
        try {
한규민's avatar
한규민 committed
152
153
            setError("");
            setLoading(true);
한규민's avatar
한규민 committed
154
            let validPw = validationPw();
Kim, Subin's avatar
Kim, Subin committed
155
156
            if (confirmMb) {
                if (validPw) {
한규민's avatar
한규민 committed
157
158
                    const userData = userRe;
                    const error = await authApi.modifyUser(userData);
Kim, Subin's avatar
Kim, Subin committed
159
                    if (error === "성공") {
한규민's avatar
한규민 committed
160
                        alert("회원정보수정에 성공하였습니다.")
Kim, Subin's avatar
Kim, Subin committed
161
                    } else {
한규민's avatar
한규민 committed
162
163
164
                        setErrorMsg(error);
                        alert("형식에 맞게 다시 작성해주세요");
                    }
Kim, Subin's avatar
Kim, Subin committed
165
                } else {
한규민's avatar
한규민 committed
166
167
                    throw new Error("비밀번호가 일치하지 않습니다.");
                }
Kim, Subin's avatar
Kim, Subin committed
168
            } else {
한규민's avatar
한규민 committed
169
                throw new Error("핸드폰 번호를 인증해주세요.");
한규민's avatar
한규민 committed
170
            }
한규민's avatar
한규민 committed
171
172
173
        } catch (error) {
            catchErrors(error, setError);
        } finally {
한규민's avatar
한규민 committed
174
            setLoading(false);
한규민's avatar
한규민 committed
175
176
        }
    }
Kim, Subin's avatar
Kim, Subin committed
177

한규민's avatar
한규민 committed
178
179
180
    const handleOnclickOut = (e) => {
        setConfirmMb(false);
    }
Kim, Subin's avatar
Kim, Subin committed
181

한규민's avatar
한규민 committed
182
183
    const handleOnClick = (e) => {
        e.preventDefault();
한규민's avatar
한규민 committed
184
        handleOnclickOut(e);
한규민's avatar
한규민 committed
185
186
        setPage(true);
    }
한규민's avatar
한규민 committed
187
188

    return (
한규민's avatar
한규민 committed
189
        <>
한규민's avatar
한규민 committed
190
            <div className={`${styles.main}`}>
한규민's avatar
한규민 committed
191
                <span className={styles.title}>마이페이지</span>
한규민's avatar
한규민 committed
192
193
                <div className={`d-flex justify-content-around`}>
                    <div className={`${styles.box}`}>
한규민's avatar
한규민 committed
194
                        <p className={`${styles.hoverTxt}`}>프로필 변경</p>
한규민's avatar
한규민 committed
195
                        <img src={`/upload/${profile}`} className={`figure-img img-fluid rounded-circle ${styles.img} ${styles.profile}`} role="button" data-bs-toggle="modal" data-bs-target="#staticBackdrop" />
한규민's avatar
한규민 committed
196
                    </div>
한규민's avatar
한규민 committed
197
                    <div className="d-flex flex-column py-2 mx-3 justify-content-around">
한규민's avatar
한규민 committed
198
                        <span className={`${styles.userName}`}>{`${userNickName}`} 반갑습니다!</span>
한규민's avatar
한규민 committed
199
                        <button className={`rounded my-2 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} data-bs-toggle="modal" href="#verifyPassword" >회원정보 수정</button>
한규민's avatar
한규민 committed
200
                    </div>
한규민's avatar
한규민 committed
201
202
                </div>
            </div>
203
            <div className="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
한규민's avatar
한규민 committed
204
205
206
207
208
                <div className="modal-dialog modal-dialog-centered">
                    <form className="modal-content" onSubmit={handleOnSummitForm}>
                        <div className="modal-header">
                            <h5 className="modal-title" id="staticBackdropLabel">프로필변경</h5>
                            <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
한규민's avatar
한규민 committed
209
                        </div>
한규민's avatar
한규민 committed
210
211
212
213
214
215
216
217
218
                        <div className="modal-body">
                            <input type="file" onChange={handleOnChange} />
                        </div>
                        <div className="modal-footer">
                            <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                            <button type="submit" className={`rounded  fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`}>변경</button>
                        </div>
                    </form>
                </div>
한규민's avatar
한규민 committed
219
            </div>
220
            <div className="modal fade" id="verifyPassword" data-bs-backdrop="static" data-bs-keyboard="false" aria-hidden="true" aria-labelledby="verifyPasswordLabel" tabIndex="-1">
한규민's avatar
한규민 committed
221
222
                <div className="modal-dialog modal-dialog-centered modal-dialog-centered">
                    {page ?
Kim, Subin's avatar
Kim, Subin committed
223
                        <form className="modal-content" onSubmit={handleOnSummitVerify}>
한규민's avatar
한규민 committed
224
225
                            <div className="modal-header">
                                <h5 className="modal-title" id="verifyPasswordLabel">기존 비밀번호 확인</h5>
226
                                <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close" onClick={handleOnClick}></button>
한규민's avatar
한규민 committed
227
                            </div>
228
                            <div className="modal-body">
한규민's avatar
한규민 committed
229
230
231
                                <div className="d-flex flex-column">
                                    <div className="d-flex justify-content-around align-items-center my-4">
                                        <label className={styles.signupLabel}>현재 비밀번호</label>
Kim, Subin's avatar
Kim, Subin committed
232
                                        <input className={`${styles.input} ${styles.inputSize}`} type="password" name="userPassword" placeholder="8~11자리 사이" onChange={handlePwOnChange} onKeyPress={enterKey} maxLength="11" />
한규민's avatar
한규민 committed
233
234
                                    </div>
                                </div>
한규민's avatar
한규민 committed
235
                            </div>
한규민's avatar
한규민 committed
236
237
238
                            <div className="modal-footer">
                                <button type="submit" className={`rounded my-3 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} disabled={loading}>확인</button>
                            </div>
Kim, Subin's avatar
Kim, Subin committed
239
240
                        </form>
                        : <form className={`modal-content d-flex col-md-6 col-12 justify-content-center d-flex flex-column`} onSubmit={handleOnSummit}>
한규민's avatar
한규민 committed
241
242
                            <div className="modal-header">
                                <h5 className="modal-title" id="modifyLabel">회원정보 수정</h5>
243
                                <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close" onClick={handleOnClick}></button>
한규민's avatar
한규민 committed
244
245
                            </div>
                            <div className={`modal-body`}>
Kim, Subin's avatar
Kim, Subin committed
246
                                <div className="d-flex flex-column">
247
248
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}>이름</label>
Kim, Subin's avatar
Kim, Subin committed
249
                                        <input className={`${styles.input} ${styles.inputSize}`} type="text" name="userName" placeholder="이름을 입력해주세요" onChange={handleUserOnChange} maxLength="11" />
250
251
252
                                    </div>
                                    {errorMsg.errorName && <p className={styles.passwordConfirmError}>이름을 입력해주세요</p>}
                                </div>
한규민's avatar
한규민 committed
253
254
255
                                <div className="d-flex flex-column">
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}>이메일</label>
Kim, Subin's avatar
Kim, Subin committed
256
                                        <input className={`${styles.input} ${styles.inputSize}`} type="email" name="userEmail" placeholder="이메일을 입력해주세요" onChange={handleUserOnChange} maxLength="20" />
한규민's avatar
한규민 committed
257
258
259
260
261
262
                                    </div>
                                    {errorMsg.errorEmail && <p className={styles.passwordConfirmError}>이메일을 입력해주세요</p>}
                                </div>
                                <div className="d-flex flex-column">
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}>별명</label>
Kim, Subin's avatar
Kim, Subin committed
263
                                        <input className={`${styles.input} ${styles.inputSize}`} type="text" name="userNickName" placeholder="10자리 이내" onChange={handleUserOnChange} maxLength="10" />
한규민's avatar
한규민 committed
264
265
266
                                    </div>
                                    {errorMsg.errorNickName && <p className={styles.passwordConfirmError}>10 이내로 입력해주세요.</p>}
                                </div>
한규민's avatar
한규민 committed
267

한규민's avatar
한규민 committed
268
269
270
271
                                <div className="d-flex flex-column">
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}>휴대폰 번호</label>
                                        <div className="d-flex col-md-auto">
Kim, Subin's avatar
Kim, Subin committed
272
                                            <input className={`${styles.input} `} type="number" name="userMbnum" placeholder="-없이 11자리 입력" onChange={handleUserOnChange} min="" max="99999999999" />
273
                                            <button type="button" disabled={loading} className={`rounded-2 mt-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample" onClick={handleOnClickMbnum}>인증번호받기</button>
한규민's avatar
한규민 committed
274
275
276
                                        </div>
                                    </div>
                                    {errorMsg.errorMbnum && <p className={styles.passwordConfirmError}>-없이 숫자 11자리를 입력해주세요.</p>}
277
278
279
280
                                    <div className="collapse" id="collapseExample">
                                        <div className="d-flex justify-content-around mt-3">
                                            <label className={`${styles.confirm}`}>인증하기</label>
                                            <div>
한규민's avatar
한규민 committed
281
                                                <input className={`${styles.input}`} type="number" placeholder="인증번호를 입력" onChange={handleOnChangeMb} />
282
283
284
285
286
287
288
289
290
                                                <button type="button" className={`rounded-2 py-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} onClick={handleOnClickMbConfirm}>확인</button>
                                                <button type="button" className={`rounded-2 py-2 ${styles.butterYellowAndBtn} ${styles.btnHover}`} onClick={handleOnClickMbnum}>재전송</button>
                                            </div>
                                        </div>
                                        {(mbError === "재전송") && <p className={styles.passwordConfirmError}>유효시간이 만료되었습니다. 재전송해주세요.</p>}
                                        {(mbError === "보냄") && <p className={styles.passwordConfirmError}>5분이내에 입력해주세요.</p>}
                                        {(mbError === "성공") && <p className={styles.passwordConfirmError}>인증되었습니다.</p>}
                                        {(mbError === "실패") && <p className={styles.passwordConfirmError}>인증번호를 다시 입력해주세요.</p>}
                                    </div>
한규민's avatar
한규민 committed
291
                                </div>
한규민's avatar
한규민 committed
292
293
294
                                <div className="d-flex flex-column">
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}> 비밀번호</label>
Kim, Subin's avatar
Kim, Subin committed
295
                                        <input className={`${styles.input} ${styles.inputSize}`} type="password" name="userPassword" placeholder="8~11자리 사이" onChange={handleUserOnChange} maxLength="11" />
한규민's avatar
한규민 committed
296
297
                                    </div>
                                    {errorMsg.errorPassword && <p className={styles.passwordConfirmError}>8~11자리 사이로 입력해주세요.</p>}
한규민's avatar
한규민 committed
298

한규민's avatar
한규민 committed
299
                                </div>
한규민's avatar
한규민 committed
300

한규민's avatar
한규민 committed
301
302
303
                                <div className="d-flex flex-column">
                                    <div className={styles.inputContent}>
                                        <label className={styles.signupLabel}> 비밀번호 확인</label>
Kim, Subin's avatar
Kim, Subin committed
304
                                        <input className={`${styles.input} ${styles.inputSize}`} type="password" name="userRePassword" placeholder="8~11자리 사이" onChange={handleUserOnChange} maxLength="11" />
한규민's avatar
한규민 committed
305
306
                                    </div>
                                    {errorMsg.errorRePassword && <p className={styles.passwordConfirmError}>비밀번호가 일치하지 않습니다.</p>}
한규민's avatar
한규민 committed
307

한규민's avatar
한규민 committed
308
                                </div>
한규민's avatar
한규민 committed
309

한규민's avatar
한규민 committed
310
311
312
313
314
                            </div>
                            <div className="modal-footer">
                                <button type="button" className="btn btn-secondary rounded my-3 py-2 fs-5" data-bs-dismiss="modal" onClick={handleOnClick} disabled={loading}>닫기</button>
                                <button type="submit" className={`rounded my-3 py-2 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} disabled={loading}>수정하기</button>
                            </div>
Kim, Subin's avatar
Kim, Subin committed
315
                        </form>}
한규민's avatar
한규민 committed
316
                </div>
한규민's avatar
한규민 committed
317
            </div>
한규민's avatar
한규민 committed
318
        </>
한규민's avatar
한규민 committed
319
320
321
322
    )
}

export default MyInfo