-
Notifications
You must be signed in to change notification settings - Fork 0
/
members.js
114 lines (106 loc) · 3.16 KB
/
members.js
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
import React, { Component, PropTypes } from 'react';
import { Button,
ListView,
Text,
TouchableHighlight,
View,
} from 'react-native';
import { ApiKey } from './api_key';
var membersHouse = { members: [], lastUpdated: null };
export class Members extends Component {
constructor(props) {
super(props);
var dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: dataSource.cloneWithRows([null]),
chamber: 'house',
}
var fifty_minutes = 3000000;
if (membersHouse.lastUpdated === null || Date.now() - membersHouse.lastUpdate > fifty_minutes) {
this._setMembers('house');
}
}
async _setMembers(chamber) {
membersHouse.members = await this._getMembersFromApiAsync(chamber);
membersHouse.lastUpdated = Date.now();
this.setState({
dataSource: this.state.dataSource.cloneWithRows(membersHouse.members),
chamber: chamber,
});
}
async _getMembersFromApiAsync(chamber) {
try {
let response = await this._fetchMembers(chamber);
let responseJson = await response.json();
members = [];
for (var result of responseJson.results) {
for (var member of result.members) {
members.push(member);
}
}
} catch (error) {
console.error(error);
}
return members.sort((a,b) => {
if (a.state < b.state) { return -1; }
if (a.state > b.state) { return +1; }
if (a.district < b.district) { return -1; }
if (a.district > b.district) { return +1; }
return 0;
});
}
_fetchMembers(chamber) {
return fetch(`https://api.propublica.org/congress/v1/114/${chamber}/members.json`, {
method: 'GET',
headers: {
'X-API-Key': ApiKey,
}
});
}
_renderRow(member) {
if (member === null) {
return (<Text>(loading members...)</Text>);
}
if (member.district === undefined) {
stateDistrict = member.state
} else {
stateDistrict = member.state + '-' + member.district;
}
text = ('(' + stateDistrict + ' ' + member.party + ') '
+ member.first_name + ' ' + member.last_name);
return(
<TouchableHighlight onPress={() => this.props.onSelect(member)}>
<Text>{text}</Text>
</TouchableHighlight>
);
}
render() {
chamberIsHouse = this.state.chamber == 'house';
houseTitle = chamberIsHouse ? 'In the House' : 'Show House';
senateTitle = chamberIsHouse ? 'Show Senate' : 'In the Senate';
return (
<View>
<View style={{flexDirection: 'row'}}>
<Button
title={houseTitle}
disabled={this.state.chamber == 'house'}
onPress={() => this._setMembers('house')}
/>
<Button
title={senateTitle}
disabled={this.state.chamber == 'senate'}
onPress={() => this._setMembers('senate')}
/>
</View>
<ListView
dataSource={this.state.dataSource}
renderRow={(member) => this._renderRow(member)}
pageSize={1000}
/>
</View>
);
}
}
Members.propTypes = {
onSelect: PropTypes.func.isRequired,
};