InfoUpdate.js 6.46 KB
Newer Older
이재연's avatar
오잉    
이재연 committed
1
import { Link, useParams } from "react-router-dom";
이재연's avatar
aa    
이재연 committed
2
3
4
import React, { useEffect, useState } from "react";
import userApi from "../../apis/user.api";

이재연's avatar
오잉    
이재연 committed
5
6
const userprofile = localStorage.getItem("user");
console.log(userprofile);
이재연's avatar
aa    
이재연 committed
7
const INIT_PROFILE = {
이재연's avatar
오잉    
이재연 committed
8
9
10
11
12
  id: userprofile,
  name:"",
  email:"",
  phone:"",
  img: "",
이재연's avatar
aa    
이재연 committed
13
};
이재연's avatar
이재연 committed
14

Kim, Chaerin's avatar
Kim, Chaerin committed
15
const InfoUpdate = () => {
이재연's avatar
오잉    
이재연 committed
16
17
  const { id } = useParams();

18

이재연's avatar
aa    
이재연 committed
19
20
  const [profile, setProfile] = useState(INIT_PROFILE);

이재연's avatar
오잉    
이재연 committed
21
22
23
24
25
26
27
28
29
30
31
  async function getProfile(userID) {
    try {
      const data = await userApi.getUser({ id: userID });
      setProfile(data);
      console.log(data);
    } catch (error) {}
  }

  useEffect(() => {
    getProfile(userprofile);
  }, []);
이재연's avatar
aa    
이재연 committed
32
33

  const handleChange = async (event) => {
이재연's avatar
오잉    
이재연 committed
34
35
36
37
38
39
40
41
42
43
44
45
46
    const { files } = event.target;
    let formData = new FormData();
    formData.append("img", files[0]);
    formData.append("id", userprofile);
    try {
      const res = await userApi.profileimg(formData);
      console.log(res);
      if(files){
        setProfile({...profile, img:res})
      }else{
        setProfile()
      }   } catch (error) {}
  };
47

이재연's avatar
오잉    
이재연 committed
48
49
50
51
52
53
54
55
56
  const changeinfo = async (event) => {
    console.log(profile);
    const res = await userApi.updateinfo(profile)
  };
  const updateinfo = (event) => {
    const { name, value } = event.target;
    setProfile({ ...profile, [name]: value });
    console.log(profile);
  };
이재연's avatar
이재연 committed
57

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

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

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

이재연's avatar
오잉    
이재연 committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
                <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
216
              </div>
이재연's avatar
오잉    
이재연 committed
217
              <div className="col"></div>
이재연's avatar
이재연 committed
218
219
            </div>
          </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
220
        </div>
이재연's avatar
이재연 committed
221
      </div>
222
    </div>
이재연's avatar
이재연 committed
223
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
224
225
226
};

export default InfoUpdate;