ProfilePage.js 7.01 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';
Choi Ga Young's avatar
Choi Ga Young committed
3
4
import { Image, Button, Container, Form, Row, Col, Dropdown } from 'react-bootstrap';
import { BrowserRouter as Link } from 'react-router-dom';
JeongYeonwoo's avatar
   
JeongYeonwoo committed
5
import axios from 'axios'
우지원's avatar
ul    
우지원 committed
6

JeongYeonwoo's avatar
JeongYeonwoo committed
7
8
9
10
import userdefault from '../Images/KakaoTalk_20201230_153151693.png'
import img1 from '../Images/img_1.png'
import img2 from '../Images/img_2.png'
import img3 from '../Images/img_3.jpg'
JeongYeonwoo's avatar
JeongYeonwoo committed
11
import DropdownItem from 'react-bootstrap/esm/DropdownItem';
우지원's avatar
ul    
우지원 committed
12

JeongYeonwoo's avatar
   
JeongYeonwoo committed
13

JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
14
15
16
17
18
// const INIT_USER = {
//     username: '',
//     email: '',
//     nickname: ''
// }
우지원's avatar
ul    
우지원 committed
19

JeongYeonwoo's avatar
   
JeongYeonwoo committed
20
function ProfilePage() {
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
21
    const [user, setUser] = useState('')
우지원's avatar
ul    
우지원 committed
22

JeongYeonwoo's avatar
JeongYeonwoo committed
23
    const [userimg, setUserimg] = useState(img2)
JeongYeonwoo's avatar
   
JeongYeonwoo committed
24
25
    const [defaultImg, setDefaultImg] = useState(true)
    const [hidden, setHidden] = useState(true)
우지원's avatar
ul    
우지원 committed
26
27


JeongYeonwoo's avatar
JeongYeonwoo committed
28
    async function getLoginedUser() { //email로 db에서 찾아오기 ㅇㅇㅇㅇㅇ
Choi Ga Young's avatar
Choi Ga Young committed
29
        const userid = sessionStorage.getItem('userId')
JeongYeonwoo's avatar
JeongYeonwoo committed
30
        const response = await axios.post(`/users/${userid}`, { 'email': userid })
JeongYeonwoo's avatar
   
JeongYeonwoo committed
31
32
        setUser(response.data)
    }
우지원's avatar
ul    
우지원 committed
33

JeongYeonwoo's avatar
JeongYeonwoo committed
34
35
    function handleSubmit(e) {
        e.preventDefault()
JeongYeonwoo's avatar
   
JeongYeonwoo committed
36
37
38
39
40
41
        if (hidden) {
            setHidden(false)
        } else {
            setHidden(true)
        }
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
42
43
44
45
    function handleChange(e) {
        setUser({ ...user, 'nickname': e.target.value })
    }
    async function handleNicksave() {
Choi Ga Young's avatar
Choi Ga Young committed
46
        const userid = sessionStorage.getItem('userId')
JeongYeonwoo's avatar
JeongYeonwoo committed
47
        await axios.put(`/users/${userid}`, user)
우지원's avatar
ul    
우지원 committed
48
49
50
51
52
53
54
55
56
    }

    function setThumbnail(event) {  //불러온 사진 <div id='image_container'>에 띄우기
        let reader = new FileReader();

        reader.onload = function (event) {
            let img = document.createElement("img");
            img.setAttribute("src", event.target.result);
            img.setAttribute("id", "profileImg")
JeongYeonwoo's avatar
   
JeongYeonwoo committed
57
58
            img.setAttribute("style", 'height:300px; width:300px')
            img.setAttribute("class", "mb-3")
우지원's avatar
ul    
우지원 committed
59
60
61
62
63
64
65
66
67
68
69
            // img.setAttribute("class","d-flex justify-content-center mb-3")
            document.querySelector("div#image_container").appendChild(img);
        };
        reader.readAsDataURL(event.target.files[0]);

        if (defaultImg) {   //첫 이미지 업로드(default이미지 지우고 유저가 올린걸로 업로드)
            let del = document.getElementById("defaultImg")
            del.remove()
            setDefaultImg(false)
        }
        else {  //기존에 올렸던 사진 지우고 재선택한 사진 업로드
JeongYeonwoo's avatar
   
JeongYeonwoo committed
70
            let del2 = document.getElementById('profileImg')
우지원's avatar
ul    
우지원 committed
71
72
73
74
75
76
            del2.remove()

            reader.onload = function (event) {
                let img = document.createElement("img");
                img.setAttribute("src", event.target.result);
                img.setAttribute("id", "profileImg")
JeongYeonwoo's avatar
   
JeongYeonwoo committed
77
                img.setAttribute("style", 'height:300px; width:300px')
우지원's avatar
ul    
우지원 committed
78
79
80
81
                document.querySelector("div#image_container").appendChild(img);
            };
        }
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
82
    useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
83
        getLoginedUser()
JeongYeonwoo's avatar
JeongYeonwoo committed
84
    }, [])
JeongYeonwoo's avatar
   
JeongYeonwoo committed
85

우지원's avatar
ul    
우지원 committed
86
87
    return (
        <div>
Choi Ga Young's avatar
Choi Ga Young committed
88
            <Menu />
JeongYeonwoo's avatar
JeongYeonwoo committed
89
            <Container className='border' fluid>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
90
91
                <Row>
                    <Col sm={4}>
JeongYeonwoo's avatar
JeongYeonwoo committed
92
93
                        <Row className='justify-content-center'>
                            <div className="d-flex ml-3 mt-3" id="defaultImg">
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
94
95
96
                                <Image src={userimg} width="300px" roundedCircle />
                            </div>
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
97
98
                        <Row className='ml-3 mt-3 justify-content-center'>
                            <Form className="d-flex">
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
99
100
101
102
103
104
                                <Form.Group>
                                    <div id="image_container"></div>
                                    <Form.File label="프로필 사진 변경" accept="image/*" onChange={setThumbnail} />
                                </Form.Group>
                            </Form>
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
105
                        <Row className="d-flex justify-content-center mb-3">
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
106
107
108
109
110
111
112
113
114
115
116
117
118
                            <Dropdown>
                                <Dropdown.Toggle variant='success' id='dropdown-basic'>
                                    프로필 사진 선택
                                </Dropdown.Toggle>
                                <Dropdown.Menu>
                                    <Dropdown.Item as='button'>홈으로</Dropdown.Item>
                                    <Dropdown.Item href='/'>라이언</Dropdown.Item>
                                    <Dropdown.Item href='/'>어피치</Dropdown.Item>
                                </Dropdown.Menu>
                            </Dropdown>
                        </Row>
                    </Col>
                    <Col sm={8}>
JeongYeonwoo's avatar
JeongYeonwoo committed
119
120
                        <Row className='m-5 justify-content-center'>
                            <h2>{user.username}님의 프로필 정보</h2>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
121
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
122
123
124
125
                        <Row className="m-3 justify-content-flex-start" style={{ fontWeight: "bold", fontSize: "large" }}>
                            <Col xs={3}>이름 :</Col>
                            <Col xs={6}>{user.username}</Col>
                            {/* <Col sm={4}></Col> */}
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
126
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
                        <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>
                                    <Form.Control defaultValue={user.nickname} style={{ width: "110%" }} onChange={handleChange} />
                                </Form>
                            </Col>
                            <Col xs={3}>
                                <Form className="d-flex" onSubmit={handleSubmit}>
                                    <Button className="ml-3 d-flex justify-content-end" variant="outline-primary" size="sm" type='submit'>수정</Button>
                                </Form>
                            </Col>
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
142
                        </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
143
144
145
146
147
                        <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
0113    
JeongYeonwoo committed
148
149
150
151
152
153
154
155
156
157
                            <Form>
                                <Button variant="outline-success" size="sm" className="mr-4" onClick={handleNicksave}>저장</Button>
                                <Link to='/'>
                                    <Button variant="outline-success" size="sm" className="ml-4" > 화면으로</Button>
                                </Link>
                            </Form>
                        </Row>
                    </Col>
                </Row>
            </Container>
우지원's avatar
ul    
우지원 committed
158
159
160
161
162
        </div>
    )
}

export default ProfilePage