import React, { useState, useEffect } from 'react' import Menu from '../Components/Menu'; import { Image, Button, Container, Form, Row, Col } from 'react-bootstrap'; import { BrowserRouter as Link } from 'react-router-dom'; import axios from 'axios' import catchErrors from '../utils/catchErrors' import { isAuthenticated } from '../utils/auth'; const INIT_USER = { username: '', email: '', nickname: '', imageUrl: [] } function ProfilePage() { const [user, setUser] = useState(INIT_USER) const [error, setError] = useState('') const [hidden, setHidden] = useState(true) const [changed, setChanged] = useState(false) const userId = isAuthenticated() async function getProfile(userId) { try { const response = await axios.get(`/users/${userId}`) setUser(response.data) } catch (error) { catchErrors(error, setError) } } function handleSubmitHidVis(e) { e.preventDefault() if (hidden) { setHidden(false) } else { setHidden(true) } } function handleChange(e) { const { name, value, files } = e.target if (files) { setUser({ ...user, [name]: files }) } else { setUser({ ...user, [name]: value }) } setChanged(true) } async function handleSubmit(e) { e.preventDefault() if (changed) { const formData = new FormData() if (user.imageUrl) { formData.append('imageUrl', user.imageUrl[0]) } formData.append('newNickname', user.nickname) //얘네는 req.body로 들어감 try { if (userId) { await axios.put(`/users/${userId}`, formData) alert('저장되었습니다.') window.location.reload() } } catch (error) { catchErrors(error, setError) } } else { alert('변경사항이 없습니다.') } } useEffect(() => { getProfile(userId) }, [userId]) return ( <> {user.profileimg ? : }
프로필 사진 변경

{user.username}님의 프로필 정보

이름 : {user.username} 별명 : {user.nickname}
이메일 : {user.email}
) } export default ProfilePage