Advertisement
Guest User

LatihanSoal

a guest
Feb 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. /* eslint-disable react-native/no-color-literals */
  2. /* eslint-disable import/first */
  3. /* eslint-disable no-unused-vars */
  4. import React from 'react';
  5. import {View, Image, Text, TouchableOpacity, Alert, ScrollView, AsyncStorage, Button} from 'react-native';
  6. import styles from './styles';
  7. import PropTypes from 'prop-types';
  8. import firebase from 'firebase';
  9. import IMAGES from '../../configs/images';
  10.  
  11. export default class Component extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. noSoal: 0,
  16. quiz_items: {},
  17. arrSoal: this.shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
  18. jawab: [],
  19. points: 0,
  20. isClicked: false
  21. };
  22. }
  23.  
  24. getData = (firstIndex) => {
  25. firebase
  26. .app()
  27. .database()
  28. .ref(`quiz_items/quiz${firstIndex}`)
  29. .on('value', snap => {
  30. this.setState({quiz_items: snap.val()});
  31. });
  32.  
  33. const arrayJawab = this.shuffle([1, 2, 3, 0]);
  34. const soals = [];
  35.  
  36. for (let indexArr = 0; indexArr < arrayJawab.length; indexArr++) {
  37. firebase
  38. .app()
  39. .database()
  40. .ref(
  41. `quiz_items/quiz${firstIndex}/answers/answer${arrayJawab[indexArr]}/text`,
  42. )
  43. .on('value', snap => {
  44. soals[indexArr] = snap.val();
  45. });
  46. }
  47.  
  48. this.setState({jawab: soals});
  49. };
  50.  
  51. answerPressed = answer => {
  52. if (answer == this.state.quiz_items.correct) {
  53. this.setState({points: this.state.points + 1});
  54. } else {
  55. this.setState({points: this.state.points - 1});
  56. }
  57. };
  58.  
  59. pressNext = async () => {
  60. this.setState({noSoal: this.state.noSoal + 1}, () => {
  61. this.getData(this.state.arrSoal[this.state.noSoal]);
  62. });
  63. };
  64.  
  65. pressPrevious = () => {
  66. this.setState({noSoal: this.state.noSoal - 1}, () => {
  67. this.getData(this.state.arrSoal[this.state.noSoal]);
  68. });
  69. };
  70.  
  71. pressFinish = async () => {
  72. this.props.navigation.navigate('FinishLatihan');
  73. await AsyncStorage.setItem('points', this.state.points.toString())
  74. };
  75.  
  76. shuffle = array => {
  77. var currentIndex = array.length,
  78. temporaryValue,
  79. randomIndex;
  80. while (0 !== currentIndex) {
  81. randomIndex = Math.floor(Math.random() * currentIndex);
  82. currentIndex -= 1;
  83. temporaryValue = array[currentIndex];
  84. array[currentIndex] = array[randomIndex];
  85. array[randomIndex] = temporaryValue;
  86. }
  87. return array;
  88. };
  89.  
  90. jawaban = () => {
  91. return this.state.jawab.map((data) => {
  92. return (
  93. <TouchableOpacity
  94. ref="myText"
  95. style={{justifyContent: 'center'}}
  96. onPress={this.answerPressed.bind(this, data)}
  97. >
  98. <View
  99. style={{
  100. width: 320,
  101. height: 55,
  102. borderWidth: 1,
  103. borderColor: '#A3A3A3',
  104. marginTop: 30,
  105. marginRight: 20,
  106. marginLeft: 20,
  107. borderRadius: 5,
  108. }}>
  109. <Text
  110. style={{
  111. margin: 15,
  112. fontSize: 15,
  113. color: '#515151',
  114. alignItems: 'center',
  115. }}>
  116. {data}
  117. </Text>
  118. </View>
  119. </TouchableOpacity>
  120. );
  121. });
  122. };
  123.  
  124. selesai = () => {
  125. if (this.state.noSoal == this.state.arrSoal.length - 1) {
  126. return (
  127. <View style={{width: '80%'}}>
  128. <TouchableOpacity
  129. style={{marginLeft: 40, alignItems: 'flex-start'}}
  130. onPress={this.pressPrevious}>
  131. <Image
  132. source={IMAGES.previous}
  133. resizeMode="contain"
  134. style={{width: 25, height: 50}}
  135. />
  136. </TouchableOpacity>
  137. <TouchableOpacity
  138. style={{marginLeft: 20, alignItems: 'flex-start'}}
  139. onPress={this.pressFinish}>
  140. <Image
  141. source={IMAGES.buttonSelesai}
  142. resizeMode="contain"
  143. style={{width: 155, height: 50}}
  144. />
  145. </TouchableOpacity>
  146. </View>
  147. );
  148. } else if (this.state.noSoal == 0) {
  149. return (
  150. <View style={{marginTop: 40}}>
  151. <TouchableOpacity
  152. style={{marginRight: 40, alignItems: 'flex-end'}}
  153. onPress={this.pressNext}>
  154. <Image
  155. source={IMAGES.next}
  156. resizeMode="contain"
  157. style={{width: 25, height: 50}}
  158. />
  159. </TouchableOpacity>
  160. </View>
  161. );
  162. } else {
  163. return (
  164. <View style={{width: '20%'}}>
  165. <TouchableOpacity
  166. style={{marginLeft: 40, alignItems: 'flex-start'}}
  167. onPress={this.pressPrevious}>
  168. <Image
  169. source={IMAGES.previous}
  170. resizeMode="contain"
  171. style={{width: 25, height: 50}}
  172. />
  173. </TouchableOpacity>
  174. <TouchableOpacity
  175. style={{marginRight: 40, alignItems: 'flex-end'}}
  176. onPress={this.pressNext}>
  177. <Image
  178. source={IMAGES.next}
  179. resizeMode="contain"
  180. style={{width: 25, height: 50}}
  181. />
  182. </TouchableOpacity>
  183. </View>
  184. );
  185. }
  186. };
  187.  
  188. componentDidMount() {
  189. const arrayJawab = this.shuffle([1, 2, 3, 0]);
  190. const soals = [];
  191.  
  192. firebase
  193. .app()
  194. .database()
  195. .ref(`quiz_items/quiz${this.state.arrSoal[this.state.noSoal]}`)
  196. .on('value', snap => {
  197. this.setState({quiz_items: snap.val()});
  198. });
  199.  
  200. for (let indexArr = 0; indexArr < arrayJawab.length; indexArr++) {
  201. firebase
  202. .app()
  203. .database()
  204. .ref(
  205. `quiz_items/quiz${
  206. this.state.arrSoal[this.state.noSoal]
  207. }/answers/answer${arrayJawab[indexArr]}/text`,
  208. )
  209. .on('value', snap => {
  210. soals[indexArr] = snap.val();
  211. });
  212. }
  213.  
  214. this.setState({jawab: soals});
  215. console.log(this.state.noSoal);
  216. }
  217.  
  218. render() {
  219. return (
  220. <View style={{flex: 1, backgroundColor: '#fff'}}>
  221. <View
  222. style={{
  223. width: 66,
  224. height: 30,
  225. backgroundColor: '#AEE7F9',
  226. margin: 20,
  227. borderRadius: 5,
  228. }}>
  229. <Text
  230. style={{
  231. textAlign: 'center',
  232. marginTop: 5,
  233. fontSize: 18,
  234. fontWeight: 'bold',
  235. }}>
  236. {this.state.noSoal + 1}/10
  237. </Text>
  238. </View>
  239.  
  240. <View style={{paddingRight: 25, paddingLeft: 25}}>
  241. <Text
  242. style={{
  243. marginTop: 5,
  244. fontSize: 16,
  245. lineHeight: 25,
  246. color: '#515151',
  247. }}>
  248. {this.state.quiz_items.question}
  249. </Text>
  250. </View>
  251.  
  252. {this.jawaban()}
  253.  
  254. <View
  255. style={{
  256. flexDirection: 'row',
  257. justifyContent: 'space-between',
  258. marginTop: 40,
  259. }}>
  260. </View>
  261.  
  262. {this.selesai()}
  263. pressExit = () => {
  264. this.props.navigation.navigate('Kuis')
  265. };
  266. </View>
  267. );
  268. }
  269. }
  270.  
  271. Component.propTypes = {
  272. navigation: PropTypes.object.isRequired,
  273. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement