Skip to content

Commit bc730b6

Browse files
committed
Fix tab navigation
1 parent 7efa4dd commit bc730b6

File tree

5 files changed

+78
-31
lines changed

5 files changed

+78
-31
lines changed

App.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
import React from 'react';
22
import { StyleSheet, Text, View } from 'react-native';
33
import { TabNavigator } from 'react-navigation';
4+
import DecksList from './components/DecksList';
5+
import AddDeck from './components/AddDeck';
6+
import { getCurrentRouteName } from './utils/helpers';
7+
8+
const DecksNavigator = TabNavigator({
9+
AddDeck: {
10+
screen: AddDeck,
11+
navigationOptions: {
12+
tabBarLabel: 'NEW DECK'
13+
}
14+
},
15+
Decks: {
16+
screen: DecksList,
17+
navigationOptions: {
18+
tabBarLabel: 'DECKS'
19+
}
20+
}
21+
});
422

523
export default class App extends React.Component {
24+
state = {};
25+
626
render() {
727
return (
828
<View style={styles.container}>
9-
<Text>Open up App.js to start working on your app!</Text>
10-
<Text>Changes you make will automatically reload.</Text>
11-
<Text>Shake your phone to open the developer menu.</Text>
29+
<DecksNavigator
30+
onNavigationStateChange={(prevState, currentState) => {
31+
const currentScreen = getCurrentRouteName(currentState);
32+
const prevScreen = getCurrentRouteName(prevState);
33+
if (prevScreen !== currentScreen) {
34+
this.setState({ DecksNavigator: currentScreen });
35+
}
36+
}}
37+
screenProps={{ currentScreen: this.state.DecksNavigator }}
38+
/>
1239
</View>
1340
);
1441
}
1542
}
1643

1744
const styles = StyleSheet.create({
1845
container: {
19-
flex: 1,
20-
backgroundColor: '#fff',
21-
alignItems: 'center',
22-
justifyContent: 'center',
23-
},
46+
flex: 1
47+
}
2448
});

api.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@ const STORAGE_KEY = '@Flashcards:store';
44

55
export const getAllDecks = () =>
66
AsyncStorage.getItem(STORAGE_KEY)
7-
.then(d => d.json())
7+
.then(d => JSON.parse(d))
88
.catch(err => {
99
console.log(err);
10-
return null;
10+
return [];
1111
});
1212

13-
export const getDeck = id =>
13+
export const saveDeckTitle = title =>
1414
AsyncStorage.getItem(STORAGE_KEY)
15-
.then(d => d.json())
16-
.then(data => data[id])
15+
.then(data => {
16+
data = JSON.parse(data);
17+
data = data ? data : {};
18+
data[title] = { title, questions: [] };
19+
return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
20+
})
1721
.catch(err => {
1822
console.log(err);
1923
return null;
2024
});
2125

22-
export const saveDeckTitle = title =>
26+
export const getDeck = id =>
2327
AsyncStorage.getItem(STORAGE_KEY)
2428
.then(d => d.json())
25-
.then(data => {
26-
data[title] = { title, questions: [] };
27-
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
28-
})
29+
.then(data => data[id])
2930
.catch(err => {
3031
console.log(err);
3132
return null;

components/AddDeck.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export default class AddDeck extends React.Component {
1010
}
1111

1212
onSubmit = () => {
13-
saveDeckTitle(this.state.title);
14-
this.props.navigation.navigate('DeckList');
13+
const title = this.state.title;
14+
this.setState({ title: '' }, () =>
15+
saveDeckTitle(title).then(() => this.props.navigation.navigate('Decks'))
16+
);
1517
};
1618

1719
render() {
@@ -24,6 +26,7 @@ export default class AddDeck extends React.Component {
2426
onChangeText={text => {
2527
this.setState({ title: text });
2628
}}
29+
value={this.state.title}
2730
spellCheck={false}
2831
underlineColorAndroid={black}
2932
/>
@@ -40,23 +43,26 @@ const styles = StyleSheet.create({
4043
container: {
4144
flex: 1,
4245
justifyContent: 'center',
43-
alignItem: 'center'
46+
alignItems: 'center'
4447
},
4548
heading: {
4649
fontSize: 22,
47-
fontWeight: 'bold'
50+
fontWeight: 'bold',
51+
width: '60%',
52+
textAlign: 'center',
53+
marginBottom: 20
4854
},
4955
submitBtn: {
5056
backgroundColor: black,
5157
color: white,
52-
fontWeight: 'bold'
58+
fontWeight: 'bold',
59+
padding: 20
5360
},
5461
input: {
55-
paddingTop: 10,
56-
paddingRight: 10,
57-
paddingBottom: 10,
58-
paddingLeft: 0,
62+
padding: 10,
63+
margin: 10,
5964
backgroundColor: white,
60-
color: black
65+
color: black,
66+
width: '80%'
6167
}
6268
});

components/DecksList.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ import { View, Text, StyleSheet, FlatList } from 'react-native';
33
import { gray, black } from '../utils/colors';
44
import { getAllDecks } from '../api';
55

6-
export default class DeckList extends React.Component {
6+
export default class DecksList extends React.Component {
77
constructor(props) {
88
super(props);
99
this.state = { decks: [] };
1010
}
1111

12-
componentDidMount() {
13-
getAllDecks().then(data => this.setState({ decks: Object.values(data) }));
12+
componentWillReceiveProps(nextProps) {
13+
if (nextProps.screenProps.currentScreen === 'Decks') {
14+
this.loadDecks();
15+
}
1416
}
1517

18+
loadDecks = () => getAllDecks().then(data => this.setState({ decks: Object.values(data ? data : []) }));
19+
1620
render() {
1721
return (
1822
<FlatList
1923
style={styles.container}
2024
data={this.state.decks}
25+
keyExtractor={(item, index) => index}
2126
renderItem={({ item }) => <Deck key={item.title} {...item} />}
2227
/>
2328
);

utils/helpers.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function getCurrentRouteName(navigationState) {
2+
if (!navigationState) {
3+
return null;
4+
}
5+
const route = navigationState.routes[navigationState.index];
6+
// dive into nested navigators
7+
if (route.routes) {
8+
return getCurrentRouteName(route);
9+
}
10+
return route.routeName;
11+
}

0 commit comments

Comments
 (0)