index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useEffect, useState } from 'react';
  2. import { FlatList, Text, TouchableOpacity, View } from 'react-native';
  3. import { Image } from 'expo-image';
  4. import { Header, HorizontalTabView, Loading, PageWrapper } from '../../../../components';
  5. import { getMastersByType, getUNMastersTypes } from '../../../../database/unMastersService';
  6. import { API_HOST } from '../../../../constants';
  7. import { UNMastersListStyles } from './styles';
  8. import ArrowUpWideIcon from '../../../../../assets/icons/arrow-up-wide-short.svg';
  9. import type { Master } from '../../../../database/unMastersService';
  10. const UNMastersScreen = () => {
  11. const [index, setIndex] = useState(0);
  12. const [routes, setRoutes] = useState<{ key: string; title: string }[]>([]);
  13. const [loading, setLoading] = useState(true);
  14. useEffect(() => {
  15. const types = getUNMastersTypes();
  16. const parseRoutes = types?.map((item) => ({
  17. key: item.type.toString(),
  18. title: item.name
  19. }));
  20. setRoutes(parseRoutes || []);
  21. setLoading(false);
  22. }, []);
  23. if (loading) return <Loading />;
  24. return (
  25. <PageWrapper>
  26. <Header label={'UN Masters'} />
  27. <HorizontalTabView
  28. index={index}
  29. setIndex={setIndex}
  30. routes={routes}
  31. renderScene={({ route }: { route: { key: string; title: string } }) => (
  32. <UNMastersList type={route.key} />
  33. )}
  34. />
  35. </PageWrapper>
  36. );
  37. };
  38. const UNMastersList = React.memo(({ type }: { type: string }) => {
  39. const [isLoading, setIsLoading] = useState(true);
  40. const [masters, setMasters] = useState<Master[] | null>([]);
  41. useEffect(() => {
  42. const fetchType = async () => {
  43. const data = getMastersByType(Number(type) || 1);
  44. setMasters(data);
  45. };
  46. fetchType();
  47. setIsLoading(false);
  48. }, [type]);
  49. if (isLoading) return <Loading />;
  50. const renderItem = ({ item }: { item: Master }) => {
  51. return (
  52. <View style={UNMastersListStyles.wrapper}>
  53. <View style={{ gap: 3, marginLeft: 5 }}>
  54. <Text style={UNMastersListStyles.firstAndLastName}>{item.full_name}</Text>
  55. <View style={UNMastersListStyles.wrapper}>
  56. <Text style={UNMastersListStyles.descriptionText}>
  57. Born: {item.born} / Age when done: {item.age} /
  58. </Text>
  59. <Image
  60. style={[UNMastersListStyles.countryFlag, { marginLeft: 5 }]}
  61. source={{ uri: API_HOST + '/img/flags_new/' + item.origin1_flag }}
  62. />
  63. {item.origin2_flag && item.origin2_flag !== item.origin1_flag ? (
  64. <Image
  65. style={[UNMastersListStyles.countryFlag, { marginLeft: -5 }]}
  66. source={{ uri: API_HOST + '/img/flags_new/' + item.origin2_flag }}
  67. />
  68. ) : null}
  69. </View>
  70. <View style={UNMastersListStyles.wrapper}>
  71. <Text style={UNMastersListStyles.descriptionText}>
  72. Year / final country: {item.final_year}
  73. </Text>
  74. <Image
  75. style={[UNMastersListStyles.countryFlag, { marginLeft: 5 }]}
  76. source={{ uri: API_HOST + '/img/flags_new/' + item.final_flag }}
  77. />
  78. </View>
  79. </View>
  80. </View>
  81. );
  82. };
  83. return (
  84. <FlatList
  85. contentContainerStyle={{ gap: 10 }}
  86. horizontal={false}
  87. data={masters}
  88. renderItem={renderItem}
  89. keyExtractor={(item) => item.id.toString()}
  90. showsVerticalScrollIndicator={false}
  91. />
  92. );
  93. });
  94. export default UNMastersScreen;