Analy.js 2.1 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react';
2
3
import ChartM from './ChartM';
import ChartY from './ChartY';
4
import chartApi from './db/chartData.api';
5
6
import ButtonsForm from './components/ButtonsForm';
import { View, Dimensions } from 'react-native';
7

Choi Ga Young's avatar
Choi Ga Young committed
8
const Analy = () => {
9
10
11
12
13
14
  const date = new Date();
  const [year, setYear] = useState(date.getFullYear());
  const [month, setMonth] = useState(date.getMonth());
  const [resDataM, setResDataM] = useState([]);
  const [resDataY, setResDataY] = useState([]);
  const [selectedIndex, setSelectedIndex] = useState(0);
Choi Ga Young's avatar
Choi Ga Young committed
15

16
  const getDataM = async () => {
17
    try {
18
19
20
      const resdata = await chartApi.outMoney({ year: year, month: month })
      console.log('db res M', resdata)
      return resdata
21
    } catch (error) {
22
      console.log('error in getDataM', error)
23
24
    }
  }
25
26
27
28
29
30
31
32
33
  const getDataY = async () => {
    try {
      const resdata = await chartApi.yearMoney({ year: year })
      console.log('db res Y', resdata)
      setResDataY(resdata)

    } catch (error) {
      console.log('error in getDataY', error)
    }
34
  }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

  const calPercentage = async () => {
    const resPrice = await getDataM()
    //console.log('어떻게 생겼니', resPrice)
    let totalP = 0;

    for (let i = 0; i < resPrice.length; i++) {
      totalP = totalP + resPrice[i].total;
    }
    for (let j = 0; j < resPrice.length; j++) {
      resPrice[j].percentage = Math.round((resPrice[j].total / totalP) * 100);
    }
    setResDataM(resPrice)
  }

50
  useEffect(() => {
51
52
    getDataY()
    calPercentage()
53
  }, [])
54
55
56
57
58
59
60
61
  useEffect(() => {
    setResDataM([])
    calPercentage()
  }, [month])

  useEffect(() => {
    getDataY()
  }, [year])
Choi Ga Young's avatar
Choi Ga Young committed
62
  return (
63
64
65
66
67
68
69
70
71
72
73
74
75
    <>
      <View>
        <ButtonsForm
          onPress={(index) => setSelectedIndex(index)}
          selectedIndex={selectedIndex}
          group={["월간", "연간"]} />
      </View>
      <View>
        {
          selectedIndex === 0 ? <ChartM resDataM={resDataM} year={year} setYear={setYear} month={month} setMonth={setMonth} /> : <ChartY resDataY={resDataY} year={year} setYear={setYear} />
        }
      </View>
    </>
Choi Ga Young's avatar
Choi Ga Young committed
76
77
78
  )
}

79
export default Analy;