forked from Harekam/GithubBrowser
/
SearchResults.js
137 lines (128 loc) · 3.84 KB
/
SearchResults.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Created by harekamsingh on 4/3/16.
*/
'use strict';
import React, {
Component,
Text,
View,
Image,
ListView,
ActivityIndicatorIOS,
TouchableHighlight,
StyleSheet
} from 'react-native';
import AuthService from './AuthService';
import moment from 'moment';
class SearchResults extends Component {
constructor(props) {
super(props);
let ds = new ListView.DataSource({
rowHasChanged: (r1, r2)=>r1 != r2
});
this.state = {
showProgress: true,
dataSource: ds,
searchQuery: this.props.searchQuery
};
}
pressRow(rowData) {
this.props.navigator.push({
title: 'Details',
component: DetailsRepo,
passProps: {
repoDetails: rowData
}
});
}
renderRow(rowData) {
return (
<View style={{
padding:20,
borderColor:'#D7D7D7',
borderBottomWidth:1,
backgroundColor:'#fff'
}}>
<Text style={{fontSize:20,fontWeight:'600'}}>{rowData.full_name}</Text>
<View
style={
{flex:1, flexDirection:'row',justifyContent:'space-between',marginTop:20,marginBottom:20}}>
<View style={styles.repoCell}>
<Image source={require('image!star')}
style={styles.repoCellIcon}/>
<Text style={styles.repoCellLabel}>
{rowData.stargazers_count}
</Text>
</View>
<View style={styles.repoCell}>
<Image source={require('image!fork')}
style={styles.repoCellIcon}/>
<Text style={styles.repoCellLabel}>
{rowData.forks}
</Text>
</View>
<View style={styles.repoCell}>
<Image source={require('image!issues2')}
style={styles.repoCellIcon}/>
<Text style={styles.repoCellLabel}>
{rowData.open_issues}
</Text>
</View>
</View>
</View>
)
}
componentDidMount() {
this.doSearch();
}
doSearch() {
let url = "https://api.github.com/search/repositories?q=" + encodeURIComponent(this.state.searchQuery);
fetch(url)
.then((response)=>response.json())
.then((responseData)=> {
this.setState({
repositories: responseData.repositories,
dataSource: this.state.dataSource.cloneWithRows(responseData.items)
});
}).catch((err)=>console.log(err))
.finally(()=> {
this.setState({
showProgress: false
})
})
}
render() {
if (this.state.showProgress) {
return ( <View style={{flex: 1, justifyContent: 'center'}}>
<ActivityIndicatorIOS
size="large"
animating={true}/>
</View>);
}
return (
<View style={{
flex: 1,
justifyContent: 'flex-start'
}}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
repoCell: {
width: 50,
alignItems: 'center'
},
repoCellIcon: {
width: 20,
height: 20
},
repoCellLabel: {
textAlign: 'center'
}
});
export default SearchResults;