-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
95 lines (80 loc) · 2.47 KB
/
App.tsx
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
import React, {useState} from 'react';
import {SafeAreaView, ScrollView, StatusBar, View} from 'react-native';
import NounPage from './src/pages/NounPage';
import NOUNS from './src/constants/noun';
import AdjectivePage from './src/pages/AdjectivePage';
import {ADJECTIVES} from './src/constants/adjective';
import VerbPage from './src/pages/VerbPage';
import VERBS from './src/constants/verb';
import {AdjectiveType, NounType, VerbType} from './src/constants/types';
import {isNoun, isVerb} from './src/constants/utils';
const PAGE_TYPES = {
NOUN: 'NOUN',
ADJECTIVE: 'ADJECTIVE',
VERB: 'VERB',
};
const getRandomNumber = (max: number) => {
return Math.floor(Math.random() * max);
};
const App: () => React.ReactElement = () => {
const [page, setPage] = useState({
pageIndex: 0,
pageType: PAGE_TYPES.NOUN,
});
const WORDS: (AdjectiveType | NounType | VerbType)[] = [
...NOUNS,
...ADJECTIVES,
...VERBS,
];
const goToNextPage = () => {
const delay = page.pageType === PAGE_TYPES.NOUN ? 1000 : 500;
const timeoutId = setTimeout(() => {
clearTimeout(timeoutId);
const pageNumber = getRandomNumber(WORDS.length);
const nextPageIndex = pageNumber === WORDS.length ? 0 : pageNumber;
const nextWord = WORDS[nextPageIndex];
const nextPage = {
pageIndex: nextPageIndex,
pageType: isNoun(nextWord)
? PAGE_TYPES.NOUN
: isVerb(nextWord)
? PAGE_TYPES.VERB
: PAGE_TYPES.ADJECTIVE,
};
setPage(nextPage);
}, delay);
};
const handleAnswer = (answer: string) => {
if (answer === (WORDS[page.pageIndex] as NounType).article) {
goToNextPage();
}
};
return (
<SafeAreaView>
<StatusBar />
<ScrollView contentInsetAdjustmentBehavior="automatic">
<View>
{page.pageType === PAGE_TYPES.NOUN && (
<NounPage
noun={WORDS[page.pageIndex] as NounType}
handleAnswer={handleAnswer}
/>
)}
{page.pageType === PAGE_TYPES.ADJECTIVE && (
<AdjectivePage
adjective={WORDS[page.pageIndex] as AdjectiveType}
onPressNext={goToNextPage}
/>
)}
{page.pageType === PAGE_TYPES.VERB && (
<VerbPage
verb={WORDS[page.pageIndex] as VerbType}
onPressNext={goToNextPage}
/>
)}
</View>
</ScrollView>
</SafeAreaView>
);
};
export default App;