UserInfo.js 5.41 KB
Newer Older
1
2
import React, { useState, useEffect } from 'react'
import { Row, Card, Button, Col } from 'react-bootstrap';
3
import '../App.css'
Spark's avatar
Spark committed
4
import { Link } from 'react-router-dom';
5
6
import { callUserInfo, checkCookies } from '../utils/CheckDB';
import axios from 'axios';
7
import { isLogined } from '../utils/Auth';
8
9


Spark's avatar
Spark committed
10
function UserInfo() {
Spark's avatar
Spark committed
11

12
13
    const islogin = localStorage.getItem('login')

14
15
    const cardstyled = {
        margin: 'auto',
Spark's avatar
Spark committed
16
        padding: '1em 0.5em 1em 0.5em',
17
18
19
20
21
22
        display: 'flex',
        justifyContent: 'center',
        width: '100%',
        borderWidth: '3px',
        borderRadius: '20px',
        borderColor: 'rgba(195, 195, 195, 0.753)',
Spark's avatar
Spark committed
23
        color: 'rgb(110, 189, 142)',
24
    }
25

26
27
28
29
    const btnstyled2 = {
        background: 'white',
        margin: 'auto',
        borderWidth: '2px',
Spark's avatar
Spark committed
30
        fontSize: '0.7em',
31
32
        color: 'rgb(110, 189, 142)',
        borderColor: 'rgba(195, 195, 195, 0.753)',
Spark's avatar
Spark committed
33
        width: '50%'
34
    }
Spark's avatar
Spark committed
35

36
37
38
39
40
    const [userNick, setUserNick] = useState('')
    const [createdTime, setCreatedTime] = useState('')

    useEffect(() => {
        callUserInfo().then((res) => {
41
            if (isLogined()) {
42
43
44
45
46
47
48
49
                setUserNick(res[0]['nick_name'])
                const dateStr = res[0]['created_at'].split('T')[0].split('-')
                const now = new Date();

                const year = now.getFullYear(); // 년
                const month = now.getMonth() + 1; // 월 0부터 시작
                const day = now.getDate(); // 일

50
51
                const stDate = new Date(dateStr[0], dateStr[1], dateStr[2]) // 가입 날짜
                const endDate = new Date(year, month, day) // 현재 시간
52
53

                const btMs = endDate.getTime() - stDate.getTime() // 주어진 날짜 사이의 경과 시간 (밀리 초)
54
                const btDay = btMs / (1000 * 60 * 60 * 24) // 초 -> 일
55
56
57
58
59
60
61

                setCreatedTime(btDay)
            }
            else {
                console.log('Not Logined')
            }
        })
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    }, [checkCookies()])


    const codeInit = {
        code_doe: '',
        code_sgg: '',
        code_emd: ''
    }
    const [codeValue, setCodeValue] = useState(codeInit)

    useEffect(() => {

        // user-info 에서 loc_code
        callUserInfo().then((res) => {
            if (isLogined()) {
                console.log(res[0].loc_code)
                const newCode = res[0].loc_code
                const newdoe = String(newCode).substring(0, 2)
                const newsgg = String(newCode).substring(0, 5)
                const newemd = String(newCode).substring(0,)

                async function CompareCode() {
                    await axios.get('/api/data/loccode').then((response) => {
                        console.log(response.data.contents.loc_code)
                    })
                }
                CompareCode()

            }
            else {
                console.log(res)
            }
        })
95
96
    }, [])

97
    return (
98
99
100
101
        <Col className='text-center pt-3 pb-2 px-0'>
            <Card style={cardstyled} id='localName'>
                <Card.Title>
                    <strong>
102
                        {isLogined() ?
103
104
105
106
107
108
109
                            <h3>
                                {`${userNick}`}
                            </h3>
                            :
                            <h3>
                                GUEST
                            </h3>
Spark's avatar
Spark committed
110
                        }
111
112
113
114
115
116
117
118
                    </strong>
                    <p style={{ fontWeight: '300', margin: '0' }}>
                        환영합니다
                    </p>
                </Card.Title>
                <hr />
                <Row style={{ alignItems: 'center', margin: 'auto', justifyContent: 'center' }}>
                    <Card.Subtitle>
119
                        {isLogined() ?
120
                            <p className='mb-2'>
121
122
                                ????????
                                {/* 
123
124
125
126
                                {`${localname_doe}`}
                                <br />
                                {`${localname_sgg}`}
                                <br />
127
                                {`${localname_emd}`} */}
128
129
130
131
132
133
134
                            </p>
                            :
                            <p className='mb-2'>
                                로그인  이용 가능합니다
                            </p>
                        }
                    </Card.Subtitle>
135
                    {isLogined() &&
136
137
138
139
140
                        <Button variant='light' className='m-auto d-flex' style={btnstyled2}>
                            <Link to='/edit' className='w-100' style={{ textDecoration: 'none', color: 'rgb(110, 189, 142)' }}>
                                변경
                            </Link>
                        </Button>
Spark's avatar
Spark committed
141
                    }
142
                </Row>
143
                {isLogined() &&
144
145
146
147
148
149
150

                    <Card.Text>
                        <hr />
                        <Row style={{ color: 'black' }}>
                            <p style={{ fontWeight: '300', margin: '0' }}>
                                환경을 향한 노력
                            </p>
151
                            <h3 style={{ fontWeight: '300', color: '#2b90d9', margin: '0' }}>
152
153
154
155
156
157
158
159
160
                                {createdTime}일차
                            </h3>

                        </Row>

                    </Card.Text>
                }
            </Card>
        </Col>
161
162
    )
}
Spark's avatar
Spark committed
163
export default UserInfo;