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

Choi Ga Young's avatar
Choi Ga Young committed
9
const Analy = () => {
Choi Ga Young's avatar
Choi Ga Young committed
10

11
12
13
14
15
16
  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
17
18
  const todayM = date.getMonth();
  const todayY = date.getFullYear();
Choi Ga Young's avatar
Choi Ga Young committed
19

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

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

  const calPercentage = async () => {
    const resPrice = await getDataM()
    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)
  }

Choi Ga Young's avatar
Choi Ga Young committed
51
52
53
54
55
56
57
58
  useFocusEffect(
    React.useCallback(() => {
      setMonth(todayM)
      setYear(todayY)
      getDataY()
      calPercentage()
    }, [])
  );
Choi Ga Young's avatar
Choi Ga Young committed
59

60
61
62
63
64
65
66
67
  useEffect(() => {
    setResDataM([])
    calPercentage()
  }, [month])

  useEffect(() => {
    getDataY()
  }, [year])
Choi Ga Young's avatar
Choi Ga Young committed
68

Choi Ga Young's avatar
Choi Ga Young committed
69
  return (
70
71
72
    <>
      <View>
        <ButtonsForm
Choi Ga Young's avatar
Choi Ga Young committed
73
          onPress={(index) => { setSelectedIndex(index), setYear(todayY) }}
74
75
76
77
78
79
80
81
82
          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
83
84
85
  )
}

86
export default Analy;