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

const MyInfo = () => {
    const { user } = useAuth();
한규민's avatar
한규민 committed
9
    const [userNickName, setUserNickName] = useState("사용자");
한규민's avatar
한규민 committed
10

한규민's avatar
한규민 committed
11
    // 사용자 이름 가져오는 함수
한규민's avatar
한규민 committed
12
13
    const getNickName = async (id) => {
        const nickname = await authApi.getNickName(id);
한규민's avatar
한규민 committed
14
        setUserNickName(nickname);
한규민's avatar
한규민 committed
15
16
    }

한규민's avatar
한규민 committed
17
18
19
20
21
22
    //현재 비밀번호 state
    const [presentPw, setPresentPw] = useState("");
    const [loading, setLoading] = useState(false);
    const [style, setStyle] = useState("visually-hidden-focusable");

    //변경할 데이터 입력받는 state
한규민's avatar
한규민 committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    const [userRe, setUserRe] = useState({
        userEmail: "",
        userNickName: "",
        userMbnum: "",
        userPassword: "",
        userRePassword: ""
    })

    //각 타입별 error 유무 state
    const [error, setError] = useState("");
    const [errorMsg, setErrorMsg] = useState({
        errorEmail: false,
        errorNickName: false,
        errorMbnum: false,
        errorPassword: false,
        errorRePassword: false
    })

한규민's avatar
한규민 committed
41
    useEffect(() => {
한규민's avatar
한규민 committed
42
        getNickName(user.id);
한규민's avatar
한규민 committed
43
44
    }, [])

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

    //입력할때마다 state에 저장
    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
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
    const enterKey = (e) => {
        if (e.key === "Enter") {

            handleOnSummitVerify(e);
        }
    }
    //기존 비밀번호 확인 
    const handleOnSummitVerify = async (e) => {
        e.preventDefault();
        try {
            setError(() => (""));
            setLoading(() => (true));
            const pw = presentPw;
            const confirmPw = await authApi.comparePw(pw);
            if (confirmPw) {
                setStyle("");
            } else {
                setStyle("visually-hidden-focusable");
                alert("비밀번호가 일치하지 않습니다.");
            }
        } catch (error) {
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }
한규민's avatar
한규민 committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

    //비교하여 error메세지 반환
    const vaildationData = (text, compareValue, error) => {
        if (text !== compareValue) {
            setErrorMsg(errorMsg => ({ ...errorMsg, [error]: true }));
        } else {
            setErrorMsg(errorMsg => ({ ...errorMsg, [error]: false }));
        }
    }

    const vaildationIdPw = (text, minValue, error) => {
        if ((text < minValue)) {
            setErrorMsg(errorMsg => ({ ...errorMsg, [error]: true }));
        } else {
            setErrorMsg(errorMsg => ({ ...errorMsg, [error]: false }));
        }
    }
    //유효성 검사
한규민's avatar
한규민 committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    const validation = async () => {
        try {//별명 유효성 검사
            vaildationData((userRe.userNickName.length === 0), false, "errorNickName");
            // 휴대폰 유효성 검사
            vaildationData(userRe.userMbnum.length, 11, "errorMbnum");
            // 비밀번호 유효성 검사
            vaildationIdPw(userRe.userPassword.length, 8, "errorPassword");
            // 비밀번호 확인 유효성 검사
            vaildationData(userRe.userPassword, userRe.userRePassword, "errorRePassword");

            // 최종 유효성 검사
            if ((Object.values(errorMsg).some((element) => (element)) === false)) {
                return true
            } else {
                throw new Error("유효하지 않은 데이터입니다.");
            }
        } catch (error) {
            catchErrors(error, setError);
한규민's avatar
한규민 committed
124
125
126
127
128
129
130
        }
    }


    const handleOnSummit = async (e) => {
        e.preventDefault();
        try {
한규민's avatar
한규민 committed
131

한규민's avatar
한규민 committed
132
133
134
            setError(() => (""));
            //처리가 될때까지 버튼(가입하기)이 안눌리게 지정
            setLoading(() => (true));
한규민's avatar
한규민 committed
135
            console.log("userRe : " + userRe.userEmail);
한규민's avatar
한규민 committed
136
            //유효성 검사
한규민's avatar
한규민 committed
137
138
139
140
141
142
143
144
145
            const vaild = validation();
            if (vaild) {
                const userData = userRe;
                console.log(userData);
                //서버로 전송
                const process = await authApi.modifyUser(userData);
                console.log("process : " + process);
                alert("회원정보 수정 완료");
            }
한규민's avatar
한규민 committed
146
147
148
149
        } catch (error) {
            //에러전송
            catchErrors(error, setError);
        } finally {
한규민's avatar
한규민 committed
150
            setLoading(() => (false));
한규민's avatar
한규민 committed
151
152
153
154
        }
    }


한규민's avatar
한규민 committed
155
156
157
158

    return (
    <>
        {/* 마이페이지 창 */}
한규민's avatar
한규민 committed
159
160
        <div className="d-flex flex-column">
            <span className={styles.title}>마이페이지</span>
한규민's avatar
한규민 committed
161
162
163
164
165
            <div className="d-flex flex justify-content-center">
                <img src="https://search.pstatic.net/sunny/?src=https%3A%2F%2Fi.pinimg.com%2Foriginals%2F1e%2F1c%2F2e%2F1e1c2e4e07f5c440c3adad38bcd6f854.jpg&type=a340"
                className="img-fluid px-5 rounded-circle" />
                {/* <button type="submit"></button> */}
                <div className="d-flex flex-column py-4 justify-content-around">
한규민's avatar
한규민 committed
166
                    <span className={`${styles.userName}`}>{`${userNickName}`} 반갑습니다!</span>
한규민's avatar
한규민 committed
167
                    <button className={`rounded my-3 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} data-bs-toggle="modal" href="#verifyPassword" >회원정보 수정</button>
한규민's avatar
한규민 committed
168
169
170
171
                </div>
            </div>
        </div>

한규민's avatar
한규민 committed
172
173
174
175
176
177
        {/* 기존 비밀번호 확인 모달창 */}
        <div className="modal fade" id="verifyPassword" data-bs-backdrop="static" data-bs-keyboard="false" aria-hidden="true" aria-labelledby="verifyPasswordLabel" tabindex="-1">
            <div className="modal-dialog modal-dialog-centered">
                <form className="modal-content" onSubmit={handleOnSummitVerify}>
                    <div className="modal-header">
                        <h5 className="modal-title" id="verifyPasswordLabel">기존 비밀번호 확인</h5>
한규민's avatar
한규민 committed
178
179
                        <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
한규민's avatar
한규민 committed
180
181
182
183
184
185
186
187
188
189
190
191
                    <div clasNames="modal-body">
                        <div className="d-flex flex-column ">
                            <div className="d-flex justify-content-around align-items-center my-4">
                                <label className={styles.signupLabel}>현재 비밀번호</label>
                                <input className={`${styles.input} ${styles.inputSize}`} type="password" name="userPassword" placeholder="8~11자리 사이" onChange={handlePwOnChange} onKeyPress={enterKey} maxLength="11" required />
                            </div>
                            {(style !== "visually-hidden-focusable") && <p className={styles.passwordConfirmError}>다음을 클릭해주세요.</p>}
                        </div>
                    </div>
                    <div className="modal-footer">
                        <button className={`${style} rounded my-3 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} data-bs-target="#modify" data-bs-toggle="modal" data-bs-dismiss="modal" disable={style === "visually-hidden-focusable"}>다음</button>
                        <button type="submit" className={`rounded my-3 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} disabled={loading}>확인</button>
한규민's avatar
한규민 committed
192

한규민's avatar
한규민 committed
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
                    </div>
                </form>
            </div>
        </div>

        {/* 회원정보 수정 모달창 */}
        <div className="modal fade" id="modify" data-bs-backdrop="static" data-bs-keyboard="false" aria-hidden="true" aria-labelledby="modifyLabel" tabindex="-1">
            <div className="modal-dialog modal-dialog-centered">
                <form className={`modal-content d-flex col-md-6 col-12 justify-content-center d-flex flex-column`} onSubmit={handleOnSummit}>
                    <div className="modal-header">
                        <h5 className="modal-title" id="modifyLabel">회원정보 수정</h5>
                        <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div className={`modal-body`}>
                        <div className="d-flex flex-column">
                            <div className={styles.inputContent}>
                                <label className={styles.signupLabel}>이메일</label>
                                <input className={`${styles.input} ${styles.inputSize}`} type="email" name="userEmail" placeholder="이메일을 입력해주세요" onChange={handleUserOnChange} maxlength="20" required />
                            </div>
                            {errorMsg.errorEmail && <p className={styles.passwordConfirmError}>이메일을 입력해주세요</p>}
                        </div>
                        <div className="d-flex flex-column">
                            <div className={styles.inputContent}>
                                <label className={styles.signupLabel}>별명</label>
                                <input className={`${styles.input} ${styles.inputSize}`} type="text" name="userNickName" placeholder="10자리 이내" onChange={handleUserOnChange} maxLength="10" required />
                            </div>
                            {errorMsg.errorNickName && <p className={styles.passwordConfirmError}>10 이내로 입력해주세요.</p>}
                        </div>
한규민's avatar
한규민 committed
221

한규민's avatar
한규민 committed
222
223
224
225
226
                        <div className="d-flex flex-column">
                            <div className={styles.inputContent}>
                                <label className={styles.signupLabel}>휴대폰 번호</label>
                                <div className="d-flex col-md-auto">
                                    <input className={`${styles.input} ${styles.inputSize}`} type="number" name="userMbnum" placeholder="-없이 11자리 입력" onChange={handleUserOnChange} min="" max="99999999999" required />
한규민's avatar
한규민 committed
227
                                </div>
한규민's avatar
한규민 committed
228
229
230
                            </div>
                            {errorMsg.errorMbnum && <p className={styles.passwordConfirmError}>-없이 숫자 11자리를 입력해주세요.</p>}
                        </div>
한규민's avatar
한규민 committed
231

한규민's avatar
한규민 committed
232
233
234
235
                        <div className="d-flex flex-column">
                            <div className={styles.inputContent}>
                                <label className={styles.signupLabel}> 비밀번호</label>
                                <input className={`${styles.input} ${styles.inputSize}`} type="password" name="userPassword" placeholder="8~11자리 사이" onChange={handleUserOnChange} maxLength="11" required />
한규민's avatar
한규민 committed
236
                            </div>
한규민's avatar
한규민 committed
237
238
239
240
241
242
243
244
                            {errorMsg.errorPassword && <p className={styles.passwordConfirmError}>8~11자리 사이로 입력해주세요.</p>}

                        </div>

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

한규민's avatar
한규민 committed
248
                        </div>
한규민's avatar
한규민 committed
249
250
251
252
253
254
255

                    </div>
                    <div className="modal-footer">
                        <button type="button" className="btn btn-secondary rounded my-3 py-2 fs-5" data-bs-dismiss="modal" disabled={loading}>닫기</button>
                        <button type="submit" className={`rounded my-3 py-2 fs-5 ${styles.butterYellowAndBtn} ${styles.btnHover}`} disabled={loading}>수정하기</button>
                    </div>
                </form>
한규민's avatar
한규민 committed
256
257
            </div>
        </div>
한규민's avatar
한규민 committed
258
    </>
한규민's avatar
한규민 committed
259
260
261
262
    )
}

export default MyInfo