-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
80 lines (68 loc) · 1.98 KB
/
Copy pathApp.js
File metadata and controls
80 lines (68 loc) · 1.98 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
import React, { useState, useEffect } from 'react';
import { StyleSheet, SafeAreaView, FlatList } from 'react-native';
import BookCardComponent from './components/BookCardComponent';
import BookCardPlaceholder from './components/BookCardPlaceholderComponent';
export default function App() {
const [books, setBooks] = useState([...new Array(10).fill({})]);
const [isDataFetched, setDataFetched] = useState(false);
useEffect(() => {
fetch(
'https://www.googleapis.com/books/v1/volumes/?maxResults=30&q=danbrown'
)
.then(response => response.json())
.then(responseJson => {
const { items } = responseJson;
const booksList = items.map(book => {
const {
volumeInfo: { title, authors, imageLinks },
id: bookId,
} = book;
return {
bookId,
thumbnail: imageLinks
? imageLinks.thumbnail
: 'https://i.ibb.co/YLC0nQQ/not-found.png',
title,
authors: authors ? authors.toString().replace(/,/g, ', ') : '-',
};
});
setBooks(booksList);
setDataFetched(true);
})
.catch(error => {
console.error(error);
});
}, [books]);
const renderBookComponent = ({ item }) => {
const { thumbnail, title, authors, bookId } = item;
return (
<BookCardComponent
key={bookId}
title={title}
authors={authors}
thumbnail={thumbnail}
/>
);
};
const renderX = () => (
<FlatList
data={books}
renderItem={renderBookComponent}
keyExtractor={item => item.bookId}
/>
);
const renderPlaceholders = () =>
books.map((e, i) => <BookCardPlaceholder key={i} />);
return (
<SafeAreaView style={styles.container}>
{isDataFetched ? renderX() : renderPlaceholders()}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fafafa',
paddingTop: 35,
},
});