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

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

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


  const [profile, setProfile] = useState(INIT_PROFILE);
19

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

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
27
28
29
  useEffect(() => {
    getProfile(userprofile);
  }, []);
이재연's avatar
이재연 committed
30

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  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()
      }   } catch (error) {}
  };

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

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

이재연's avatar
이재연 committed
133
134
            <div className="col d-flex justify-content-center">
              <input
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
135
136
137
                onChange={updateinfo}
                name="email"
                value={profile.email}
이재연's avatar
이재연 committed
138
139
140
141
142
143
144
145
146
                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
147
                  borderColor: "#d4cafb",
이재연's avatar
이재연 committed
148
149
150
151
152
153
154
155
                  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
156
                #{userprofile}
이재연's avatar
이재연 committed
157
              </div>
158
            </div>
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
            <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
186

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

export default InfoUpdate;