profile.tsx 5 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
51
    setPicturename(profile.fileInfo.newfilename);
    setPlaceholder(profile.fileInfo.nickname);
52
53
54
55
  };
  const handleClick = async (
    e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent>
  ) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
56
    e.preventDefault();
57
    const formdata = new FormData();
Kim, MinGyu's avatar
Kim, MinGyu committed
58
    if (!(file === undefined || nickname === "")) {
59
      formdata.append("picture", file);
Kim, MinGyu's avatar
Kim, MinGyu committed
60
61
      formdata.append("nickname", nickname);
      console.log("both");
Kim, MinGyu's avatar
Kim, MinGyu committed
62
      console.log(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
63
      await profileApi.profileUpload(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
64
    } else if (!(file === undefined) && nickname === "") {
Kim, MinGyu's avatar
Kim, MinGyu committed
65
66
      formdata.append("picture", file);
      await profileApi.profileUpload(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
67
68
69
    } else if (file === undefined && !(nickname === "")) {
      formdata.append("nickname", nickname);
      await profileApi.profileUpload(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
70
71
    } else {
      alert("수정할 정보를 입력해주세요.");
72
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
73
74
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
75
  const deleteClick = async () => {
Kim, MinGyu's avatar
Kim, MinGyu committed
76
77
78
79
80
81
82
    if (confirm("삭제하시겠습니까?") == true) {
      const success = await profileApi.deleteUser();
      if (success) {
        await logout();
      }
    } else {
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
83
84
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
85
  useEffect(() => {
86
    userChange();
Kim, MinGyu's avatar
Kim, MinGyu committed
87
88
89
  }, []);

  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
90
    <div className="grid ">
Kim, MinGyu's avatar
Kim, MinGyu committed
91
      <form className="justify-items-center">
Kim, MinGyu's avatar
Kim, MinGyu committed
92
93
94
95
        <div className=" mt-10">프로필 수정</div>
        <div className="grid mt-10 border-0 border-y-2 border-gray-400 ">
          <div className="flex h-20">
            <div className=" basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
96
97
              Email
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
98
99
100
            <div className=" basis-full grid place-items-center justify-items-stretch px-4">
              {email}
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
101
          </div>
102
          <div className="flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
103
            <div className="basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
104
105
              사진
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
106
107
            <div className="basis-full py-4 ">
              <div className="overflow-hidden w-28 h-28 rounded-full border-2 m-5">
Kim, MinGyu's avatar
Kim, MinGyu committed
108
109
110
111
112
113
114
115
116
117
118
119
                {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>
120
121
122
123
124
125
              <input
                type="file"
                id="files"
                className="hidden"
                onChange={onUploadFile}
              ></input>
Kim, MinGyu's avatar
Kim, MinGyu committed
126
              <label htmlFor="files" className="border-2 m-5">
127
128
129
                이미지 선택
              </label>
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
130
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
131
132
          <div className="flex border-0 border-t-2 h-20">
            <div className="basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center shrink-0">
133
134
              별명
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
135
136
137
138
139
140

            <input
              placeholder={placeholder}
              className="basis-1/5 placeholder:text-black my-6 ml-5 border-2 "
              onChange={onNickChange}
            />
Kim, MinGyu's avatar
Kim, MinGyu committed
141
142
          </div>
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
143
144
145
146
147
        <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"
          >
148
149
            저장하기
          </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
150
151
152
153
          <button
            onClick={deleteClick}
            className="w-20 border-2 shrink-0 mr-10"
          >
Kim, MinGyu's avatar
Kim, MinGyu committed
154
155
            계정 삭제
          </button>
156
157
        </div>
      </form>
Kim, MinGyu's avatar
Kim, MinGyu committed
158
159
160
    </div>
  );
}