index.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { FC, useCallback, useState } from 'react';
  2. import { FlatList, Text } from 'react-native';
  3. import { useFocusEffect } from '@react-navigation/native';
  4. import { RenderItem } from '../Components/StatisticRouter';
  5. import { Header, Loading, PageWrapper } from '../../../../components';
  6. import { getStatistic, StatisticType } from '../../../../database/statisticsService';
  7. import { ItemStyles } from '../Components/styles';
  8. type Props = {
  9. route: { params: { title: string; url1: string; url2: string | null } };
  10. };
  11. //TODO: For future fix types
  12. const StatisticsListScreen: FC<Props> = ({ route }) => {
  13. const title = route.params.title;
  14. const url1 = route.params.url1;
  15. const url2 = route.params.url2;
  16. const [isLoading, setIsLoading] = useState(true);
  17. const [statistic, setStatistic] = useState<StatisticType | null>(null);
  18. useFocusEffect(
  19. useCallback(() => {
  20. function fetchStatistic() {
  21. const data = getStatistic(url1, url2);
  22. if (!data) return;
  23. setStatistic(JSON.parse(data as unknown as string) as unknown as StatisticType);
  24. setIsLoading(false);
  25. }
  26. fetchStatistic();
  27. }, [url1])
  28. );
  29. if (isLoading) return <Loading />;
  30. if (!statistic) return null;
  31. return (
  32. <PageWrapper>
  33. <Header label={title} />
  34. {statistic.comment && <Text style={ItemStyles.comment}>{statistic.comment}</Text>}
  35. <FlatList
  36. contentContainerStyle={{ gap: 10 }}
  37. horizontal={false}
  38. data={statistic.ranking}
  39. renderItem={({ item, index }) => (
  40. <RenderItem item={item} index={index} ranking={statistic?.ranking} />
  41. )}
  42. showsVerticalScrollIndicator={false}
  43. />
  44. </PageWrapper>
  45. );
  46. };
  47. export default StatisticsListScreen;