Skip to content

Commit 1beaf6d

Browse files
committed
Add screen for creating new cards
1 parent 8f0587d commit 1beaf6d

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed

api.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ export const saveDeckTitle = title =>
2525

2626
export const getDeck = id =>
2727
AsyncStorage.getItem(STORAGE_KEY)
28-
.then(d => d.json())
29-
.then(data => data[id])
28+
.then(data => {
29+
data = JSON.parse(data);
30+
return data[id];
31+
})
3032
.catch(err => {
3133
console.log(err);
3234
return null;
3335
});
3436

3537
export const addCardToDeck = (title, card) =>
3638
AsyncStorage.getItem(STORAGE_KEY)
37-
.then(d => d.json())
3839
.then(data => {
39-
data[title].questions.concat(card);
40-
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
40+
data = JSON.parse(data);
41+
data[title].questions = data[title].questions.concat(card);
42+
return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
4143
})
4244
.catch(err => {
4345
console.log(err);

components/AddCard.js

+40-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
2-
import { StyleSheet, Text, View } from 'react-native';
2+
import { View, Text, StyleSheet, TextInput, KeyboardAvoidingView, TouchableOpacity } from 'react-native';
33
import { black, white } from '../utils/colors';
4+
import { styles } from './AddDeck';
5+
import { addCardToDeck } from '../api';
46

57
export default class AddCard extends React.Component {
68
static navigationOptions = ({ navigation }) => ({
@@ -11,22 +13,47 @@ export default class AddCard extends React.Component {
1113
title: `Add Card for ${navigation.state.params.deckId}`
1214
});
1315

16+
onSubmit = () => {
17+
const { question, answer } = this.state;
18+
const { deckId } = this.props.navigation.state.params;
19+
this.setState({ question: '', answer: '' }, () =>
20+
addCardToDeck(deckId, { question, answer }).then(() => this.props.navigation.goBack())
21+
);
22+
};
23+
24+
state = {
25+
question: '',
26+
answer: ''
27+
};
28+
1429
render() {
1530
return (
1631
<View style={styles.container}>
17-
<Text style={styles.deckTitle}>Add Card {this.props.navigation.state.params.deckId}</Text>
32+
<TextInput
33+
style={styles.input}
34+
placeholder="Question"
35+
onChangeText={text => {
36+
this.setState({ question: text });
37+
}}
38+
value={this.state.question}
39+
spellCheck={false}
40+
underlineColorAndroid={black}
41+
/>
42+
<TextInput
43+
style={styles.input}
44+
placeholder="Answer"
45+
onChangeText={text => {
46+
this.setState({ answer: text });
47+
}}
48+
value={this.state.answer}
49+
spellCheck={false}
50+
underlineColorAndroid={black}
51+
/>
52+
<TouchableOpacity onPress={this.onSubmit}>
53+
<Text style={styles.submitBtn}>Submit</Text>
54+
</TouchableOpacity>
55+
<KeyboardAvoidingView behavior="padding" keyboardVerticalOffset={100} />
1856
</View>
1957
);
2058
}
2159
}
22-
23-
const styles = StyleSheet.create({
24-
container: {
25-
flex: 1,
26-
justifyContent: 'center',
27-
alignItems: 'center'
28-
},
29-
deckTitle: {
30-
fontSize: 20
31-
}
32-
});

components/AddDeck.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class AddDeck extends React.Component {
1212
onSubmit = () => {
1313
const title = this.state.title;
1414
this.setState({ title: '' }, () =>
15-
saveDeckTitle(title).then(() => this.props.navigation.navigate('DecksList'))
15+
saveDeckTitle(title).then(() => this.props.navigation.goBack())
1616
);
1717
};
1818

@@ -39,7 +39,7 @@ export default class AddDeck extends React.Component {
3939
}
4040
}
4141

42-
const styles = StyleSheet.create({
42+
export const styles = StyleSheet.create({
4343
container: {
4444
flex: 1,
4545
justifyContent: 'center',

components/DeckDetail.js

+41-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
22
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
33
import { black, white } from '../utils/colors';
4+
import { getDeck } from '../api';
5+
import { gray } from '../utils/colors';
46

57
export default class DeckDetail extends React.Component {
68
static navigationOptions = ({ navigation }) => ({
@@ -11,16 +13,35 @@ export default class DeckDetail extends React.Component {
1113
title: navigation.state.params.deckId
1214
});
1315

16+
state = {};
17+
18+
getDeckDetails = () =>
19+
getDeck(this.props.navigation.state.params.deckId).then(data => this.setState({ ...data }));
20+
21+
componentDidMount() {
22+
this.getDeckDetails();
23+
}
24+
25+
componentWillReceiveProps(nextProps) {
26+
if (nextProps.screenProps.currentScreen === 'DeckDetail') {
27+
this.getDeckDetails();
28+
}
29+
}
30+
1431
render() {
1532
const id = this.props.navigation.state.params.deckId;
1633
return (
1734
<View style={styles.container}>
18-
<TouchableOpacity
19-
onPress={() => this.props.navigation.navigate('AddCard', { deckId: id })}
20-
>
35+
<Text style={styles.deckTitle}>{id}</Text>
36+
<Text style={styles.cardsCount}>
37+
{this.state.questions && this.state.questions.length} Cards
38+
</Text>
39+
<TouchableOpacity onPress={() => this.props.navigation.navigate('AddCard', { deckId: id })}>
2140
<Text style={styles.addCardBtn}>Add Card</Text>
2241
</TouchableOpacity>
23-
<Text style={styles.deckTitle}>Details - {id}</Text>
42+
<TouchableOpacity onPress={() => this.props.navigation.navigate('AddCard', { deckId: id })}>
43+
<Text style={styles.quizStartBtn}>Start Quiz</Text>
44+
</TouchableOpacity>
2445
</View>
2546
);
2647
}
@@ -30,12 +51,27 @@ const styles = StyleSheet.create({
3051
container: {
3152
flex: 1,
3253
justifyContent: 'center',
33-
alignItems: 'center'
54+
alignItems: 'center',
55+
backgroundColor: white
3456
},
3557
deckTitle: {
3658
fontSize: 20
3759
},
60+
cardsCount: {
61+
fontSize: 16,
62+
color: gray,
63+
margin: 5
64+
},
3865
addCardBtn: {
66+
backgroundColor: white,
67+
color: black,
68+
fontWeight: 'bold',
69+
padding: 20,
70+
borderWidth: 1,
71+
borderColor: black,
72+
margin: 10
73+
},
74+
quizStartBtn: {
3975
backgroundColor: black,
4076
color: white,
4177
fontWeight: 'bold',

0 commit comments

Comments
 (0)