Skip to content

Commit b3ec126

Browse files
Qaiser AbbasQaiser Abbas
authored andcommitted
Update the UI on tab change
1 parent 8886ac4 commit b3ec126

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

src/stores/book/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { types as t, flow, getEnv } from 'mobx-state-tree'
2-
import bookApi from './api'
2+
import bookApi from './mock-api/api'
33

44
let store = null
55

@@ -49,6 +49,7 @@ export const BookStore = t
4949
}
5050

5151
const loadBooks = flow(function* loadBooks() {
52+
if (self.books.length > 0) return
5253
try {
5354
const books = yield self.api.fetchBooks()
5455
updateBooks(books)
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import React, { Component } from 'react'
2-
import BookStore from '../../../stores/book'
2+
import { View } from 'react-native'
3+
import { observer } from 'mobx-react'
4+
5+
import BkStore from '../../../stores/book'
36
import BookList from './BookList'
7+
import Title from '../../../components/Title'
48

59
class BookListView extends Component {
610
async componentWillMount() {
7-
this.store = BookStore()
11+
this.store = BkStore()
812
await this.store.loadBooks()
913
}
1014

1115
render() {
12-
return <BookList books={this.store.books} />
16+
const { routeName } = this.props.navigation.state
17+
return (
18+
<View>
19+
<Title text={`${routeName} Books`} />
20+
<BookList books={this.store.sortedBooks} />
21+
</View>
22+
)
1323
}
1424
}
1525

16-
export default BookListView
26+
export default observer(BookListView)

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/views/book/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { createBottomTabNavigator } from 'react-navigation'
2+
import { observer } from 'mobx-react'
23

3-
import {
4-
AllBooksTab,
5-
FictionBooksTab,
6-
NonfictionBooksTab,
7-
} from './components/book-type-tabs'
4+
import BkStore from '../../stores/book'
5+
import BookListView from './components/BookListView'
86

9-
export default createBottomTabNavigator({
10-
'All Books': AllBooksTab,
11-
Fiction: FictionBooksTab,
12-
Nonfiction: NonfictionBooksTab,
13-
})
7+
export default observer(
8+
createBottomTabNavigator(
9+
{
10+
All: BookListView,
11+
Fiction: BookListView,
12+
Nonfiction: BookListView,
13+
},
14+
{
15+
navigationOptions: ({ navigation }) => ({
16+
tabBarOnPress: () => {
17+
const { routeName } = navigation.state
18+
const store = BkStore()
19+
store.setGenre(routeName)
20+
},
21+
}),
22+
}
23+
)
24+
)

0 commit comments

Comments
 (0)