HistoryTab.js
4.48 KB
1
2
3
4
5
6
7
8
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
79
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
143
144
145
146
import React, { Component } from 'react';
import { View, Text, StyleSheet,ScrollView, RefreshControl } from 'react-native';
import HistoryGraph from '../component/HistoryGraph'
import SegmentedControlTab from "react-native-segmented-control-tab";
import {get_id,add_data, get_week_data, get_month_data, get_year_data, add_temp_data} from'../component/DBhelper.js';
//var today = new Date();
//today.setHours(today.getHours()+9);
function total(list){
sum = 0;
for(var i =0; i< list.length; i++)
sum += list[i].value;
return sum
}
export default class HomeTab extends Component {
constructor(){
super();
this.state = {
selectedIndex: 0,
data: get_week_data(),
title: "총 " + total(get_week_data())/1000 + "kg",
spane: "",
refreshing: false,
ID: get_id(),
}
}
_onRefresh = () => {
this.setState({refresing: true});
this.get_diff_data();
this.setState({refreshing: false});
}
componentDidMount() {
const { navigation } = this.props;
this.focusListener = navigation.addListener('didFocus', () => {
this.handleIndexChange(this.state.selectedIndex);
});
}
componentWillUnmount() {
// Remove the event listener
this.focusListener.remove();
}
handleIndexChange = index => {
this.setState({selectedIndex: index});
switch(index){
case 0:
this.setState({data: get_week_data(), title: "총 " + total(get_week_data())/1000 + "kg"});
break;
case 1:
this.setState({data: get_month_data(), title: "총 " + total(get_month_data())/1000 + "kg"});
break;
case 2:
this.setState({data: get_year_data(), title: "총 " + total(get_year_data())/1000 + "kg"});
break;
}
};
get_diff_data(){
return fetch('http://testloadbalancer-1847561458.ap-northeast-2.elb.amazonaws.com/app/get_diff_data/',{
method:'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body:JSON.stringify({
ras_id : this.state.ID,
date : "1576339901541",
}),
})
.then((response) => response.json())
.then((responseJson)=> {
console.log(responseJson)
//add_data(responseJson)
add_temp_data();
},function(){
})
.then(()=>{this.handleIndexChange(this.state.selectedIndex)})
.catch((error) => {
console.error(error);
})
}
render() {
return(
<ScrollView style = {style.container}
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh}
tintColor="#000000"
title="Loading..."
titleColor="#0066cc"
colors={["#ffffff",'#0066cc','#ffffff']}
progressBackgroundColor="#0066cc"
/>
}
>
<View style = {style.container}>
<View style={style.header}>
<Text style={style.tabName}>History </Text>
</View>
<View style={{justifyContent:'center', padding: 10}}>
<SegmentedControlTab
values={["Week", "Month", "Year"]}
selectedIndex={this.state.selectedIndex}
onTabPress={this.handleIndexChange}
/>
</View>
<View style={{flex : 8, justifyContent:'center', padding: 10}}>
<Text style ={style.date_text}>{this.state.title}</Text>
</View>
<HistoryGraph data={this.state.data} round={1000} unit="g"/>
</View>
</ScrollView>
)
}
}
const style = StyleSheet.create({
container: {
flex: 1,
},
header: {
flex :2,
justifyContent : 'center',
padding : 10,
},
tabName:{
marginStart : 10,
textAlignVertical : 'center',
fontSize : 20,
color : "#0066cc",
fontWeight: 'bold',
},
date_text:{
fontSize :20,
marginStart:5,
textAlignVertical:'center'
}
})