profile.tsx 3.76 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
    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
39
40
41
42
43
  const onNickChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const nickname = e.target.value;
    setNickname(nickname);
  };

44
45
46
  const userChange = async () => {
    const profile = await handleProfile();
    setEmail(profile.email);
Kim, MinGyu's avatar
Kim, MinGyu committed
47
    setPicturename(profile.avatar.newfilename);
48
49
50
51
52
53
54
55
  };
  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
56
    formdata.append("nickname", nickname);
57
    await profileApi.picture(formdata);
Kim, MinGyu's avatar
Kim, MinGyu committed
58
59
60
  };

  useEffect(() => {
61
    userChange();
Kim, MinGyu's avatar
Kim, MinGyu committed
62
63
64
65
  }, []);

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