ProfilePage.js 11.9 KB
Newer Older
JeongYeonwoo's avatar
JeongYeonwoo committed
1
import React, { useState, useEffect } from 'react';
우지원's avatar
ul    
우지원 committed
2
import ReactDOM from 'react-dom';
Choi Ga Young's avatar
Choi Ga Young committed
3
import Menu from '../Components/Menu';
JeongYeonwoo's avatar
JeongYeonwoo committed
4
import { Image, Button, Container, Form, FormControl, Navbar, Nav, Row, Col, Dropdown, Carousel } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
5
import { BrowserRouter as Router, Route, Redirect, Switch, Link } from 'react-router-dom';
JeongYeonwoo's avatar
   
JeongYeonwoo committed
6
import axios from 'axios'
우지원's avatar
ul    
우지원 committed
7
8

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

JeongYeonwoo's avatar
   
JeongYeonwoo committed
14

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

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

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


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

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

    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
59
60
            img.setAttribute("style", 'height:300px; width:300px')
            img.setAttribute("class", "mb-3")
우지원's avatar
ul    
우지원 committed
61
62
63
64
65
66
67
68
69
70
71
            // 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
72
            let del2 = document.getElementById('profileImg')
우지원's avatar
ul    
우지원 committed
73
74
75
76
77
78
            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
79
                img.setAttribute("style", 'height:300px; width:300px')
우지원's avatar
ul    
우지원 committed
80
81
82
83
                document.querySelector("div#image_container").appendChild(img);
            };
        }
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
84
    useEffect(() => {
우지원's avatar
a    
우지원 committed
85
        if (sessionStorage.getItem('user')) { //id뿐만아니라 토큰같은거를 확인 못하나 + 이런식으로 확인해도 되는것도 맞나
JeongYeonwoo's avatar
JeongYeonwoo committed
86
87
88
89
90
91
92
            getLoginedUser()
        }
        else {
            alert("로그인 후 이용하세요")
            window.location.href = '/login'
        }
    }, [])
JeongYeonwoo's avatar
   
JeongYeonwoo committed
93

우지원's avatar
ul    
우지원 committed
94
95
    return (
        <div>
Choi Ga Young's avatar
Choi Ga Young committed
96
            <Menu />
JeongYeonwoo's avatar
0113    
JeongYeonwoo committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
            <Container>
                <Row><h1>ProfilePage</h1></Row>
                <Row><h2>{user.username} 환영합니다 !</h2></Row>
            </Container>
            <Container className="mt-5 shadow w-75">
                <Row>
                    <Col sm={4}>
                        <Row>
                            <div className="d-flex justify-content-center mb-3" id="defaultImg">
                                <Image src={userimg} width="300px" roundedCircle />
                            </div>
                        </Row>
                        <Row>
                            <Form className="d-flex justify-content-center">
                                <Form.Group>
                                    <div id="image_container"></div>
                                    <Form.File label="프로필 사진 변경" accept="image/*" onChange={setThumbnail} />
                                </Form.Group>
                            </Form>
                        </Row>
                        <Row className="d-flex justify-content-center">
                            <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}>

                        <Row className="m-3 justify-content-flex-start" style={{ fontWeight: "bold", fontSize: "x-large" }}>
                            <Col sm={2.5}>이름 :</Col>
                            <Col sm={8}>{user.username}</Col>
                        </Row>
                        <Row className="m-3 justify-content-flex-start" id="nickname">
                            <Form className="d-flex" onSubmit={handleSubmit} style={{ fontWeight: "bold", fontSize: "x-large" }}>

                                <Row>
                                    <Col>별명 :</Col>
                                    <Col hidden={!hidden}>
                                        {user.nickname}
                                    </Col>
                                    <Col hidden={hidden}>
                                        <Form.Control defaultValue={user.nickname} style={{width : "110%"}} onChange={handleChange} />
                                    </Col>
                                    <Col>
                                        <Button className="ml-3 d-flex justify-content-end" variant="outline-primary" size="sm" type='submit'>수정</Button>
                                    </Col>
                                </Row>
                            </Form>

                        </Row>
                        <Row className="m-3" style={{ fontWeight: "bold", fontSize: "x-large" }}>
                            <Col sm={2.5}>이메일 : </Col>
                            <Col sm={8}>{user.email}</Col>
                        </Row>
                        <Row className='justify-content-center'>
                            <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
173
174
175
            <Container className="d-flex justify-content-center">
                <div className="mt-5 shadow w-75">
                    <h1 className="text-center mt-4 ml-5 mr-5 mb-3">Profile Page</h1>
JeongYeonwoo's avatar
JeongYeonwoo committed
176
                    <h4 className="text-center mb-5">{user.username} 환영합니다 !</h4>
우지원's avatar
ul    
우지원 committed
177
178

                    <div className="d-flex justify-content-center mb-3" id="defaultImg">
JeongYeonwoo's avatar
JeongYeonwoo committed
179
                        <Image src={userimg} width="300px" roundedCircle />
우지원's avatar
ul    
우지원 committed
180
                    </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
181
182
                    <Row className="d-flex justify-content-center">

우지원's avatar
ul    
우지원 committed
183
184
185
186
187
188
                        <Form className="d-flex justify-content-center">
                            <Form.Group>
                                <div id="image_container"></div>
                                <Form.File label="프로필 사진 변경" accept="image/*" onChange={setThumbnail} />
                            </Form.Group>
                        </Form>
JeongYeonwoo's avatar
JeongYeonwoo committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                    </Row>
                    <Row className="d-flex justify-content-center">
                        <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>
                    {/* <Carousel className="d-flex justify-content-center" style={{ width: "300px", height: "300px" }}>
                        <Carousel.Item className="d-flex justify-content-center">
                            <Image
                                className="d-block w-100"
                                src={img1}
                            />
                            <Carousel.Caption>
                                <p style={{color : 'black'}}>펭수입니다.</p>
                            </Carousel.Caption>
                        </Carousel.Item>
                        <Carousel.Item>
                            <Image
                                className="d-block w-100"
                                src={img2}
                            />
                            <Carousel.Caption>
                                <p>라이언입니다.</p>
                            </Carousel.Caption>
                        </Carousel.Item>
                        <Carousel.Item>
                            <Image
                                className="d-block w-100"
                                src={img3}
                            />
                            <Carousel.Caption>
                                <p>어피치 입니다.</p>
                            </Carousel.Caption>
                        </Carousel.Item>
                    </Carousel> */}
우지원's avatar
ul    
우지원 committed
231
                    <div className="text-center">
JeongYeonwoo's avatar
JeongYeonwoo committed
232
                        <div className="m-3" style={{ fontWeight: "bold", fontSize: "x-large" }}>이름 : {user.username}</div>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
233
                        <div className="m-3" id="nickname">
JeongYeonwoo's avatar
JeongYeonwoo committed
234
                            <Form className="d-flex justify-content-center" onSubmit={handleSubmit} style={{ fontWeight: "bold", fontSize: "x-large" }}>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
235
                                별명 :
JeongYeonwoo's avatar
JeongYeonwoo committed
236
                                <div hidden={!hidden}>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
237
238
                                    {user.nickname}
                                </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
239
240
                                <Form.Control defaultValue={user.nickname} style={{ width: "40%" }} size="sm" onChange={handleChange} hidden={hidden} />
                                <Button className="ml-3" variant="outline-primary" size="sm" type='submit'>수정</Button>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
241
242
                            </Form>

우지원's avatar
ul    
우지원 committed
243
                        </div>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
244
                        <div className="m-3" style={{ fontWeight: "bold", fontSize: "x-large" }}>이메일 : {user.email}</div>
우지원's avatar
ul    
우지원 committed
245
246
                    </div>
                    <div className="text-center m-5">
JeongYeonwoo's avatar
JeongYeonwoo committed
247
                        <Form>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
248
249
250
251
                            <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>
JeongYeonwoo's avatar
JeongYeonwoo committed
252
                        </Form>
우지원's avatar
ul    
우지원 committed
253
254
255
256
257
258
259
260
261
                    </div>
                </div>
            </Container>

        </div>
    )
}

export default ProfilePage