profile.tsx 4.72 KB
Newer Older
Kim, MinGyu's avatar
Kim, MinGyu committed
1
import React, { 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
6
7

export default function Profile() {
  // 로컬 저장소에는 로그인 여부만 저장
8
9
10
11
12
  const [email, setEmail] = useState("");
  const [picturename, setPicturename] = useState("");
  const [file, setFile] = useState<File>();
  const [imageSrc, setImageSrc] = useState("");
  const [nickname, setNickname] = useState("");
Kim, MinGyu's avatar
Kim, MinGyu committed
13
  const [placeholder, setPlaceholder] = useState("");
Kim, MinGyu's avatar
Kim, MinGyu committed
14
  const { logout } = useAuth();
Kim, MinGyu's avatar
Kim, MinGyu committed
15

16
17
18
19
20
21
22
23
24
  const handleProfile = async () => {
    const user: Profile = await profileApi.profile();
    return user;
  };

  const PictureSrc = (fileBlob: Blob) => {
    const reader = new FileReader();

    reader.readAsDataURL(fileBlob);
Kim, MinGyu's avatar
Kim, MinGyu committed
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    reader.onload = (data) => {
      if (typeof data.target?.result === "string") {
        console.log(data.target?.result);
        setImageSrc(data.target?.result);
      }
    };
  };

  const onUploadFile = (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (!(file === undefined)) {
      setFile(file);
      PictureSrc(file);
    }
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
42
  const onNickChange = (e: React.ChangeEvent<HTMLInputElement>) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
43
44
    const newnickname = e.target.value;
    setNickname(newnickname);
Kim, MinGyu's avatar
Kim, MinGyu committed
45
46
  };

47
48
49
  const userChange = async () => {
    const profile = await handleProfile();
    setEmail(profile.email);
Kim, MinGyu's avatar
Kim, MinGyu committed
50
    setPicturename(profile.avatar.newfilename);
Kim, MinGyu's avatar
Kim, MinGyu committed
51
    setPlaceholder(profile.avatar.nickname);
52
53
54
55
56
  };
  const handleClick = async (
    e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent>
  ) => {
    const formdata = new FormData();
Kim, MinGyu's avatar
Kim, MinGyu committed
57
    if (!(file === undefined || nickname === "")) {
58
      formdata.append("picture", file);
Kim, MinGyu's avatar
Kim, MinGyu committed
59
60
61
      formdata.append("nickname", nickname);
      console.log("both");
      await profileApi.picture(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
62
    } else if (!(nickname === "")) {
Kim, MinGyu's avatar
Kim, MinGyu committed
63
64
65
66
67
      formdata.append("nickname", nickname);
      console.log("picture");
      await profileApi.picture(formdata);
    } else {
      alert("수정할 정보를 입력해주세요.");
68
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
69
70
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
71
  const deleteClick = async () => {
Kim, MinGyu's avatar
Kim, MinGyu committed
72
73
74
75
    await profileApi.deleteUser().then(() => {
      logout();
      console.log("test");
    });
Kim, MinGyu's avatar
Kim, MinGyu committed
76
77
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
78
  useEffect(() => {
79
    userChange();
Kim, MinGyu's avatar
Kim, MinGyu committed
80
81
82
  }, []);

  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
83
84
85
86
    <div className="grid md:px-60">
      <form className="justify-items-center">
        <div className="ml-20 mt-10">프로필 수정</div>
        <div className="grid mt-20 border-0 border-y-2 ">
87
          <div className="flex">
Kim, MinGyu's avatar
Kim, MinGyu committed
88
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
89
90
91
              Email
            </div>
            <div className="basis-full my-5 p-5">{email}</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
92
          </div>
93
          <div className="flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
94
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
95
96
              사진
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
            <div className="basis-full p-2 ">
              <div className="overflow-hidden w-40 h-40 rounded-full border-2 m-5">
                {imageSrc ? (
                  <img
                    src={imageSrc}
                    className="object-cover object-center h-full"
                  />
                ) : (
                  <img
                    src={"http://localhost:3000/images/" + picturename}
                    className="object-cover object-center h-full"
                  />
                )}
              </div>
111
112
113
114
115
116
              <input
                type="file"
                id="files"
                className="hidden"
                onChange={onUploadFile}
              ></input>
Kim, MinGyu's avatar
Kim, MinGyu committed
117
              <label htmlFor="files" className="border-2 m-5">
118
119
120
                이미지 선택
              </label>
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
121
          </div>
122
          <div className="flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
123
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
124
125
126
127
              별명
            </div>
            <div className="basis-full ">
              <input
Kim, MinGyu's avatar
Kim, MinGyu committed
128
129
                placeholder={placeholder}
                className="basis-full placeholder:text-black my-10 ml-5 border-2"
Kim, MinGyu's avatar
Kim, MinGyu committed
130
                onChange={onNickChange}
131
132
              />
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
133
134
          </div>
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
135
136
137
138
139
        <div className="grid grid-cols-2 my-4 md:mb-20 justify-items-center">
          <button
            onClick={handleClick}
            className="w-20 border-2 shrink-0 ml-10"
          >
140
141
            저장하기
          </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
142
143
144
145
          <button
            onClick={deleteClick}
            className="w-20 border-2 shrink-0 mr-10"
          >
Kim, MinGyu's avatar
Kim, MinGyu committed
146
147
            계정 삭제
          </button>
148
149
        </div>
      </form>
Kim, MinGyu's avatar
Kim, MinGyu committed
150
151
152
    </div>
  );
}