profile.tsx 3.53 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
5
6

export default function Profile() {
  // 로컬 저장소에는 로그인 여부만 저장
7
8
9
10
11
  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
12

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

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    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);
    }
  };

  const userChange = async () => {
    const profile = await handleProfile();
    setEmail(profile.email);
    setPicturename(profile.newfilename);
  };
  const handleClick = async (
    e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent>
  ) => {
    const formdata = new FormData();
    if (!(file === undefined || file === null)) {
      formdata.append("picture", file);
    }
    await profileApi.picture(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
52
53
54
  };

  useEffect(() => {
55
    userChange();
Kim, MinGyu's avatar
Kim, MinGyu committed
56
57
58
59
  }, []);

  return (
    <div className="grid ">
60
61
62
63
64
65
66
67
      <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
68
          </div>
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
          <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
                  src={"http://localhost:3000/" + picturename}
                  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
98
          </div>
99
100
101
102
103
104
105
106
107
108
          <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"
              />
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
109
110
          </div>
        </div>
111
112
113
114
115
116
        <div className="grid justify-items-center mb-20">
          <button onClick={handleClick} className=" border-2 ">
            저장하기
          </button>
        </div>
      </form>
Kim, MinGyu's avatar
Kim, MinGyu committed
117
118
119
    </div>
  );
}