-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
123 lines (116 loc) · 3.27 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
114
115
116
117
118
119
120
121
122
123
import React, {useState, useEffect} from 'react';
import Home from './components/Home';
import RadicalsList from './components/RadicalsList';
import RadicalDetail from './components/RadicalDetail';
import Quiz from './components/Quiz';
import SplashScreen from 'react-native-splash-screen';
import NetInfo from '@react-native-community/netinfo';
import {
getAllRadicals,
getLearntInfo,
getToLearnRadicals,
updateRadical,
} from './db';
// https://stackoverflow.com/a/2450976
const shuffle = (array) => {
let currentIndex = array.length,
temporaryValue,
randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
const App = () => {
const [allRadicals, setAllRadicals] = useState([]);
const [queue, setQueue] = useState([]);
const [learning, setLearning] = useState(false);
const [home, setHome] = useState(true);
const [connected, setConnected] = useState(false);
const getQuiz = () => {
if (allRadicals.length !== 214 || !queue.length) {
return;
}
const radical = queue[0].radical;
const isGuessMeaning = Math.random() < 0.5;
const answer = isGuessMeaning ? radical.meaning : radical.radical[0];
const question = isGuessMeaning ? radical.radical[0] : radical.meaning;
const options = [answer];
for (let i = 0; i < 3; i++) {
let option;
do {
const index = Math.floor(Math.random() * allRadicals.length);
option = isGuessMeaning
? allRadicals[index].meaning
: allRadicals[index].radical[0];
} while (option === answer);
options.push(option);
}
return {
question,
answer,
options: shuffle(options),
};
};
useEffect(() => {
SplashScreen.hide();
NetInfo.addEventListener((state) => {
setConnected(state.isConnected);
});
}, []);
useEffect(() => {
if (queue.length) {
return;
}
setAllRadicals(getAllRadicals());
setQueue(shuffle(getToLearnRadicals()));
}, [queue]);
return (
<>
{home && (
<Home
length={getLearntInfo().length}
text={getLearntInfo().text}
setHome={setHome}
setLearning={setLearning}
learnAvailable={queue.length > 0}
/>
)}
{!home && !learning && (
<RadicalsList
radicals={allRadicals}
setHome={setHome}
setLearning={setLearning}
learnAvailable={queue.length > 0}
/>
)}
{learning && queue.length && queue[0].quiz && (
<Quiz
queue={queue}
setQueue={setQueue}
quiz={getQuiz()}
updateRadical={updateRadical}
setHome={setHome}
setLearning={setLearning}
connected={connected}
/>
)}
{learning && queue.length && !queue[0].quiz && (
<RadicalDetail
queue={queue}
setQueue={setQueue}
setHome={setHome}
setLearning={setLearning}
/>
)}
</>
);
};
export default App;