You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.6 KiB

3 months ago
import React, { useEffect, useState, useCallback } from "react";
import {
View,
ScrollView,
StyleSheet,
Text,
Image,
BackHandler,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useNavigation, useRoute, useFocusEffect } from "@react-navigation/native";
3 months ago
import { Button, ButtonContainer } from "../components/Button";
import { Banner } from "../components/Banner";
import { colors, texts, examScheme } from "../components/Variables";
import aerodynamicsQuestions from "../data/aerodynamics";
import firstAidQuestions from "../data/firstAid";
import flightSafetyQuestions from "../data/flightSafety";
import instrumentsQuestions from "../data/instruments";
import legislationQuestions from "../data/legislation";
import materialsQuestions from "../data/materials";
import meteorologyQuestions from "../data/meteorology";
import physiopathologyQuestions from "../data/physiopathology";
import pilotingTechniquesQuestions from "../data/pilotingTechniques";
6 years ago
const allQuestions = {
aerodynamics: aerodynamicsQuestions,
firstAid: firstAidQuestions,
flightSafety: flightSafetyQuestions,
instruments: instrumentsQuestions,
legislation: legislationQuestions,
materials: materialsQuestions,
meteorology: meteorologyQuestions,
physiopathology: physiopathologyQuestions,
3 months ago
pilotingTechniques: pilotingTechniquesQuestions,
3 months ago
};
const header = require("../assets/header.png");
6 years ago
const styles = StyleSheet.create({
3 months ago
container: { backgroundColor: colors.dark_blue, flex: 1 },
3 months ago
safearea: {
flex: 1,
marginTop: 0,
justifyContent: "space-between",
paddingHorizontal: 20,
paddingBottom: 40,
},
headerContainer: {
marginTop: -40,
alignItems: "center",
justifyContent: "center",
width: "100%",
height: 200,
},
3 months ago
header: { width: "100%" },
3 months ago
box: {
marginTop: 30,
borderColor: colors.black_alpha,
borderWidth: 1,
padding: 15,
borderRadius: 5,
backgroundColor: colors.white_alpha,
},
text: {
color: colors.white,
fontSize: 20,
textAlign: "center",
fontWeight: "600",
paddingTop: 0,
},
textCode: {
color: colors.white,
fontSize: 12,
textAlign: "center",
fontWeight: "500",
paddingTop: 10,
paddingBottom: 0,
},
textBig: {
color: colors.white,
fontSize: 22,
textAlign: "center",
fontWeight: "400",
paddingBottom: 15,
textTransform: "uppercase",
textShadowColor: "rgba(0, 0, 0, 0.75)",
textShadowOffset: { width: -1, height: 1 },
textShadowRadius: 10,
},
bannerContainer: { flex: 1, alignItems: "center", justifyContent: "center" },
3 months ago
});
6 years ago
3 months ago
const Recap = () => {
const navigation = useNavigation();
const route = useRoute();
const [storeWrongAnswers, setStoreWrongAnswers] = useState([]);
3 months ago
// Load stored wrong answers
useEffect(() => {
AsyncStorage.getItem("storeWrongAnswers").then((value) => {
3 months ago
if (value) {
3 months ago
setStoreWrongAnswers(JSON.parse(value));
3 months ago
}
});
3 months ago
}, []);
6 years ago
3 months ago
const handleBackButton = useCallback(() => {
3 months ago
const tmpQuestions = [];
6 years ago
3 months ago
AsyncStorage.getItem("setupData").then((value) => {
3 months ago
const setupData = JSON.parse(value) || {};
3 months ago
examScheme.forEach((elem) => {
3 months ago
let currentSection = setupData.excludeDelta
3 months ago
? allQuestions[elem.section].filter((item) => !item.delta)
3 months ago
: allQuestions[elem.section];
3 months ago
for (let i = 0; i < elem.questions; i++) {
const currentIndex = Math.floor(Math.random() * currentSection.length);
tmpQuestions.push(currentSection[currentIndex]);
3 months ago
currentSection = currentSection.filter((_, idx) => idx !== currentIndex);
}
3 months ago
});
3 months ago
navigation.navigate("Splash", {
examQuestions: tmpQuestions,
3 months ago
storeWrongAnswers,
3 months ago
});
});
3 months ago
return true;
3 months ago
}, [navigation, storeWrongAnswers]);
// Attach hardware back handler
useFocusEffect(
useCallback(() => {
3 months ago
this.backHandler = BackHandler.addEventListener( 'hardwareBackPress', this.handleBackButton)
return () => this.backHandler?.remove();
3 months ago
}, [handleBackButton])
);
const questions = route.params?.wrongAnswers || [];
return (
<View style={styles.container}>
<View style={styles.headerContainer}>
<Image source={header} style={styles.header} resizeMode="contain" />
</View>
6 years ago
3 months ago
<Text style={styles.textBig}>aaa{texts.recapTitle}</Text>
<ScrollView>
<SafeAreaView style={styles.safearea}>
{questions.map((question) => (
<View style={styles.box} key={question.id}>
<Text style={styles.textCode}>{question.id}</Text>
<Text style={styles.text}>{question.question}</Text>
<ButtonContainer>
{question.answers.map((answer) => (
<Button
key={answer.id}
text={answer.text}
colorize={{
id: answer.id,
clicked: question.clicked,
answered: true,
isCorrect: answer.correct,
}}
/>
))}
</ButtonContainer>
6 years ago
</View>
3 months ago
))}
<View style={{ marginTop: 20 }}>
<Button
color={colors.white_alpha2}
hasShadow
hasBg
text={texts.restart}
onPress={handleBackButton}
/>
</View>
3 months ago
</SafeAreaView>
<View style={styles.bannerContainer}>
<Banner />
</View>
</ScrollView>
</View>
);
};
6 years ago
3 months ago
export default Recap;