InfoUpdate.js 6.54 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";
Kim, Chaerin's avatar
Kim, Chaerin committed
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);
Kim, Chaerin's avatar
Kim, Chaerin committed
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);
Kim, Chaerin's avatar
Kim, Chaerin committed
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
  const handleChange = async (event) => {
    const { files } = event.target;
Kim, Chaerin's avatar
Kim, Chaerin committed
37
    console.log('files:',files)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
38
39
40
41
42
43
44
45
46
    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()
Kim, Chaerin's avatar
Kim, Chaerin committed
47
48
49
      }   } catch (error) {
        catchErrors(error, setError);
      }
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
50
51
52
53
54
55
56
57
58
59
60
  };

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

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

이재연's avatar
이재연 committed
140
141
            <div className="col d-flex justify-content-center">
              <input
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
142
143
144
                onChange={updateinfo}
                name="email"
                value={profile.email}
이재연's avatar
이재연 committed
145
146
147
148
149
150
151
152
153
                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
154
                  borderColor: "#d4cafb",
이재연's avatar
이재연 committed
155
156
157
158
159
160
161
162
                  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
163
                #{userprofile}
이재연's avatar
이재연 committed
164
              </div>
165
            </div>
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
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
            <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
193

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

export default InfoUpdate;