-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookListV2.js
executable file
·111 lines (99 loc) · 2.24 KB
/
BookListV2.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
import React, {
Component,
} from 'react';
import {
StyleSheet,
Text,
View,
Image,
ListView,
} from 'react-native';
import BookItem from './BookItem';
const API_KEY = '73b19491b83909c7e07016f4bb4644f9:2:60667290';
const QUERY_TYPE = 'hardcover-fiction';
const API_STEM = 'http://api.nytimes.com/svc/books/v3/lists'
const ENDPOINT = `${API_STEM}/${QUERY_TYPE}?response-format=json&api-key=${API_KEY}`;
class BookList extends Component {
constructor(props) {
super(props);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([])
};
}
componentDidMount() {
this._refreshData();
}
_renderRow(rowData) {
return <BookItem coverURL={rowData.book_image} title={rowData.title} author={rowData.author}/>;
}
_renderHeader() {
return (<View style={styles.sectionDivider}>
<Text style={styles.headingText}>
Bestsellers in Hardcover Fiction
</Text>
</View>);
}
_renderFooter() {
return(
<View style={styles.sectionDivider}>
<Text>Data from the New York Times bestsellers list.</Text>
</View>
);
}
_refreshData() {
fetch(ENDPOINT)
.then((response) => response.json())
.then((rjson) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(rjson.results.books)
});
});
}
render() {
return (
<ListView
style={{marginTop: 24}}
dataSource={this.state.dataSource}
renderRow={this._renderRow}
renderHeader={this._renderHeader}
renderFooter={this._renderFooter}
/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
paddingTop: 24
},
list: {
flex: 1,
flexDirection: 'row'
},
listContent: {
flex: 1,
flexDirection: 'column'
},
row: {
flex: 1,
fontSize: 24,
padding: 42,
borderWidth: 1,
borderColor: '#DDDDDD'
},
sectionDivider: {
padding: 8,
backgroundColor: '#EEEEEE',
alignItems: 'center'
},
headingText: {
flex: 1,
fontSize: 24,
alignSelf: 'center'
}
});
export default BookList;