InfoUpdate.js 6.5 KB
Newer Older
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
1
2
3
import { Link, useParams } from "react-router-dom";
import React, { useEffect, useState } from "react";
import userApi from "../../apis/user.api";
4
import catchErrors from "../../context/catchError";
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
5
6
7
8
9
10
11
12
13

const userprofile = localStorage.getItem("user");
const INIT_PROFILE = {
  id: userprofile,
  name:"",
  email:"",
  phone:"",
  img: "",
};
이재연's avatar
이재연 committed
14

Kim, Chaerin's avatar
Kim, Chaerin committed
15
const InfoUpdate = () => {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
16
17
18
19
  const { id } = useParams();


  const [profile, setProfile] = useState(INIT_PROFILE);
20
  const [error,setError]=useState("");
21

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
22
23
24
25
  async function getProfile(userID) {
    try {
      const data = await userApi.getUser(userID);
      setProfile(data);
26
27
28
    } catch (error) {
      catchErrors(error, setError);
    }
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
29
  }
30

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
31
32
33
  useEffect(() => {
    getProfile(userprofile);
  }, []);
이재연's avatar
이재연 committed
34

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
35
36
37
38
39
40
41
42
43
44
45
  const handleChange = async (event) => {
    const { files } = event.target;
    let formData = new FormData();
    formData.append("img", files[0]);
    formData.append("id", userprofile);
    try {
      const res = await userApi.profileimg(formData);
      if(files){
        setProfile({...profile, img:res})
      }else{
        setProfile()
46
47
48
      }   } catch (error) {
        catchErrors(error, setError);
      }
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
49
50
51
52
53
54
55
56
57
58
59
  };

  const changeinfo = async (event) => {
    const res = await userApi.updateinfo(profile)
  };
  const updateinfo = (event) => {
    const { name, value } = event.target;
    setProfile({ ...profile, [name]: value });
  };

  const{email,phone,name}=profile
이재연's avatar
이재연 committed
60
  return (
우지원's avatar
우지원 committed
61
62
    <div className="container" style={{ background: "#FCF4FF" }}>
      <div className="row">
Kim, Chaerin's avatar
Kim, Chaerin committed
63
        <div>
이재연's avatar
이재연 committed
64
          <label
우지원's avatar
우지원 committed
65
            className="form-label mx-2 my-4"
이재연's avatar
이재연 committed
66
67
68
69
            style={{ fontSize: "18px", fontWeight: "bold" }}
          >
            프로필 정보
          </label>
이재연's avatar
이재연 committed
70
          <Link to={`/profile/${id}`}>
71
72
            <button
              type="button"
우지원's avatar
우지원 committed
73
              className="btn btn-outline-white "
74
              style={{
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
75
                background: "#d4cafb",
76
77
78
79
                fontSize: "13px",
                fontWeight: "bold",
              }}
            >
이재연's avatar
이재연 committed
80
              돌아가기
81
            </button>
이재연's avatar
이재연 committed
82
          </Link>
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
          <Link to={`/profile/${id}`}>
            <button
              onClick={changeinfo}
              type="button"
              className="btn btn-outline-white mx-2 "
              style={{
                background: "#d4cafb",
                fontSize: "13px",
                fontWeight: "bold",
              }}
            >
              저장
            </button>
          </Link>
이재연's avatar
이재연 committed
97
        </div>
98
      </div>
이재연's avatar
이재연 committed
99

우지원's avatar
우지원 committed
100
      <div className="col">
Kim, Chaerin's avatar
Kim, Chaerin committed
101
        <div>
우지원's avatar
우지원 committed
102
          <div className="row">
이재연's avatar
이재연 committed
103
104
105
106
            <div
              className="d-flex justify-content-center"
              style={{ position: "relative" }}
            >
107
              <img
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
108
                src={`/uploads/${profile.img}`}
이재연's avatar
이재연 committed
109
                id="imgfile"
이재연's avatar
이재연 committed
110
                className="rounded-circle mt-2"
111
112
                style={{ height: "320px", width: "320px" }}
              />
이재연's avatar
이재연 committed
113
114
115
116
              <div
                className="d-flex justify-content-end"
                style={{ position: "absolute", left: "295px", top: "30px" }}
              >
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
117
                <label htmlFor="inputfile">
이재연's avatar
이재연 committed
118
119
120
121
122
123
124
125
126
127
128
                  <img
                    className="rounded-circle"
                    src="/infoimg.jpg"
                    style={{
                      width: "50px",
                      height: "50px",
                      backgroundColor: "white",
                    }}
                  />
                </label>
                <input
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
129
130
                  onChange={handleChange}
                  name="avatarUrl"
이재연's avatar
이재연 committed
131
132
133
134
135
136
                  type="file"
                  accept="image/*"
                  id="inputfile"
                  style={{ display: "none" }}
                />
              </div>
137
            </div>
이재연's avatar
이재연 committed
138

이재연's avatar
이재연 committed
139
140
            <div className="col d-flex justify-content-center">
              <input
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
141
142
143
                onChange={updateinfo}
                name="email"
                value={profile.email}
이재연's avatar
이재연 committed
144
145
146
147
148
149
150
151
152
                type="text"
                className="form-control my-4 "
                placeholder="사용자명 입력"
                style={{
                  background: "#fcf4ff",
                  borderTop: "0",
                  borderRight: "0",
                  borderLeft: "0",
                  borderBottom: "1",
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
153
                  borderColor: "#d4cafb",
이재연's avatar
이재연 committed
154
155
156
157
158
159
160
161
                  height: "38px",
                  width: "130px",
                }}
              />
              <div
                className="justify-content-center ms-2 my-4"
                style={{ fontSize: "25px", fontWeight: "bold" }}
              >
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
162
                #{userprofile}
이재연's avatar
이재연 committed
163
              </div>
164
            </div>
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
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
            <div className="row">
              <div className="col-3"></div>
              <div className="col-6 my-2">
                <h2
                  className="mb-3"
                  style={{ fontSize: "13px", fontWeight: "bold" }}
                >
                  이름
                </h2>
                <input
                  onChange={updateinfo}
                  name="name"
                  value={profile.name}
                  type="text"
                  className="form-control my-3 "
                  placeholder="이름 입력"
                  style={{
                    background: "#fcf4ff",
                    borderTop: "0",
                    borderRight: "0",
                    borderLeft: "0",
                    borderBottom: "1",
                    borderColor: "#d4cafb",
                    height: "38px",
                    width: "180px",
                  }}
                />
이재연's avatar
이재연 committed
192

Kim, Chaerin's avatar
merge19    
Kim, Chaerin 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
                <h2
                  className="mb-3 mt-2"
                  style={{ fontSize: "13px", fontWeight: "bold" }}
                >
                  전화번호
                </h2>
                <input
                  onChange={updateinfo}
                  name="phone"
                  value={profile.phone}
                  type="text"
                  className="form-control my-3 "
                  placeholder="전화번호 입력"
                  style={{
                    background: "#fcf4ff",
                    borderTop: "0",
                    borderRight: "0",
                    borderLeft: "0",
                    borderBottom: "1",
                    borderColor: "#d4cafb",
                    height: "38px",
                    width: "180px",
                  }}
                />
이재연's avatar
이재연 committed
217
              </div>
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
218
              <div className="col"></div>
이재연's avatar
이재연 committed
219
220
            </div>
          </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
221
        </div>
이재연's avatar
이재연 committed
222
      </div>
223
    </div>
이재연's avatar
이재연 committed
224
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
225
226
227
};

export default InfoUpdate;