-
Notifications
You must be signed in to change notification settings - Fork 1
/
StatisticsPage.tsx
125 lines (115 loc) · 4.11 KB
/
StatisticsPage.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
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
import React from "react";
import { ScrollView, View } from "react-native";
import { Button, useTheme, ActivityIndicator } from "react-native-paper";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import { useWindowDimensions } from "react-native";
import { useNavigation } from "@react-navigation/native";
import { PreferencesContext } from "../Contexts/PreferencesContext";
import { useFocusEffect } from "@react-navigation/core";
import TotalStatistics from "../Components/Statistics/TotalStatistics";
import Alert from "react-native-awesome-alerts";
import { rgba } from "polished";
import { Statistics } from "../Functions/Api/Statistics";
const StatisticsPage = () => {
const theme = useTheme();
const navigation: any = useNavigation();
const size = useWindowDimensions();
const reSize = Math.min(size.width, size.height);
const { updateLearnedLessons, learnedLessons } =
React.useContext(PreferencesContext);
const [isLoading, setLoading] = React.useState<boolean>(true);
const [totalStatistics, setTotalStatistics] = React.useState<any>();
const [warningVisible, setWarningVisible] = React.useState(false);
const showWarningButton = () => setWarningVisible(true);
const hideWarningButton = () => setWarningVisible(false);
async function deleteUserStatistics() {
await Statistics.deleteStatistics().then(() => {
updateLearnedLessons([]);
});
}
async function getUserStatistics() {
await Statistics.getStatistics().then((res: any) => {
if (res) {
setTotalStatistics(res);
} else {
console.log("Cannot get user statistics!");
}
setLoading(false);
});
}
useFocusEffect(
React.useCallback(() => {
getUserStatistics();
}, [])
);
if (isLoading) {
return (
<SafeAreaProvider>
<SafeAreaView style={{ height: "100%", width: "100%" }}>
<ActivityIndicator animating={true} color={theme.colors.primary} />
</SafeAreaView>
</SafeAreaProvider>
);
} else {
return (
<SafeAreaProvider>
<SafeAreaView style={{ height: "100%", width: "100%" }}>
<ScrollView>
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
}}
>
<TotalStatistics
totalScore={totalStatistics.totalScore}
numGamesPlayed={totalStatistics.numGamesPlayed}
fastestSolveTime={totalStatistics.fastestSolveTime}
averageSolveTime={totalStatistics.averageSolveTime}
totalSolveTime={totalStatistics.totalSolveTime}
numHintsUsed={totalStatistics.numHintsUsed}
numWrongCellsPlayed={totalStatistics.numWrongCellsPlayed}
/>
<Button
mode="contained"
onPress={() => {
showWarningButton();
}}
>
Delete Statistics
</Button>
</View>
</ScrollView>
</SafeAreaView>
<Alert
show={warningVisible}
title="Delete Statistics Warning"
message={`\n\nThis action will delete ALL of your current progress.\nThis includes lesson completions. Do you wish to proceed?\n\n`}
messageStyle={{ maxWidth: 500 }}
alertContainerStyle={{
backgroundColor: rgba(theme.colors.background, 0.3),
}}
showConfirmButton={true}
showCancelButton={true}
closeOnTouchOutside={true}
closeOnHardwareBackPress={true}
confirmText={"Delete"}
cancelText={"Cancel"}
confirmButtonColor="red"
cancelButtonColor={theme.colors.primary}
onConfirmPressed={() => {
deleteUserStatistics().then(() => {
hideWarningButton();
});
}}
onCancelPressed={() => {
hideWarningButton();
}}
overlayStyle={{ backgroundColor: "transparent" }}
/>
</SafeAreaProvider>
);
}
};
export default StatisticsPage;