UserInfo.js 4.1 KB
Newer Older
1
2
3
4
5
6
import React, { useState, useEffect } from "react";
import { Row, Card, Button, Col } from "react-bootstrap";
import "../App.css";
import { Link } from "react-router-dom";
import { callUserInfo } from "../utils/CheckDB";
import { isLogined } from "../utils/Auth";
7

Spark's avatar
Spark committed
8
function UserInfo() {
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  const cardstyled = {
    margin: "auto",
    padding: "1em 0.5em 1em 0.5em",
    display: "flex",
    justifyContent: "center",
    width: "100%",
    borderWidth: "3px",
    borderRadius: "20px",
    borderColor: "rgba(195, 195, 195, 0.753)",
    color: "rgb(110, 189, 142)",
  };

  const btnstyled2 = {
    background: "white",
    margin: "auto",
    borderWidth: "2px",
    fontSize: "0.7em",
    color: "rgb(110, 189, 142)",
    borderColor: "rgba(195, 195, 195, 0.753)",
    width: "50%",
  };

  const [userNick, setUserNick] = useState("");
  const [createdTime, setCreatedTime] = useState("");

  useEffect(() => {
    callUserInfo().then((res) => {
      if (isLogined()) {
        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(); // 일

        const stDate = new Date(dateStr[0], dateStr[1], dateStr[2]); // 가입 날짜

        const endDate = new Date(year, month, day); // 현재 시간

        const btMs = endDate.getTime() - stDate.getTime(); // 주어진 날짜 사이의 경과 시간 (밀리 초)

        const btDay = btMs / (1000 * 60 * 60 * 24); // Ms -> 일

        setCreatedTime(btDay);
      }
    });
  }, []);

  const [showState, setShowState] = useState("");
  const [localState, setLocalState] = useState([]);

  useEffect(() => {
    // user-info 에서 loc_code
    callUserInfo().then((res) => {
      if (isLogined()) {
        const dbloc = res[0].loc_code;
        if (dbloc === null) {
          setShowState("지역을 입력해주세요");
          const localstyle = document.getElementById("local_state");
          localstyle.style.display = "none";
        } else {
          const localName = res[0].loc_name;
          setLocalState(localName);
        }
      }
    });
  }, []);

  return (
Spark's avatar
Spark committed
79
    <Col className="text-center pt-2 px-0">
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
      <Card style={cardstyled} id="localName">
        <Card.Title>
          <strong>
            {isLogined() ? <h3>{`${userNick}`}</h3> : <h3>손 님</h3>}
          </strong>
          <p style={{ fontWeight: "300", margin: "0" }}>환영합니다</p>
        </Card.Title>
        <hr />
        <Row
          style={{
            alignItems: "center",
            margin: "auto",
            justifyContent: "center",
          }}
        >
          <Card.Subtitle>
            {isLogined() ? (
              <div className="mb-2">
                <div>{showState}</div>

                <div id="local_state">
                  {`${localState["doe"]}`}
                  <br />
                  {`${localState["sgg"]}`}
                  <br />
                  {`${localState["emd"]}`}
                </div>
              </div>
            ) : (
              <div className="mb-2">로그인  이용 가능합니다</div>
            )}
          </Card.Subtitle>

          {isLogined() && (
            <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>
          )}
        </Row>
        {isLogined() && (
          <Card.Text>
            <hr />
            <Row style={{ color: "black" }}>
              <p style={{ fontWeight: "300", margin: "0" }}>환경을 향한 노력</p>
              <h3 style={{ fontWeight: "300", color: "#2b90d9", margin: "0" }}>
                <strong>{createdTime}</strong> 일
              </h3>
            </Row>
          </Card.Text>
        )}
      </Card>
    </Col>
  );
143
}
144
export default UserInfo;