|
@@ -1,159 +1,196 @@
|
|
|
-import React, { useEffect, useState } from 'react';
|
|
|
+import React, { useCallback, useState } from 'react';
|
|
|
import { FlatList, View, Text } from 'react-native';
|
|
|
import { Image } from 'expo-image';
|
|
|
+import { useFocusEffect } from '@react-navigation/native';
|
|
|
|
|
|
import { Header, Loading, PageWrapper } from '../../../../components';
|
|
|
-import {
|
|
|
- getStatistic,
|
|
|
- StatisticType,
|
|
|
- Type2,
|
|
|
- Type1,
|
|
|
- Type3
|
|
|
-} from '../../../../database/statisticsService';
|
|
|
+import { getStatistic, StatisticType, ListType } from '../../../../database/statisticsService';
|
|
|
import { API_HOST } from '../../../../constants';
|
|
|
import { Colors } from '../../../../theme';
|
|
|
import { getFontSize } from '../../../../utils';
|
|
|
+import {
|
|
|
+ isType1,
|
|
|
+ isType2,
|
|
|
+ isType3,
|
|
|
+ isType4,
|
|
|
+ isType5,
|
|
|
+ isType6,
|
|
|
+ isType7,
|
|
|
+ isType8,
|
|
|
+ isType9
|
|
|
+} from './temp';
|
|
|
|
|
|
const StatisticsListScreen = ({ route }) => {
|
|
|
const title = route.params.title;
|
|
|
const url1 = route.params.url1;
|
|
|
+ const url2 = route.params.url2;
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
const [statistic, setStatistic] = useState<StatisticType | null>(null);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- const data = getStatistic(url1);
|
|
|
+ useFocusEffect(
|
|
|
+ useCallback(() => {
|
|
|
+ function fetchStatistic() {
|
|
|
+ const data = getStatistic(url1, url2);
|
|
|
|
|
|
- if (!data) return;
|
|
|
+ if (!data) return;
|
|
|
|
|
|
- setStatistic(JSON.parse(data as unknown as string) as unknown as StatisticType);
|
|
|
- setIsLoading(false);
|
|
|
- }, []);
|
|
|
+ setStatistic(JSON.parse(data as unknown as string) as unknown as StatisticType);
|
|
|
+ setIsLoading(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ fetchStatistic();
|
|
|
+ }, [url1])
|
|
|
+ );
|
|
|
|
|
|
if (isLoading) return <Loading />;
|
|
|
if (!statistic) return null;
|
|
|
|
|
|
- console.log(statistic);
|
|
|
+ const Item = (data: { name: string; flag: string; cnt: number | string; index: number }) => (
|
|
|
+ <View
|
|
|
+ style={{
|
|
|
+ display: 'flex',
|
|
|
+ alignItems: 'center',
|
|
|
+ flexDirection: 'row',
|
|
|
+ flex: 1,
|
|
|
+ gap: 8
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <Text style={{ color: Colors.DARK_BLUE, fontSize: getFontSize(18), fontWeight: '700' }}>
|
|
|
+ {data.index + 1}
|
|
|
+ </Text>
|
|
|
+ <Image
|
|
|
+ style={{ width: 36, height: 36, borderRadius: 18 }}
|
|
|
+ source={{ uri: API_HOST + '/img/flags_new/' + data.flag }}
|
|
|
+ />
|
|
|
+ <View
|
|
|
+ style={{
|
|
|
+ flex: 1,
|
|
|
+ display: 'flex',
|
|
|
+ flexDirection: 'row',
|
|
|
+ justifyContent: 'space-between',
|
|
|
+ alignItems: 'center'
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <Text style={{ width: '75%', color: Colors.DARK_BLUE, fontWeight: '700' }}>
|
|
|
+ {data.name}
|
|
|
+ </Text>
|
|
|
+ <Text style={{ color: Colors.DARK_BLUE, fontWeight: '700' }}>{data.cnt}</Text>
|
|
|
+ </View>
|
|
|
+ </View>
|
|
|
+ );
|
|
|
|
|
|
- const renderItem = ({ item, index }: { index: number; item: Type1 | Type2 | Type3 }) => {
|
|
|
- if ('region_id' in item) {
|
|
|
+ const renderItem = ({ item, index }: { index: number; item: ListType }) => {
|
|
|
+ if (isType1(item) || isType4(item) || isType5(item)) {
|
|
|
return (
|
|
|
- <View
|
|
|
- style={{
|
|
|
- display: 'flex',
|
|
|
- alignItems: 'center',
|
|
|
- flexDirection: 'row',
|
|
|
- flex: 1,
|
|
|
- gap: 8
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontSize: getFontSize(18), fontWeight: '700' }}>
|
|
|
- {index + 1}
|
|
|
- </Text>
|
|
|
- <Image
|
|
|
- style={{ width: 36, height: 36, borderRadius: 18 }}
|
|
|
- source={{ uri: API_HOST + '/img/flags_new/' + (item.flag ?? item.region_flag) }}
|
|
|
- />
|
|
|
- <View
|
|
|
- style={{
|
|
|
- flex: 1,
|
|
|
- display: 'flex',
|
|
|
- flexDirection: 'row',
|
|
|
- justifyContent: 'space-between',
|
|
|
- alignItems: 'center'
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ width: '75%', color: Colors.DARK_BLUE, fontWeight: '700' }}>
|
|
|
- {item.region_name ?? item.region}
|
|
|
- </Text>
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontWeight: '700' }}>{item.cnt}</Text>
|
|
|
- </View>
|
|
|
- </View>
|
|
|
+ <>
|
|
|
+ {'megaregion' in item && (
|
|
|
+ <View>
|
|
|
+ {item.megaregion !== statistic.ranking[index - 1]?.megaregion ? (
|
|
|
+ <Text
|
|
|
+ style={{
|
|
|
+ color: Colors.DARK_BLUE,
|
|
|
+ fontSize: getFontSize(14),
|
|
|
+ fontWeight: '700',
|
|
|
+ textAlign: 'center',
|
|
|
+ marginTop: 10,
|
|
|
+ marginBottom: 15
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.megaregion as string}
|
|
|
+ </Text>
|
|
|
+ ) : null}
|
|
|
+ </View>
|
|
|
+ )}
|
|
|
+ <Item cnt={item.cnt} flag={item.flag} index={index} name={item.region_name} />
|
|
|
+ </>
|
|
|
);
|
|
|
- } else if ('mega_id' in item) {
|
|
|
+ } else if (isType6(item)) {
|
|
|
return (
|
|
|
- <View
|
|
|
- style={{
|
|
|
- display: 'flex',
|
|
|
- alignItems: 'center',
|
|
|
- flexDirection: 'row',
|
|
|
- flex: 1,
|
|
|
- gap: 8
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontSize: getFontSize(18), fontWeight: '700' }}>
|
|
|
- {index + 1}
|
|
|
- </Text>
|
|
|
- <Image
|
|
|
- style={{ width: 36, height: 36, borderRadius: 18 }}
|
|
|
- source={{ uri: API_HOST + '/img/flags_new/' + (item.dare_flag ?? item.flag) }}
|
|
|
- />
|
|
|
- <View
|
|
|
- style={{
|
|
|
- flex: 1,
|
|
|
- display: 'flex',
|
|
|
- flexDirection: 'row',
|
|
|
- justifyContent: 'space-between',
|
|
|
- alignItems: 'center'
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ width: '75%', color: Colors.DARK_BLUE, fontWeight: '700' }}>
|
|
|
- {item.dare_name ?? item.country}
|
|
|
- </Text>
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontWeight: '700' }}>{item.cnt}</Text>
|
|
|
+ <>
|
|
|
+ <View>
|
|
|
+ {item.country !== statistic.ranking[index - 1]?.country ? (
|
|
|
+ <Text
|
|
|
+ style={{
|
|
|
+ color: Colors.DARK_BLUE,
|
|
|
+ fontSize: getFontSize(14),
|
|
|
+ fontWeight: '700',
|
|
|
+ textAlign: 'center',
|
|
|
+ marginTop: 10,
|
|
|
+ marginBottom: 15
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.country}
|
|
|
+ </Text>
|
|
|
+ ) : null}
|
|
|
</View>
|
|
|
- </View>
|
|
|
+ <Item cnt={item.cnt} flag={item.region_flag} index={index} name={item.region} />
|
|
|
+ </>
|
|
|
);
|
|
|
- } else if ('user' in item) {
|
|
|
+ } else if (isType2(item)) {
|
|
|
return (
|
|
|
- <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
|
|
|
- <Text>
|
|
|
- {item.first_name} {item.last_name}
|
|
|
- </Text>
|
|
|
- </View>
|
|
|
+ <>
|
|
|
+ <View>
|
|
|
+ {item.mega_name !== statistic.ranking[index - 1]?.mega_name ? (
|
|
|
+ <Text
|
|
|
+ style={{
|
|
|
+ color: Colors.DARK_BLUE,
|
|
|
+ fontSize: getFontSize(14),
|
|
|
+ fontWeight: '700',
|
|
|
+ textAlign: 'center',
|
|
|
+ marginTop: 10,
|
|
|
+ marginBottom: 15
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.mega_name as string}
|
|
|
+ </Text>
|
|
|
+ ) : null}
|
|
|
+ </View>
|
|
|
+ <Item name={item.dare_name} flag={item.dare_flag} cnt={item.cnt} index={index} />
|
|
|
+ </>
|
|
|
);
|
|
|
- } else {
|
|
|
+ } else if (isType7(item)) {
|
|
|
+ return <Item name={item.country_name} flag={item.flag} cnt={item.cnt} index={index} />;
|
|
|
+ } else if (isType8(item)) {
|
|
|
return (
|
|
|
- <View
|
|
|
- style={{
|
|
|
- display: 'flex',
|
|
|
- alignItems: 'center',
|
|
|
- flexDirection: 'row',
|
|
|
- flex: 1,
|
|
|
- gap: 8
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontSize: getFontSize(18), fontWeight: '700' }}>
|
|
|
- {index + 1}
|
|
|
- </Text>
|
|
|
- <Image
|
|
|
- style={{ width: 36, height: 36, borderRadius: 18 }}
|
|
|
- source={{ uri: API_HOST + '/img/flags_new/' + item.flag }}
|
|
|
- />
|
|
|
- <View
|
|
|
- style={{
|
|
|
- flex: 1,
|
|
|
- display: 'flex',
|
|
|
- flexDirection: 'row',
|
|
|
- justifyContent: 'space-between',
|
|
|
- alignItems: 'center'
|
|
|
- }}
|
|
|
- >
|
|
|
- <Text style={{ width: '75%', color: Colors.DARK_BLUE, fontWeight: '700' }}>
|
|
|
- {item.country_name ?? item.nation ?? item.item_name}
|
|
|
- </Text>
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontWeight: '700' }}>
|
|
|
- {item.cnt ?? item.score}
|
|
|
- </Text>
|
|
|
+ <>
|
|
|
+ <View>
|
|
|
+ {item.mega_name !== statistic.ranking[index - 1]?.mega_name ? (
|
|
|
+ <Text
|
|
|
+ style={{
|
|
|
+ color: Colors.DARK_BLUE,
|
|
|
+ fontSize: getFontSize(14),
|
|
|
+ fontWeight: '700',
|
|
|
+ textAlign: 'center',
|
|
|
+ marginTop: 10,
|
|
|
+ marginBottom: 15
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.mega_name as string}
|
|
|
+ </Text>
|
|
|
+ ) : null}
|
|
|
+ <Item name={item.country} flag={item.flag} cnt={item.cnt} index={index} />
|
|
|
</View>
|
|
|
- </View>
|
|
|
+ </>
|
|
|
+ );
|
|
|
+ } else if (isType9(item)) {
|
|
|
+ return <Item name={item.nation} flag={item.flag} cnt={item.score} index={index} />;
|
|
|
+ } else
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <Item name={item.nation} flag={item.flag} cnt={item.score} index={index} />
|
|
|
+ </>
|
|
|
);
|
|
|
- }
|
|
|
};
|
|
|
|
|
|
return (
|
|
|
<PageWrapper>
|
|
|
<Header label={title} />
|
|
|
+ {statistic.comment && (
|
|
|
+ <Text style={{ color: Colors.DARK_BLUE, textAlign: 'center', marginBottom: 15 }}>
|
|
|
+ {statistic.comment}
|
|
|
+ </Text>
|
|
|
+ )}
|
|
|
<FlatList
|
|
|
contentContainerStyle={{ gap: 10 }}
|
|
|
horizontal={false}
|