import React, { useEffect, useState } from 'react'; import { FlatList, Text, TouchableOpacity, View } from 'react-native'; import { Image } from 'expo-image'; import { Header, HorizontalTabView, Loading, PageWrapper } from '../../../../components'; import { getMastersByCountryOfOrigin, getMastersByType, getMastersByYearOfCompletion, getUNMastersTypes } from '../../../../database/unMastersService'; import { API_HOST } from '../../../../constants'; import { UNMastersListStyles } from './styles'; import ArrowUpWideIcon from '../../../../../assets/icons/arrow-up-wide-short.svg'; import type { Master } from '../../../../database/unMastersService'; import { Colors } from '../../../../theme'; import { getFontSize } from '../../../../utils'; const UNMastersScreen = () => { const [index, setIndex] = useState(0); const [routes, setRoutes] = useState<{ key: string; title: string }[]>([]); const [loading, setLoading] = useState(true); useEffect(() => { const types = getUNMastersTypes(); const parseRoutes = types?.map((item) => ({ key: item.type.toString(), title: item.name })); setRoutes([ { key: '-1', title: 'Sorted by country of origin' }, { key: '-2', title: 'Sorted by year of completion' }, ...(parseRoutes || []) ]); setLoading(false); }, []); if (loading) return ; return (
( )} /> ); }; const UNMastersList = React.memo(({ type }: { type: string }) => { const [isLoading, setIsLoading] = useState(true); const [masters, setMasters] = useState< | Master[] | { country: string; count: number; masters: Master[] }[] | { year: string; count: number; masters: Master[] }[] | null >([]); useEffect(() => { const fetchType = async () => { if (type === '-2') { const data = getMastersByYearOfCompletion(); setMasters(data); } else if (type === '-1') { const data = getMastersByCountryOfOrigin(); setMasters(data); } else { const data = getMastersByType(Number(type) || 1); setMasters(data); } }; fetchType(); setIsLoading(false); }, [type]); if (isLoading) return ; const renderItem = ({ item }: { item: any }) => { const UserItem = ({ user }: { user: Master }) => { return ( {user.full_name} Born: {user.born} / Age when done: {user.age} / {user.origin2_flag && user.origin2_flag !== user.origin1_flag ? ( ) : null} Year / final country: {user.final_year} ); }; const CountryItem = ({ country, masters, count, flag }: { country: string; masters: Master[]; count: number; flag: string; }) => { if (masters.length === 0) return; return ( {country} ({count}) } /> ); }; const YearItem = ({ year, masters, count }: { year: string; masters: Master[]; count: number; }) => { return ( {year} ({count}) } /> ); }; return type !== '-2' ? ( type !== '-1' ? ( ) : ( ) ) : ( ); }; return ( ); }); export default UNMastersScreen;