ProfilePage.js 5.83 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';
JeongYeonwoo's avatar
   
JeongYeonwoo committed
4
import axios from 'axios'
JeongYeonwoo's avatar
JeongYeonwoo committed
5
6
import catchErrors from '../utils/catchErrors'
import { isAuthenticated } from '../utils/auth';
우지원's avatar
ul    
우지원 committed
7

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

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

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

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

JeongYeonwoo's avatar
JeongYeonwoo committed
25
26
27
28
29
30
31
    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
32
    }
우지원's avatar
ul    
우지원 committed
33

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

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

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

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

87
88


우지원's avatar
ul    
우지원 committed
89
    return (
JeongYeonwoo's avatar
JeongYeonwoo committed
90
        <>
Choi Ga Young's avatar
Choi Ga Young committed
91
            <Menu />
JeongYeonwoo's avatar
JeongYeonwoo committed
92
93
            <Container>
                <Row >
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
94
                    <Col sm={4}>
JeongYeonwoo's avatar
JeongYeonwoo committed
95
                        <Row className='justify-content-center'>
96
97
                            {!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
98
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
99
100
                        <Row className='ml-3 mt-3 justify-content-center'>
                            <Form className="d-flex">
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
101
                                <Form.Group>
JeongYeonwoo's avatar
JeongYeonwoo committed
102
                                    <Form.Label>프로필 사진 변경</Form.Label>
JeongYeonwoo's avatar
JeongYeonwoo committed
103
                                    <Form.Control type='file' name='imageUrl' onChange={handleChange} accept='image/*' />
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
104
105
106
107
108
                                </Form.Group>
                            </Form>
                        </Row>
                    </Col>
                    <Col sm={8}>
JeongYeonwoo's avatar
JeongYeonwoo committed
109
110
                        <Row className='m-5 justify-content-center'>
                            <h2>{user.username}님의 프로필 정보</h2>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
111
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
112
113
114
                        <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
115
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
116
117
118
119
120
121
122
                        <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
123
                                    <Form.Control defaultValue={user.nickname} name='nickname' style={{ width: "110%" }} onChange={handleChange} />
JeongYeonwoo's avatar
JeongYeonwoo committed
124
125
126
                                </Form>
                            </Col>
                            <Col xs={3}>
JeongYeonwoo's avatar
JeongYeonwoo committed
127
                                <Form className="d-flex" onSubmit={handleSubmitHidVis}>
우지원's avatar
우지원 committed
128
                                    <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
129
130
                                </Form>
                            </Col>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
131
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133
134
135
136
                        <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
137
                            <Form onSubmit={handleSubmit}>
Choi Ga Young's avatar
Choi Ga Young committed
138
                                <Button size="sm" className="mr-4" type='submit' variant="outline" style={{ border: "3px solid", borderColor: "#9174ad", background: 'white' }}>저장</Button>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
139
                                <Button href="/" size="sm" className="ml-4" variant="outline" style={{ border: "3px solid", borderColor: "#9174ad", background: 'white' }}> 화면으로</Button>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
140
141
142
143
144
                            </Form>
                        </Row>
                    </Col>
                </Row>
            </Container>
JeongYeonwoo's avatar
JeongYeonwoo committed
145
        </>
우지원's avatar
ul    
우지원 committed
146
147
148
149
    )
}

export default ProfilePage