import React, { useState, useEffect } from 'react' import Menu from '../Components/Menu'; import { Image, Button, Container, Form, Row, Col, Dropdown } 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]) // const [img, setImg] = useState('') // function bbb(e) { // const { name } = e.target // let reader = new FileReader(); // reader.onload = function (e){ // console.log(e.target.result) // // setImg(e.target.result) // // setProfileimg // // console.log(user,name) // setUser({...user, [name]: e.target.result}) // }; // reader.readAsDataURL(e.target.files[0]) // ) return ( <>