profile.tsx 4.04 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 { logout } = useAuth();
Kim, MinGyu's avatar
Kim, MinGyu committed
14

15
16
17
18
19
20
21
22
23
  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
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    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
41
42
43
44
45
  const onNickChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const nickname = e.target.value;
    setNickname(nickname);
  };

46
47
48
  const userChange = async () => {
    const profile = await handleProfile();
    setEmail(profile.email);
Kim, MinGyu's avatar
Kim, MinGyu committed
49
    setPicturename(profile.avatar.newfilename);
50
51
52
53
54
55
56
57
  };
  const handleClick = async (
    e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent>
  ) => {
    const formdata = new FormData();
    if (!(file === undefined || file === null)) {
      formdata.append("picture", file);
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
58
    formdata.append("nickname", nickname);
59
    await profileApi.picture(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
60
61
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
62
63
64
65
  const deleteClick = async () => {
    await profileApi.deleteUser().then(() => logout());
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
66
  useEffect(() => {
67
    userChange();
Kim, MinGyu's avatar
Kim, MinGyu committed
68
69
70
71
  }, []);

  return (
    <div className="grid ">
72
73
74
75
76
77
78
79
      <form className="">
        <div className="ml-40 mt-10">프로필 </div>
        <div className="grid ml-40 mt-20 border-0 border-y-2 w-2/3">
          <div className="flex">
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center ">
              Email
            </div>
            <div className="basis-full my-5 p-5">{email}</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
80
          </div>
81
82
83
84
85
86
87
88
89
90
91
92
93
94
          <div className="flex border-0 border-t-2">
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center">
              사진
            </div>
            <div className="basis-full p-2">
              {imageSrc ? (
                <img
                  src={imageSrc}
                  width={200}
                  height={200}
                  alt="preview-img"
                />
              ) : (
                <img
Kim, MinGyu's avatar
Kim, MinGyu committed
95
                  src={"http://localhost:3000/images/" + picturename}
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                  width={200}
                  height={200}
                />
              )}
              <input
                type="file"
                id="files"
                className="hidden"
                onChange={onUploadFile}
              ></input>
              <label htmlFor="files" className="border-2">
                이미지 선택
              </label>
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
110
          </div>
111
112
113
114
115
116
117
118
          <div className="flex border-0 border-t-2">
            <div className="py-10 basis-1/5 border-0 border-r-2 bg-gray-100 grid place-items-center">
              별명
            </div>
            <div className="basis-full ">
              <input
                placeholder="빈칸"
                className="basis-full placeholder:text-black my-10 ml-5"
Kim, MinGyu's avatar
Kim, MinGyu committed
119
                onChange={onNickChange}
120
121
              />
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
122
123
          </div>
        </div>
124
125
126
127
        <div className="grid justify-items-center mb-20">
          <button onClick={handleClick} className=" border-2 ">
            저장하기
          </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
128
129
130
          <button onClick={deleteClick} className="border-2">
            계정 삭제
          </button>
131
132
        </div>
      </form>
Kim, MinGyu's avatar
Kim, MinGyu committed
133
134
135
    </div>
  );
}