Skip to content

Commit ad23749

Browse files
Qaiser AbbasQaiser Abbas
Qaiser Abbas
authored and
Qaiser Abbas
committed
MobX store in ReactNative - fetch data from Google Books API
1 parent f029d09 commit ad23749

File tree

9 files changed

+111
-12
lines changed

9 files changed

+111
-12
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
{
1212
singleQuote: true,
1313
bracketSpacing: true,
14-
trailingComma: 'all',
14+
trailingComma: 'es5',
1515
semi: false,
1616
},
1717
],
@@ -21,6 +21,7 @@ module.exports = {
2121
'react/jsx-closing-tag-location': 'off',
2222
'react/no-multi-comp': 'off',
2323
'no-use-before-define': 'off',
24+
'no-param-reassign': 'off',
2425
'react/jsx-closing-bracket-location': 'off',
2526
'react/jsx-curly-spacing': 'off',
2627
'react/jsx-equals-spacing': 'off',

keys.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"GBOOKS_KEY": "YOUR_GOOGLE_BOOKS_API_KEY"
3+
}

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@
3030
"jest": {
3131
"preset": "jest-expo",
3232
"setupTestFrameworkScriptFile": "<rootDir>/jestSetup.js",
33-
"snapshotSerializers": ["enzyme-to-json/serializer"]
33+
"snapshotSerializers": [
34+
"enzyme-to-json/serializer"
35+
]
3436
},
3537
"dependencies": {
38+
"axios": "^0.18.0",
3639
"enzyme": "^3.3.0",
3740
"enzyme-adapter-react-16": "^1.1.1",
3841
"expo": "^27.0.1",
42+
"mobx": "^4.2.1",
43+
"mobx-react": "^5.1.2",
44+
"mobx-state-tree": "^2.0.5",
3945
"prop-types": "^15.6.1",
4046
"react": "16.3.1",
4147
"react-dom": "16",

src/stores/book/api.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import axios from 'axios'
2+
3+
const baseUrl = `https://www.googleapis.com/books/v1/volumes`
4+
const { GBOOKS_KEY } = require('../../../keys')
5+
6+
const fetchBooks = () =>
7+
axios.get(`${baseUrl}?q=books&printType=books&key=${GBOOKS_KEY}`)
8+
9+
export default {
10+
fetchBooks,
11+
}

src/stores/book/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { types as t, flow } from 'mobx-state-tree'
2+
import api from './api'
3+
4+
let store = null
5+
6+
const Book = t.model('Book', {
7+
id: t.identifier(),
8+
title: t.string,
9+
pageCount: t.number,
10+
authors: t.array(t.string),
11+
image: t.maybe(t.string),
12+
inStock: t.optional(t.boolean, true),
13+
})
14+
15+
const BookStore = t
16+
.model('BookStore', {
17+
books: t.array(Book),
18+
})
19+
.actions(self => {
20+
function updateBooks(books) {
21+
books.items.forEach(book => {
22+
self.books.push({
23+
id: book.id,
24+
title: book.volumeInfo.title,
25+
pageCount: book.volumeInfo.pageCount,
26+
authors: book.volumeInfo.authors,
27+
publisher: book.volumeInfo.publisher,
28+
image: book.volumeInfo.imageLinks.smallThumbnail,
29+
})
30+
})
31+
}
32+
33+
const loadBooks = flow(function* loadBooks() {
34+
try {
35+
const books = yield api.fetchBooks()
36+
updateBooks(books)
37+
} catch (err) {
38+
console.error('Failed to load books ', err)
39+
}
40+
})
41+
42+
return {
43+
loadBooks,
44+
}
45+
})
46+
47+
export default () => {
48+
if (store) return store
49+
50+
store = BookStore.create({ books: [] })
51+
return store
52+
}

src/views/book/components/Book.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
import { Text } from 'react-native'
3+
import { observer } from 'mobx-react'
4+
5+
export default observer(({ book }) => (
6+
<Text>
7+
{book.title} by {book.authors[0]}
8+
</Text>
9+
))

src/views/book/components/BookList.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import { View } from 'react-native'
3+
import { observer } from 'mobx-react'
4+
import Book from './Book'
5+
6+
export default observer(({ books }) => (
7+
<View>{books.map(book => <Book key={book.id} book={book} />)}</View>
8+
))
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, { Component } from 'react'
2+
import BookStore from '../../../stores/book'
3+
import BookList from './BookList'
4+
5+
class BookListView extends Component {
6+
async componentWillMount() {
7+
this.store = BookStore()
8+
await this.store.loadBooks()
9+
}
10+
11+
render() {
12+
return <BookList books={this.store.books} />
13+
}
14+
}
15+
16+
export default BookListView

src/views/book/components/book-type-tabs.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import React from 'react'
2-
import { View, Button } from 'react-native'
2+
import { View } from 'react-native'
33
import Title from '../../../components/Title'
4+
import BookListView from './BookListView'
45

56
export const AllBooksTab = ({ navigation }) => (
67
<View>
78
<Title text="All Books" />
8-
<Button
9-
onPress={() => navigation.navigate('Authors')}
10-
title="Go to Authors"
11-
/>
12-
<Button
13-
onPress={() => navigation.navigate('NonfictionBooksTab')}
14-
title="Go to NonfictionBooksTab"
15-
/>
16-
<Button onPress={() => navigation.openDrawer()} title="Open Drawer" />
9+
<BookListView />
1710
</View>
1811
)
1912

0 commit comments

Comments
 (0)