ProfilePage.js 7.53 KB
Newer Older
우지원's avatar
ul    
우지원 committed
1
2
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
Choi Ga Young's avatar
Choi Ga Young committed
3
import Menu from '../Components/Menu';
우지원's avatar
ul    
우지원 committed
4
5
import { Image, Button, Container, Form, FormControl, Navbar, Nav } from 'react-bootstrap';
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
9

import userdefault from './KakaoTalk_20201230_153151693.png'

JeongYeonwoo's avatar
   
JeongYeonwoo committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//localStorage에 아이디? 가있는걸로 db에서 그 사람꺼 찾은(findOne 이런걸로) 다음에 닉네임도 불러오고 여기서 수정할 수 있는 기능을 만들자!


////////////////////////////////////////////////////////////////////////////////
// async function sending() {
//     const info = await fetch(`/api/users/signup`, {
//         method: "POST",
//         headers: {
//             "Content-type": 'application/json'
//         },
//         body: JSON.stringify({
//             name: userName,
//             email: userEmail,
//             password: "123123123",
//             nickname: userNickname
//         })
//     })
//     const data = await info.json()
//     console.log(data)
// }

////////////////////////////////////////////////////////////////////////////////

const INIT_USER = {
    name: '',
    email: '',
    nickname: ''
}
우지원's avatar
ul    
우지원 committed
38

JeongYeonwoo's avatar
   
JeongYeonwoo committed
39
40
function ProfilePage() {
    const [user, setUser] = useState(INIT_USER)
우지원's avatar
ul    
우지원 committed
41

JeongYeonwoo's avatar
   
JeongYeonwoo committed
42
43
    const [defaultImg, setDefaultImg] = useState(true)
    const [hidden, setHidden] = useState(true)
우지원's avatar
ul    
우지원 committed
44

JeongYeonwoo's avatar
   
JeongYeonwoo committed
45
46
47
48
49
50
51
52
53
    function setlocal() {
        localStorage.setItem('_id', '5ff77d56da609732c001b392')
    }
    async function loginedinfo() {
        const userid = localStorage.getItem('_id')
        const response = await axios.post(`/api/users/${userid}`, user)
        setUser(response.data[1])
        console.log(user)
    }
우지원's avatar
ul    
우지원 committed
54

JeongYeonwoo's avatar
   
JeongYeonwoo committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    
    async function getInfo() {  //db에서 로그인 정보 가져옴
        const response = await axios.get('/api/users')
        console.log(response.data)
        setUser(response.data[1])
        console.log(user)
    }
    async function makeLogin() {    //로그인 상태로 만들어줌
        const info = await axios.get('/api/users')
        console.log('info', info.data)
        setUser(info.data[1])

        await axios.post(`/api/users/`, user)
        localStorage.setItem('loginStatus', 'true')
        localStorage.setItem('_id', user._id)
        console.log(user)
    }
우지원's avatar
ul    
우지원 committed
72

JeongYeonwoo's avatar
   
JeongYeonwoo committed
73
74
75
76
77
78
    async function authinfo() { //id로 db에서 찾아오기
        const userid = { '_id': localStorage.getItem('_id') }
        const response = await axios.post(`/api/users/${userid._id}`, userid)
        console.log(response.data)
        setUser(response.data)
    }
우지원's avatar
ul    
우지원 committed
79

JeongYeonwoo's avatar
   
JeongYeonwoo committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    //@@그다음에 찾아온걸로 다시 띄우기
    //@@닉네임 수정하기

    function handleEditnick() {
        if (hidden) {
            setHidden(false)
        } else {
            setUser({ ...user, 'nickname': document.getElementById("inputNick").value })
            setHidden(true)
        }
    }
    async function handleNicksave(){
        const userid = { '_id': localStorage.getItem('_id') }
        await axios.put(`/api/users/${userid._id}`, user)
우지원's avatar
ul    
우지원 committed
94
95
96
97
98
99
100
101
102
103

    }

    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
104
105
            img.setAttribute("style", 'height:300px; width:300px')
            img.setAttribute("class", "mb-3")
우지원's avatar
ul    
우지원 committed
106
107
108
109
110
111
112
113
114
115
116
            // 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
117
            let del2 = document.getElementById('profileImg')
우지원's avatar
ul    
우지원 committed
118
119
120
121
122
123
            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
124
                img.setAttribute("style", 'height:300px; width:300px')
우지원's avatar
ul    
우지원 committed
125
126
127
128
129
130
                document.querySelector("div#image_container").appendChild(img);
            };
        }
    }


JeongYeonwoo's avatar
   
JeongYeonwoo committed
131
132


우지원's avatar
ul    
우지원 committed
133
134
    return (
        <div>
Choi Ga Young's avatar
Choi Ga Young committed
135
            <Menu />
JeongYeonwoo's avatar
   
JeongYeonwoo committed
136
137
138
139
140
141
            <Button onClick={setlocal}>로컬에 추가</Button>
            <Button onClick={loginedinfo}>로그인된 정보 불러오기</Button>
            <Button onClick={getInfo}>불러오기</Button>
            <Button onClick={makeLogin}>로그인 상태로 만들기</Button>
            <Button onClick={authinfo}>로그인 유저 불러오기</Button>
            {/* <Button onClick={sending}>보내기</Button> */}
우지원's avatar
ul    
우지원 committed
142
143
144
145

            <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
146
                    <h4 className="text-center mb-5">{user.name} 환영합니다 !</h4>
우지원's avatar
ul    
우지원 committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160

                    <div className="d-flex justify-content-center mb-3" id="defaultImg">
                        <Image src={userdefault} width="300px" roundedCircle />
                    </div>
                    <div className="d-flex justify-content-center">
                        <Form className="d-flex justify-content-center">
                            <Form.Group>
                                <div id="image_container"></div>
                                <Form.File label="프로필 사진 변경" accept="image/*" onChange={setThumbnail} />
                            </Form.Group>
                        </Form>
                    </div>

                    <div className="text-center">
JeongYeonwoo's avatar
   
JeongYeonwoo committed
161
162
163
164
165
166
167
168
169
170
171
                        <div className="m-3" style={{ fontWeight: "bold", fontSize: "x-large" }}>이름 : {user.name}</div>
                        <div className="m-3" id="nickname">
                            <Form className="d-flex justify-content-center" style={{ fontWeight: "bold", fontSize: "x-large" }}>
                                별명 :
                                <div id="mainNick" hidden={!hidden}>
                                    {user.nickname}
                                </div>
                                <Form.Control id="inputNick" type="name" defaultValue={user.nickname} style={{ width: "40%" }} size="sm" hidden={hidden} />
                                <Button className="ml-3" variant="outline-primary" size="sm" onClick={handleEditnick}>수정</Button>
                            </Form>

우지원's avatar
ul    
우지원 committed
172
                        </div>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
173
                        <div className="m-3" style={{ fontWeight: "bold", fontSize: "x-large" }}>이메일 : {user.email}</div>
우지원's avatar
ul    
우지원 committed
174
175
176
                    </div>
                    <div className="text-center m-5">
                        <span>
JeongYeonwoo's avatar
   
JeongYeonwoo committed
177
178
179
180
                            <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>
우지원's avatar
ul    
우지원 committed
181
182
183
184
185
186
187
188
189
190
                        </span>
                    </div>
                </div>
            </Container>

        </div>
    )
}

export default ProfilePage