profile.tsx 5.31 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
import React, { ChangeEvent, FormEvent, useEffect, useState } from "react";
2
3
import { Profile } from "../types";
import { profileApi } from "../apis";
Kim, MinGyu's avatar
Kim, MinGyu committed
4
import { useAuth } from "../auth/auth.context";
Kim, MinGyu's avatar
Kim, MinGyu committed
5
import { Link } from "react-router-dom";
Kim, MinGyu's avatar
Kim, MinGyu committed
6
7
8

export default function Profile() {
  // 로컬 저장소에는 로그인 여부만 저장
9
  const [email, setEmail] = useState("");
Yoon, Daeki's avatar
Yoon, Daeki committed
10
11
12
13
14
  const [profile, setProfile] = useState<{
    name: string;
    avatar: File | null;
  }>({ name: "", avatar: null });
  const [avatarUrl, setAvatarUrl] = useState("");
15
  const [imageSrc, setImageSrc] = useState("");
Kim, MinGyu's avatar
Kim, MinGyu committed
16
  const { logout } = useAuth();
Kim, MinGyu's avatar
Kim, MinGyu committed
17

Yoon, Daeki's avatar
Yoon, Daeki committed
18
19
20
21
22
23
24
25
26
  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    const { name, value, files } = e.target;
    console.log("name", name, "value", value);
    if (files) {
      setProfile({ ...profile, [name]: files[0] });
      showImage(files[0]);
    } else {
      setProfile({ ...profile, [name]: value });
    }
27
28
  };

Yoon, Daeki's avatar
Yoon, Daeki committed
29
  const showImage = (blob: Blob) => {
30
    const reader = new FileReader();
Yoon, Daeki's avatar
Yoon, Daeki committed
31
    reader.readAsDataURL(blob);
32
33
    reader.onload = (data) => {
      if (typeof data.target?.result === "string") {
Yoon, Daeki's avatar
Yoon, Daeki committed
34
        // console.log(data.target?.result);
35
36
37
38
39
        setImageSrc(data.target?.result);
      }
    };
  };

Yoon, Daeki's avatar
Yoon, Daeki committed
40
  const handleSubmit = async (e: FormEvent) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
41
    e.preventDefault();
Yoon, Daeki's avatar
Yoon, Daeki committed
42
    console.log("profile in submit", profile);
43
    const formdata = new FormData();
Yoon, Daeki's avatar
Yoon, Daeki committed
44
    profile.avatar && formdata.append("avatar", profile.avatar);
45
46

    console.log(profile.avatar);
Yoon, Daeki's avatar
Yoon, Daeki committed
47
48
49
    formdata.append("name", profile.name);
    console.log("form data", formdata.get("avatar"));
    profileApi.profileUpload(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
50
51
  };

Yoon, Daeki's avatar
Yoon, Daeki committed
52
  const onDelete = async () => {
Kim, MinGyu's avatar
Kim, MinGyu committed
53
    if (confirm("삭제하시겠습니까?") == true) {
Yoon, Daeki's avatar
Yoon, Daeki committed
54
55
      await profileApi.deleteUser();
      await logout();
Kim, MinGyu's avatar
Kim, MinGyu committed
56
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
57
58
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
59
  useEffect(() => {
Yoon, Daeki's avatar
Yoon, Daeki committed
60
61
62
63
    const getProfile = async () => {
      const user: Profile = await profileApi.profile();
      console.log("user in effect", user);
      setEmail(user.email);
64
      setAvatarUrl(user.avatar?.newfilename);
Yoon, Daeki's avatar
Yoon, Daeki committed
65
66
67
      setProfile({ ...profile, name: user.name });
    };
    getProfile();
Kim, MinGyu's avatar
Kim, MinGyu committed
68
69
70
  }, []);

  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
71
72
73
74
    <div className="grid bg-white rounded shadow-lg mb-5">
      <form className="mx-24 " onSubmit={handleSubmit}>
        <div className=" mt-7 text-2xl">프로필 수정</div>
        <div className=" mt-10 border-0 border-y-2 border-gray-400">
Kim, MinGyu's avatar
Kim, MinGyu committed
75
          <div className="flex h-20">
Kim, MinGyu's avatar
Kim, MinGyu committed
76
            <div className="w-24  border-0 border-r-2 bg-gray-100 grid place-items-center whitespace-nowrap">
77
78
              Email
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
79
            <div className=" grid place-items-center px-4 whitespace-nowrap">
Kim, MinGyu's avatar
Kim, MinGyu committed
80
81
              {email}
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
82
          </div>
83
          <div className="flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
84
            <div className="w-24 border-0 border-r-2 bg-gray-100 grid place-items-center whitespace-nowrap">
85
86
              사진
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
87
88
89
90
            <div className="flex  py-4 ">
              <div className="">
                <div className="overflow-hidden w-28 h-28 rounded-full border-2 mx-3 mb-3">
                  {imageSrc ? (
Yoon, Daeki's avatar
Yoon, Daeki committed
91
                    <img
Kim, MinGyu's avatar
Kim, MinGyu committed
92
                      src={imageSrc}
Yoon, Daeki's avatar
Yoon, Daeki committed
93
94
                      className="object-cover object-center h-full"
                    />
Kim, MinGyu's avatar
Kim, MinGyu committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                  ) : (
                    avatarUrl && (
                      <img
                        src={"http://localhost:3000/images/" + avatarUrl}
                        className="object-cover object-center h-full"
                      />
                    )
                  )}
                </div>
                <input
                  type="file"
                  name="avatar"
                  id="avatar"
                  className="hidden"
                  onChange={handleChange}
                ></input>
                <label
                  htmlFor="avatar"
                  className="border-2 m-5 whitespace-nowrap "
                >
                  이미지 선택
                </label>
              </div>
              <div className="pl-10 flex place-self-center text-gray-400 text-sm whitespace-nowrap">
                {"<새로운 이미지를 넣어보세요!>"}
Kim, MinGyu's avatar
Kim, MinGyu committed
120
              </div>
121
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
122
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
123
          <div className="flex border-0 border-t-2 h-20">
Kim, MinGyu's avatar
Kim, MinGyu committed
124
            <div className="w-24 border-0 border-r-2 bg-gray-100 grid place-items-center whitespace-nowrap">
Yoon, Daeki's avatar
Yoon, Daeki committed
125
              이름
126
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
127
            <input
Yoon, Daeki's avatar
Yoon, Daeki committed
128
129
              name="name"
              placeholder={profile.name}
Kim, MinGyu's avatar
Kim, MinGyu committed
130
              className=" placeholder:text-black my-6 ml-5 border-2 "
Yoon, Daeki's avatar
Yoon, Daeki committed
131
              onChange={handleChange}
Kim, MinGyu's avatar
Kim, MinGyu committed
132
            />
Kim, MinGyu's avatar
Kim, MinGyu committed
133
134
          </div>
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
135
        <div className="flex mb-5 justify-center gap-x-3">
Kim, MinGyu's avatar
Kim, MinGyu committed
136
          <button
Yoon, Daeki's avatar
Yoon, Daeki committed
137
            onClick={handleSubmit}
Kim, MinGyu's avatar
Kim, MinGyu committed
138
            className=" mt-5 h-12 w-1/5 border-2 border-blue-400 text-lg  place-self-center whitespace-nowrap"
Kim, MinGyu's avatar
Kim, MinGyu committed
139
          >
140
141
            저장하기
          </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
142

Kim, MinGyu's avatar
Kim, MinGyu committed
143
          <button className=" mt-5 h-12 w-1/5  text-lg border-2 border-orange-400 place-self-center whitespace-nowrap">
Kim, MinGyu's avatar
Kim, MinGyu committed
144
145
146
            <Link to="/">취소</Link>
          </button>

Kim, MinGyu's avatar
Kim, MinGyu committed
147
          <button
Yoon, Daeki's avatar
Yoon, Daeki committed
148
149
            type="button"
            onClick={onDelete}
Kim, MinGyu's avatar
Kim, MinGyu committed
150
            className=" mt-5 h-12 w-1/5  text-lg border-2 border-red-400 place-self-center whitespace-nowrap"
Kim, MinGyu's avatar
Kim, MinGyu committed
151
          >
Kim, MinGyu's avatar
Kim, MinGyu committed
152
153
            계정 삭제
          </button>
154
155
        </div>
      </form>
Kim, MinGyu's avatar
Kim, MinGyu committed
156
157
158
    </div>
  );
}