|
| 1 | +import { |
| 2 | + View, |
| 3 | + Text, |
| 4 | + FlatList, |
| 5 | + ActivityIndicator, |
| 6 | + Image, |
| 7 | + StyleSheet, |
| 8 | +} from 'react-native'; |
| 9 | +import React from 'react'; |
| 10 | +import usePokemonViewModel from '../viewModels/PokemonViewModel'; |
| 11 | +import {Pokemon} from '../../domain/entities/Pokemon'; |
| 12 | + |
| 13 | +const PokemonScreen: React.FC = () => { |
| 14 | + const { |
| 15 | + pokemons, |
| 16 | + error, |
| 17 | + isFetching, |
| 18 | + isFetchingNextPage, |
| 19 | + fetchNextPage, |
| 20 | + hasNextPage, |
| 21 | + } = usePokemonViewModel(); |
| 22 | + |
| 23 | + if (isFetching && !isFetchingNextPage) { |
| 24 | + return <ActivityIndicator size="large" />; |
| 25 | + } |
| 26 | + |
| 27 | + if (error) { |
| 28 | + return <Text>Bir hata oluştu</Text>; |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <View style={styles.container}> |
| 33 | + <FlatList<Pokemon> |
| 34 | + data={pokemons} |
| 35 | + keyExtractor={item => item.name} |
| 36 | + renderItem={({item}) => ( |
| 37 | + <View style={styles.cardContainer}> |
| 38 | + <Image |
| 39 | + source={{ |
| 40 | + uri: `https://img.pokemondb.net/artwork/${item.name}.jpg`, |
| 41 | + }} |
| 42 | + style={styles.image} |
| 43 | + resizeMode="contain" |
| 44 | + /> |
| 45 | + <View style={styles.textContainer}> |
| 46 | + <Text style={styles.title}>{item.name}</Text> |
| 47 | + </View> |
| 48 | + </View> |
| 49 | + )} |
| 50 | + onEndReached={() => { |
| 51 | + if (hasNextPage) { |
| 52 | + fetchNextPage(); |
| 53 | + } |
| 54 | + }} |
| 55 | + onEndReachedThreshold={0.8} |
| 56 | + ListFooterComponent={ |
| 57 | + isFetchingNextPage ? <ActivityIndicator size="large" /> : null |
| 58 | + } |
| 59 | + /> |
| 60 | + </View> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +const styles = StyleSheet.create({ |
| 65 | + container: {flex: 1, padding: 20, backgroundColor: '#fff'}, |
| 66 | + cardContainer: { |
| 67 | + padding: 10, |
| 68 | + borderBottomWidth: 1, |
| 69 | + borderBottomColor: '#ccc', |
| 70 | + flexDirection: 'row', |
| 71 | + alignItems: 'center', |
| 72 | + }, |
| 73 | + image: {width: 100, height: 100, marginRight: 10}, |
| 74 | + title: {fontSize: 20, fontWeight: '800', textTransform: 'capitalize'}, |
| 75 | + textContainer: {gap: 5}, |
| 76 | +}); |
| 77 | + |
| 78 | +export default PokemonScreen; |
0 commit comments