ProfilePage.js 5.64 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';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
3
import styled from 'styled-components';
4
import { Image, Button, Container, Form, Row, Col } from 'react-bootstrap';
Choi Ga Young's avatar
Choi Ga Young committed
5
import { BrowserRouter as Link } from 'react-router-dom';
JeongYeonwoo's avatar
   
JeongYeonwoo committed
6
import axios from 'axios'
JeongYeonwoo's avatar
JeongYeonwoo committed
7
8
import catchErrors from '../utils/catchErrors'
import { isAuthenticated } from '../utils/auth';
우지원's avatar
ul    
우지원 committed
9

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

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

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

JeongYeonwoo's avatar
JeongYeonwoo committed
75
    useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
76
77
        getProfile(userId)
    }, [userId])
JeongYeonwoo's avatar
   
JeongYeonwoo committed
78

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

export default ProfilePage