-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
169 lines (157 loc) · 4.12 KB
/
App.js
File metadata and controls
169 lines (157 loc) · 4.12 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from 'react';
import { StyleSheet, Text, ListView, View, TextInput, Button } from 'react-native';
import _ from 'lodash';
export default class App extends React.Component {
constructor(props){
super(props);
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: this.ds.cloneWithRows([]),
searchText: '',
status: '',
error: false,
isLoading: false,
};
this.onSearch = this.onSearch.bind(this);
}
onSearch(){
this.setState({isLoading: true});
return fetch(this.getIssuesUrl())
.then((response) => {
let issues = [];
let status = '';
if (!response.ok){
issues = [];
status = 'Invalid Repository Name.';
error = true;
} else {
data = _.cloneDeep(JSON.parse(response._bodyText));
issues = _.map(data, (issue) => {
return {
id: issue.id,
title: issue.title,
author: issue.user.login,
date: issue.created_at,
state: issue.state,
};
});
status = `${data.length} issues found.`;
error = false;
}
this.setState({
dataSource: this.ds.cloneWithRows(issues),
status,
error,
isLoading: false,
});
})
.catch((error) => {
console.error(error);
this.setState({status: 'Something went wrong. Please retry.'});
});
}
getIssuesUrl(){
return `https://api.github.com/repos/${this.state.searchText}/issues`;
}
isButtonDisabled(){
return this.state.searchText === '' || this.state.isLoading;
}
render() {
return (
<View style={styles.appContainer}>
<View><Text style={styles.heading}>GitHub Issue Fetcher</Text></View>
<View>
<Text>Enter GitHub Repo name:</Text>
<TextInput
style={styles.searchBox}
underlineColorAndroid='transparent'
onChangeText={(searchText) => this.setState({searchText: _.trim(searchText)})}
value={this.state.searchText}
/>
<Button
onPress={this.onSearch}
title={this.state.isLoading ? "Getting Issues..." : "Get Issues"}
style={styles.searchButton}
disabled={this.isButtonDisabled()}
accessibilityLabel="Get all isssues of the specified github repos."
/>
</View>
<View>
{
!_.isEmpty(this.state.status) &&
<Text style={this.state.error ? styles.errorStatus : styles.successStatus}>
{this.state.status}
</Text>
}
<ListView
style={styles.issuesList}
enableEmptySections
dataSource={this.state.dataSource}
renderRow={(rowData) => <GitHubIssue issue={rowData} key={rowData.id} />}
/>
</View>
</View>
);
}
}
const GitHubIssue = ({issue}) => (
<View style={styles.issueContainer}>
<Text style={styles.issueTitle}>Title: {issue.title}</Text>
<Text style={styles.issueState}>State: {issue.state}</Text>
<View>
<Text>Author: {issue.author}</Text>
<Text>Date: {issue.date}</Text>
</View>
</View>
)
const styles = StyleSheet.create({
appContainer: {
flex: 1,
paddingTop: 22,
paddingBottom: 60
},
heading: {
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 15,
},
searchBox: {
height: 40,
borderColor: 'gray',
margin: 2,
marginBottom: 5,
borderWidth: 1,
paddingLeft: 2,
},
issuesList: {
marginTop: 5,
},
successStatus: {
color: 'green',
textAlign: 'center',
fontSize: 11,
},
errorStatus: {
color: 'red',
textAlign: 'center',
fontSize: 11,
},
searchButton: {
color: '#841584',
},
issueTitle: {
fontWeight: 'bold',
fontSize: 14,
},
issueState: {
fontStyle: 'italic',
fontSize: 13,
},
issueContainer: {
borderWidth: 1,
padding: 2,
marginLeft: 2,
marginRight: 2,
marginBottom: 5,
}
});