ChartTabs.js 4.36 KB
Newer Older
Spark's avatar
Spark committed
1
import { useEffect, useState } from "react";
Spark's avatar
chartjs    
Spark committed
2
import { Card, Row, Tab, Tabs } from "react-bootstrap";
KangMin An's avatar
KangMin An committed
3
4
5
6
7
8
import ChartTemp from "./ChartTemp";
import ChartHumidity from "./ChartHumidity";
import ChartWindSpeed from "./ChartWindSpeed";
import ChartPressure from "./ChartPressure";
import "../App.css";
import { isLogined } from "./../utils/Auth";
Spark's avatar
Spark committed
9
import axios from "axios";
KangMin An's avatar
KangMin An committed
10
import { routesClient } from "./../routesClient";
Spark's avatar
chartjs    
Spark committed
11
12

function ChartTabs() {
KangMin An's avatar
KangMin An committed
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  const cardstyled = {
    margin: "auto",
    padding: "1em",
    display: "flex",
    justifyContent: "center",
    width: "100%",
    borderWidth: "3px",
    borderRadius: "20px",
    borderColor: "rgb(110, 189, 142)",
    color: "#04AB70",
  };

  const [temp, setTemp] = useState([]);
  const [press, setPress] = useState([]);
  const [humi, setHumi] = useState([]);
  const [windSpd, setWindSpd] = useState([]);

  const [newLabel, setNewLabel] = useState([]);
  const [tempNewLabel, setTempNewLabel] = useState([]);

  useEffect(() => {
    if (isLogined()) {
      axios
        .get(routesClient.userWeather, { withCredentials: true })
        .then((res) => {
          console.log(res.data.contents.weather_in);
          const userWeather = res.data.contents.weather_in;
          const userWeatherPredict = res.data.contents.weather_predict;

          const tempArray = [];
          const humiArray = [];
          const pressArray = [];
          const windspeedArray = [];

          const tempLabelArray = [];
          const labelArray = [];

          for (let i = 0; i < userWeather.length; i++) {
            tempArray.push(userWeather[i].temp);
            humiArray.push(userWeather[i].humi);
            pressArray.push(userWeather[i].press);
            windspeedArray.push(userWeather[i].wind_speed);

            let date = new Date(
              new Date(userWeather[i].collected_at).getTime()
            );

            tempLabelArray.push(`${date.getHours()}:${date.getMinutes()}`);
            labelArray.push(`${date.getHours()}:${date.getMinutes()}`);
          }
          for (let j = 0; j < userWeatherPredict.length; j++) {
            tempArray.push(userWeatherPredict[j].temp);
            let date = new Date(
              new Date(userWeatherPredict[j].collected_at).getTime()
            );
            tempLabelArray.push(`${date.getHours()}:${date.getMinutes()}`);
          }

          setTemp(tempArray);
          setHumi(humiArray);
          setPress(pressArray);
          setWindSpd(windspeedArray);

          setTempNewLabel(tempLabelArray);
          setNewLabel(labelArray);
        });
    } else {
      axios.get(routesClient.outsideLoc + `3743011`).then((res) => {
        const outWeather = res.data.contents.weather_out;

        const tempArray = [];
        const humiArray = [];
        const pressArray = [];
        const windspeedArray = [];

        const tempLabelArray = [];
        const labelArray = [];

        for (let i = 0; i < outWeather.length; i++) {
          tempArray.push(outWeather[i].temp);
          humiArray.push(outWeather[i].humi);
          pressArray.push(outWeather[i].press);
          windspeedArray.push(outWeather[i].wind_speed);

          let date = new Date(new Date(outWeather[i].collected_at).getTime());

          tempLabelArray.push(`${date.getHours()}:${date.getMinutes()}`);
          labelArray.push(`${date.getHours()}:${date.getMinutes()}`);
        }
Spark's avatar
chartjs    
Spark committed
102

KangMin An's avatar
KangMin An committed
103
104
105
106
        setTemp(tempArray);
        setHumi(humiArray);
        setPress(pressArray);
        setWindSpd(windspeedArray);
Spark's avatar
chartjs    
Spark committed
107

KangMin An's avatar
KangMin An committed
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
        setTempNewLabel(tempLabelArray);
        setNewLabel(labelArray);
      });
    }
  }, []);
  //3743011 default

  const [key, setKey] = useState("temp");

  return (
    <Row className="text-center w-100 my-2">
      <Card style={cardstyled}>
        <Tabs activeKey={key} onSelect={(k) => setKey(k)} className="mb-3">
          <Tab eventKey="temp" title="온도">
            <ChartTemp temp={temp} newLabel={tempNewLabel} />
          </Tab>
          <Tab eventKey="humidity" title="습도">
            <ChartHumidity humi={humi} newLabel={newLabel} />
          </Tab>
          <Tab eventKey="windspeed" title="풍속">
            <ChartWindSpeed windSpd={windSpd} newLabel={newLabel} />
          </Tab>
          <Tab eventKey="pressure" title="기압">
            <ChartPressure press={press} newLabel={newLabel} />
          </Tab>
        </Tabs>
      </Card>
    </Row>
  );
Spark's avatar
chartjs    
Spark committed
137
138
}

KangMin An's avatar
KangMin An committed
139
export default ChartTabs;