-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
113 lines (104 loc) · 2.86 KB
/
App.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useEffect, useState } from 'react';
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import Home from './src/components/Home';
import Deck from './src/components/Deck';
import NewQuestion from './src/components/NewQuestion';
import {
getDecks,
saveDeckTitle,
removeDeck,
addCardToDeck,
} from './src/utils/api';
import Quiz from './src/components/Quiz';
import { setLocalNotification } from './src/utils/helpers';
const MyTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
card: '#694fad',
text: '#f5f5f5',
},
};
const Stack = createStackNavigator();
const App = () => {
const [decks, setDecks] = useState({});
useEffect(() => {
setLocalNotification();
getDecks().then((results) => setDecks(results));
}, []);
const addQuestion = (deckId, question) => {
addCardToDeck({
title: deckId,
card: question,
}).then(() => {
setDecks((prevDecks) => ({
...prevDecks,
[deckId]: {
...prevDecks[deckId],
questions: [...prevDecks[deckId].questions, question],
},
}));
});
};
const deleteDeck = (deckId) => {
removeDeck(deckId).then(() => {
setDecks(({ [deckId]: toRemove, ...rest }) => rest);
});
};
const addDeck = (deckId) => {
saveDeckTitle(deckId).then(() =>
setDecks((prevDecks) => ({
...prevDecks,
[deckId]: {
title: deckId,
questions: [],
},
})),
);
};
return (
<NavigationContainer theme={MyTheme}>
<Stack.Navigator>
<Stack.Screen name="Home">
{() => <Home decks={decks} addDeck={addDeck} />}
</Stack.Screen>
<Stack.Screen name="Deck">
{({ route: { params }, navigation }) => (
<Deck
navigate={navigation.navigate}
deleteDeck={() => {
deleteDeck(params.deckId);
navigation.navigate('Home');
}}
{...decks[params.deckId]}
/>
)}
</Stack.Screen>
<Stack.Screen name="New Question">
{({ route: { params }, navigation }) => (
<NewQuestion
navigate={navigation.navigate}
deckId={params.deckId}
handleSubmit={(question) => addQuestion(params.deckId, question)}
/>
)}
</Stack.Screen>
<Stack.Screen name="Quiz">
{({
route: {
params: { deckId },
},
navigation,
}) => (
<Quiz
questions={decks[deckId].questions}
goBack={() => navigation.navigate('Deck', { deckId })}
/>
)}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;