import React, { useCallback, useEffect, useState } from 'react'; import { View, Text, FlatList, Image, TouchableOpacity } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { Header, PageWrapper, HorizontalTabView, Loading, WarningModal } from 'src/components'; import { useGetSeriesGroups, useGetSeriesWithGroup } from '@api/series'; import { useFocusEffect } from '@react-navigation/native'; import { API_HOST } from 'src/constants'; import { StoreType, storage } from 'src/storage'; import { styles } from './styles'; import { NAVIGATION_PAGES } from 'src/types'; import AddSvg from 'assets/icons/travels-section/add.svg'; interface SeriesGroup { key: string; title: string; } interface SeriesList { id: number; name: string; icon: string; count: number; new: number; score: number; icon_png: string; count2: number; } const SeriesScreen = () => { const token = storage.get('token', StoreType.STRING) as string; const [isLoading, setIsLoading] = useState(true); const [index, setIndex] = useState(0); const [routes, setRoutes] = useState([]); const [warningVisible, setWarningVisible] = useState(false); const { data } = useGetSeriesGroups(true); const navigation = useNavigation(); useFocusEffect( useCallback(() => { const fetchGroups = async () => { let staticGroups = [ { key: '-1', title: 'Top of the tops' }, { key: '-2', title: 'Milestones' }, { key: 'all', title: 'All series' } ]; const routesData = data?.data?.map((item) => ({ key: item.id.toString(), title: item.name })); routesData && staticGroups.push(...routesData); setRoutes(staticGroups); setIsLoading(false); }; if (data && data.data) { fetchGroups(); } }, [data]) ); if (isLoading) return ; return (
{ if (token) { navigation.navigate(NAVIGATION_PAGES.SUGGEST_SERIES as never); } else { setWarningVisible(true); } }} > } /> ( )} /> setWarningVisible(false)} /> ); }; const SeriesList = React.memo(({ groupId, navigation }: { groupId: string; navigation: any }) => { const [seriesData, setSeriesData] = useState([]); const [isLoading, setIsLoading] = useState(true); const token = storage.get('token', StoreType.STRING) as string; const { mutateAsync } = useGetSeriesWithGroup(); useEffect(() => { const fetchSeriesData = async () => { try { await mutateAsync( { group: groupId, token: String(token) }, { onSuccess: (data) => { setSeriesData(data.data); } } ); } catch (error) { console.error('Failed to fetch series data', error); } finally { setIsLoading(false); } }; fetchSeriesData(); }, [groupId]); if (isLoading) return ; const renderItem = ({ item }: { item: SeriesList }) => { return ( navigation.navigate(NAVIGATION_PAGES.SERIES_ITEM, { id: item.id, name: item.name, token }) } > {item.name} {item.new === 1 && NEW} Total objects: {item.count} {token ? item.score : 0} / {item.count} ); }; return ( item.id.toString()} showsVerticalScrollIndicator={false} /> ); }); export default SeriesScreen;