-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
246 lines (243 loc) · 6.39 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {
View,
Text,
FlatList,
TouchableOpacity,
Dimensions,
Animated,
Modal,
} from 'react-native';
import React, {useRef, useState} from 'react';
import {englishData} from './src/EnglishQuestions';
import QuestionItem from './QuestionItem';
const {height, width} = Dimensions.get('window');
const App = () => {
const [currentIndex, setCurrentIndex] = useState(1);
const [questions, setQuestions] = useState(englishData);
const listRef = useRef();
const [modalVisible, setModalVisible] = useState(false);
const OnSelectOption = (index, x) => {
const tempData = questions;
tempData.map((item, ind) => {
if (index == ind) {
if (item.marked !== -1) {
item.marked = -1;
} else {
item.marked = x;
}
}
});
let temp = [];
tempData.map(item => {
temp.push(item);
});
setQuestions(temp);
};
const getTextScore = () => {
let marks = 0;
questions.map(item => {
if (item.marked !== -1) {
marks = marks + 5;
}
});
return marks;
};
const reset = () => {
const tempData = questions;
tempData.map((item, ind) => {
item.marked = -1;
});
let temp = [];
tempData.map(item => {
temp.push(item);
});
setQuestions(temp);
};
return (
<View style={{flex: 1}}>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 20,
}}>
<Text
style={{
fontSize: 20,
fontWeight: '600',
marginLeft: 20,
color: '#000',
}}>
English Questions :{' ' + currentIndex + '/' + englishData.length}
</Text>
<Text
style={{
marginRight: 20,
fontSize: 20,
fontWeight: '600',
color: 'black',
}}
onPress={() => {
reset();
listRef.current.scrollToIndex({animated: true, index: 0});
}}>
Reset
</Text>
</View>
<View style={{marginTop: 30}}>
<FlatList
ref={listRef}
showsHorizontalScrollIndicator={false}
pagingEnabled
horizontal
onScroll={e => {
const x = e.nativeEvent.contentOffset.x / width + 1;
setCurrentIndex(x.toFixed(0));
}}
data={questions}
renderItem={({item, index}) => {
return (
<QuestionItem
data={item}
selectedOption={x => {
OnSelectOption(index, x);
}}
/>
);
}}
/>
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
position: 'absolute',
bottom: 50,
width: '100%',
}}>
<TouchableOpacity
style={{
backgroundColor: currentIndex > 1 ? 'purple' : 'gray',
height: 50,
width: 100,
borderRadius: 10,
marginLeft: 20,
justifyContent: 'center',
alignItems: 'center',
}}
onPress={() => {
console.log(parseInt(currentIndex) - 1);
if (currentIndex > 1) {
listRef.current.scrollToIndex({
animated: true,
index: parseInt(currentIndex) - 2,
});
}
}}>
<Text style={{color: '#fff'}}>Previous</Text>
</TouchableOpacity>
{currentIndex == 8 ? (
<TouchableOpacity
style={{
backgroundColor: 'green',
height: 50,
width: 100,
borderRadius: 10,
marginRight: 20,
justifyContent: 'center',
alignItems: 'center',
}}
onPress={() => {
setModalVisible(true);
}}>
<Text style={{color: '#fff'}}>Submit</Text>
</TouchableOpacity>
) : (
<TouchableOpacity
style={{
backgroundColor: 'purple',
height: 50,
width: 100,
borderRadius: 10,
marginRight: 20,
justifyContent: 'center',
alignItems: 'center',
}}
onPress={() => {
console.log(currentIndex);
if (questions[currentIndex - 1].marked !== -1) {
if (currentIndex < questions.length) {
listRef.current.scrollToIndex({
animated: true,
index: currentIndex,
});
}
}
}}>
<Text style={{color: '#fff'}}>Next</Text>
</TouchableOpacity>
)}
</View>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}>
<View
style={{
flex: 1,
backgroundColor: 'rgba(0,0,0,.5)',
justifyContent: 'center',
alignItems: 'center',
}}>
<View
style={{
backgroundColor: '#fff',
width: '90%',
borderRadius: 10,
}}>
<Text
style={{
fontSize: 30,
fontWeight: '800',
alignSelf: 'center',
marginTop: 20,
}}>
Text Score
</Text>
<Text
style={{
fontSize: 40,
fontWeight: '800',
alignSelf: 'center',
marginTop: 20,
color: 'green',
}}>
{getTextScore()}
</Text>
<TouchableOpacity
style={{
alignSelf: 'center',
height: 40,
padding: 10,
borderWidth: 1,
borderRadius: 10,
marginTop: 20,
marginBottom: 20,
}}
onPress={() => {
setModalVisible(!modalVisible);
}}>
<Text>Close</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
</View>
);
};
export default App;