ProfilePage.js 5.97 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react'
Choi Ga Young's avatar
Choi Ga Young committed
2
import Menu from '../Components/Menu';
3
import { Image, Button, Container, Form, Row, Col } from 'react-bootstrap';
Choi Ga Young's avatar
Choi Ga Young committed
4
import { BrowserRouter as Link } from 'react-router-dom';
JeongYeonwoo's avatar
   
JeongYeonwoo committed
5
import axios from 'axios'
JeongYeonwoo's avatar
JeongYeonwoo committed
6
7
import catchErrors from '../utils/catchErrors'
import { isAuthenticated } from '../utils/auth';
우지원's avatar
ul    
우지원 committed
8

JeongYeonwoo's avatar
JeongYeonwoo committed
9
10
11
12
13
14
const INIT_USER = {
    username: '',
    email: '',
    nickname: '',
    imageUrl: []
}
우지원's avatar
ul    
우지원 committed
15

JeongYeonwoo's avatar
   
JeongYeonwoo committed
16
function ProfilePage() {
JeongYeonwoo's avatar
JeongYeonwoo committed
17
18
    const [user, setUser] = useState(INIT_USER)
    const [error, setError] = useState('')
우지원's avatar
ul    
우지원 committed
19

JeongYeonwoo's avatar
   
JeongYeonwoo committed
20
    const [hidden, setHidden] = useState(true)
JeongYeonwoo's avatar
JeongYeonwoo committed
21
    const [changed, setChanged] = useState(false)
22
    const [selectedImg, setSelectedImg] = useState('')
우지원's avatar
ul    
우지원 committed
23

JeongYeonwoo's avatar
JeongYeonwoo committed
24
    const userId = isAuthenticated()
우지원's avatar
ul    
우지원 committed
25

JeongYeonwoo's avatar
JeongYeonwoo committed
26
27
28
29
30
31
32
    async function getProfile(userId) {
        try {
            const response = await axios.get(`/users/${userId}`)
            setUser(response.data)
        } catch (error) {
            catchErrors(error, setError)
        }
JeongYeonwoo's avatar
   
JeongYeonwoo committed
33
    }
우지원's avatar
ul    
우지원 committed
34

JeongYeonwoo's avatar
JeongYeonwoo committed
35
    function handleSubmitHidVis(e) {
JeongYeonwoo's avatar
JeongYeonwoo committed
36
        e.preventDefault()
JeongYeonwoo's avatar
   
JeongYeonwoo committed
37
38
39
40
41
42
        if (hidden) {
            setHidden(false)
        } else {
            setHidden(true)
        }
    }
43

JeongYeonwoo's avatar
JeongYeonwoo committed
44
    function handleChange(e) {
JeongYeonwoo's avatar
JeongYeonwoo committed
45
46
47
48
49
        const { name, value, files } = e.target
        if (files) {
            setUser({ ...user, [name]: files })
        } else {
            setUser({ ...user, [name]: value })
우지원's avatar
ul    
우지원 committed
50
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
51
        setChanged(true)
52
53
54
55
56
57
58
59

        if (files) {
            let reader = new FileReader()
            reader.onload = function (e) {
                setSelectedImg(e.target.result)
            }
            reader.readAsDataURL(e.target.files[0])
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
60
61
62
63
64
    }
    async function handleSubmit(e) {
        e.preventDefault()
        if (changed) {
            const formData = new FormData()
JeongYeonwoo's avatar
JeongYeonwoo committed
65
66
67
68
            if (user.imageUrl) {
                formData.append('imageUrl', user.imageUrl[0])
            }
            formData.append('newNickname', user.nickname)   //얘네는 req.body로 들어감
JeongYeonwoo's avatar
JeongYeonwoo committed
69
            try {
JeongYeonwoo's avatar
JeongYeonwoo committed
70
                if (userId) {
JeongYeonwoo's avatar
JeongYeonwoo committed
71
72
73
74
75
76
77
                    await axios.put(`/users/${userId}`, formData)
                    alert('저장되었습니다.')
                    window.location.reload()
                }
            } catch (error) {
                catchErrors(error, setError)
            }
JeongYeonwoo's avatar
JeongYeonwoo committed
78
79
        }
        else {
JeongYeonwoo's avatar
JeongYeonwoo committed
80
            alert('변경사항이 없습니다.')
우지원's avatar
ul    
우지원 committed
81
82
        }
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
83

JeongYeonwoo's avatar
JeongYeonwoo committed
84
    useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
85
86
        getProfile(userId)
    }, [userId])
JeongYeonwoo's avatar
   
JeongYeonwoo committed
87

88
89


우지원's avatar
ul    
우지원 committed
90
    return (
JeongYeonwoo's avatar
JeongYeonwoo committed
91
        <>
Choi Ga Young's avatar
Choi Ga Young committed
92
            <Menu />
JeongYeonwoo's avatar
JeongYeonwoo committed
93
94
            <Container>
                <Row >
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
95
                    <Col sm={4}>
JeongYeonwoo's avatar
JeongYeonwoo committed
96
                        <Row className='justify-content-center'>
97
98
                            {!selectedImg ? <Image src={user.profileimg && `/images/${user.profileimg}`} style={{ width: "300px", height: "300px" }} roundedCircle /> :
                                <Image src={selectedImg} style={{ width: "300px", height: "300px" }} roundedCircle />}
JeongYeonwoo's avatar
JeongYeonwoo committed
99
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
100
101
                        <Row className='ml-3 mt-3 justify-content-center'>
                            <Form className="d-flex">
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
102
                                <Form.Group>
JeongYeonwoo's avatar
JeongYeonwoo committed
103
                                    <Form.Label>프로필 사진 변경</Form.Label>
JeongYeonwoo's avatar
JeongYeonwoo committed
104
                                    <Form.Control type='file' name='imageUrl' onChange={handleChange} accept='image/*' />
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
105
106
107
108
109
                                </Form.Group>
                            </Form>
                        </Row>
                    </Col>
                    <Col sm={8}>
JeongYeonwoo's avatar
JeongYeonwoo committed
110
111
                        <Row className='m-5 justify-content-center'>
                            <h2>{user.username}님의 프로필 정보</h2>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
112
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
113
114
115
                        <Row className="m-3 justify-content-flex-start" style={{ fontWeight: "bold", fontSize: "large" }}>
                            <Col xs={3}>이름 :</Col>
                            <Col xs={6}>{user.username}</Col>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
116
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
117
118
119
120
121
122
123
                        <Row className="m-3 justify-content-flex-start" id="nickname" style={{ fontWeight: "bold", fontSize: "large" }}>
                            <Col xs={3}>별명 :</Col>
                            <Col xs={6} hidden={!hidden}>
                                {user.nickname}
                            </Col>
                            <Col xs={6} hidden={hidden}>
                                <Form>
JeongYeonwoo's avatar
JeongYeonwoo committed
124
                                    <Form.Control defaultValue={user.nickname} name='nickname' style={{ width: "110%" }} onChange={handleChange} />
JeongYeonwoo's avatar
JeongYeonwoo committed
125
126
127
                                </Form>
                            </Col>
                            <Col xs={3}>
JeongYeonwoo's avatar
JeongYeonwoo committed
128
                                <Form className="d-flex" onSubmit={handleSubmitHidVis}>
우지원's avatar
우지원 committed
129
                                    <Button className="ml-3 d-flex justify-content-end" variant="outline" style={{ border: "3px solid", borderColor: "#b49dc9", background: 'white' }} size="sm" type='submit'>수정</Button>
JeongYeonwoo's avatar
JeongYeonwoo committed
130
131
                                </Form>
                            </Col>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
132
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
133
134
135
136
137
                        <Row className="m-3" style={{ fontWeight: "bold", fontSize: "large" }}>
                            <Col xs={3}>이메일 : </Col>
                            <Col xs={6}>{user.email}</Col>
                        </Row>
                        <Row className='m-3 justify-content-center'>
JeongYeonwoo's avatar
JeongYeonwoo committed
138
                            <Form onSubmit={handleSubmit}>
Choi Ga Young's avatar
Choi Ga Young committed
139
                                <Button size="sm" className="mr-4" type='submit' variant="outline" style={{ border: "3px solid", borderColor: "#9174ad", background: 'white' }}>저장</Button>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
140
                                <Link to='/'>
Choi Ga Young's avatar
Choi Ga Young committed
141
                                    <Button size="sm" className="ml-4" variant="outline" style={{ border: "3px solid", borderColor: "#9174ad", background: 'white' }}> 화면으로</Button>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
142
143
144
145
146
147
                                </Link>
                            </Form>
                        </Row>
                    </Col>
                </Row>
            </Container>
JeongYeonwoo's avatar
JeongYeonwoo committed
148
        </>
우지원's avatar
ul    
우지원 committed
149
150
151
152
    )
}

export default ProfilePage