import React, { useEffect, useState } from "react"; import { Profile } from "../types"; import { profileApi } from "../apis"; import { useAuth } from "../auth/auth.context"; export default function Profile() { // 로컬 저장소에는 로그인 여부만 저장 const [email, setEmail] = useState(""); const [picturename, setPicturename] = useState(""); const [file, setFile] = useState(); const [imageSrc, setImageSrc] = useState(""); const [nickname, setNickname] = useState(""); const [placeholder, setPlaceholder] = useState(""); const { logout } = useAuth(); const handleProfile = async () => { const user: Profile = await profileApi.profile(); return user; }; const PictureSrc = (fileBlob: Blob) => { const reader = new FileReader(); reader.readAsDataURL(fileBlob); reader.onload = (data) => { if (typeof data.target?.result === "string") { console.log(data.target?.result); setImageSrc(data.target?.result); } }; }; const onUploadFile = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!(file === undefined)) { setFile(file); PictureSrc(file); } }; const onNickChange = (e: React.ChangeEvent) => { const newnickname = e.target.value; setNickname(newnickname); }; const userChange = async () => { const profile = await handleProfile(); setEmail(profile.email); setPicturename(profile.avatar.newfilename); setPlaceholder(profile.avatar.nickname); }; const handleClick = async ( e: React.MouseEvent ) => { const formdata = new FormData(); if (!(file === undefined || nickname === "")) { formdata.append("picture", file); formdata.append("nickname", nickname); console.log("both"); await profileApi.picture(formdata); } else if (!(nickname === "")) { formdata.append("nickname", nickname); console.log("picture"); await profileApi.picture(formdata); } else { alert("수정할 정보를 입력해주세요."); } }; const deleteClick = async () => { await profileApi.deleteUser().then(() => { logout(); console.log("test"); }); }; useEffect(() => { userChange(); }, []); return (
프로필 수정
Email
{email}
사진
{imageSrc ? ( ) : ( )}
별명
); }