-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
247 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import React, { useEffect, useState, memo } from 'react'; | ||
import { StyleSheet, Alert, Image, TouchableOpacity, Dimensions, FlatList } from 'react-native'; | ||
import { Text, View } from '../components/Themed'; | ||
import Loading from 'components/Loading'; | ||
import axios from 'axios'; | ||
import { getPokemonByRegion } from 'utils/getPokemonByRegion'; | ||
import { capitalizeFirstLetter } from 'utils/commonUtils'; | ||
import { useLocalSearchParams, useRouter, Stack } from 'expo-router'; | ||
var { width } = Dimensions.get('window'); | ||
|
||
function PokemonListScreen() { | ||
const [pokemonList, setPokemonList] = useState([]); | ||
const [nextApi, setNextApi] = useState(''); | ||
const params = useLocalSearchParams(); | ||
const router = useRouter(); | ||
const { name } = params; | ||
const [loading, setLoading] = useState(true); | ||
const pokemonWidth = width >= 600 ? 300 : width * 0.3; | ||
const wrapperWidth = width >= 600 ? 200 : width * 0.3; | ||
|
||
useEffect(() => { | ||
const fetchPokemon = async () => { | ||
try { | ||
const data = await getPokemonByRegion(name); | ||
setPokemonList(data?.results); | ||
setNextApi(data?.next); | ||
setLoading(false) | ||
} catch (error) { | ||
console.error('Error fetching Pokémon:', error); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchPokemon(); | ||
}, []); | ||
|
||
function handlePokemonDetails(link: any, id: any) { | ||
// @ts-ignore | ||
// router.push('/pokemonDetailsScreen', { | ||
// endPoint: link, | ||
// imgId: id, | ||
// }); | ||
} | ||
|
||
function handleNextPokemon() { | ||
axios({ | ||
url: nextApi, | ||
method: 'GET', | ||
}).then((response) => { | ||
if (response.status === 200) { | ||
if (response.data.next != null) { | ||
const nextResult = [...pokemonList, ...response.data.results] as any; | ||
setPokemonList(nextResult); | ||
setNextApi(response.data.next); | ||
} else { | ||
Alert.alert('No more Pokemon available at the moment'); | ||
} | ||
} | ||
}).catch((error) => { | ||
Alert.alert('Something went wrong', error); | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<Stack.Screen | ||
options={{ | ||
// @ts-ignore | ||
headerTitle: capitalizeFirstLetter(params.name), | ||
}} | ||
/> | ||
<View style={styles.container}> | ||
{pokemonList.length === 0 || loading ? <Loading /> : | ||
<FlatList | ||
data={pokemonList} | ||
numColumns={3} | ||
showsVerticalScrollIndicator={false} | ||
keyExtractor={(_, index) => index.toString()} | ||
onEndReached={handleNextPokemon} | ||
onEndReachedThreshold={0.3} | ||
contentContainerStyle={styles.innerContainer} | ||
maxToRenderPerBatch={10} | ||
removeClippedSubviews={true} | ||
renderItem={({ item }) => { | ||
const urlString = String(item.url); | ||
const pokemonId = urlString.slice(-7).replace(/\D|\//g, ""); | ||
const pokemonImage = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/' + pokemonId + '.png'; | ||
return ( | ||
<TouchableOpacity | ||
onPress={() => handlePokemonDetails(item.url, pokemonId)} | ||
style={{...styles.pokemonWrapper, width: wrapperWidth}} | ||
key={item.name} | ||
> | ||
<Image | ||
style={{...styles.pokemonImg, width: pokemonWidth}} | ||
source={{ uri: pokemonImage }} | ||
/> | ||
<Text style={styles.titleCase} ellipsizeMode='tail' numberOfLines={1}>{item.name}</Text> | ||
</TouchableOpacity> | ||
) | ||
}} | ||
/> | ||
} | ||
</View> | ||
</> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
innerContainer: { | ||
width: '100%', | ||
maxWidth: 900, // Maximum width for desktop | ||
}, | ||
titleCase: { | ||
textTransform: 'capitalize' | ||
}, | ||
pokemonWrapper: { | ||
margin: 3, | ||
borderRadius: 8, | ||
overflow: 'hidden', | ||
alignItems: 'center', | ||
}, | ||
pokemonImg: { | ||
height: 110, | ||
resizeMode: 'contain', | ||
}, | ||
}); | ||
|
||
export default memo(PokemonListScreen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { ActivityIndicator, StyleSheet } from 'react-native'; | ||
import { View } from '../components/Themed'; | ||
|
||
const Loading = () => ( | ||
<View style={styles.loadingContainer}> | ||
<ActivityIndicator /> | ||
</View> | ||
); | ||
|
||
const styles = StyleSheet.create({ | ||
loadingContainer: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
}) | ||
|
||
export default Loading |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const capitalizeFirstLetter = (str: string) => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// api.js | ||
import axios from 'axios'; | ||
|
||
const POKEMON_API_BASE_URL = 'https://pokeapi.co/api/v2'; | ||
|
||
// Function to get Pokémon by custom offset for each region | ||
export const getPokemonByRegion = async (region) => { | ||
try { | ||
let offset = 0; | ||
|
||
switch (region.toLowerCase()) { | ||
case 'kanto': | ||
offset = 0; | ||
break; | ||
case 'johto': | ||
offset = 151; | ||
break; | ||
case 'hoenn': | ||
offset = 251; | ||
break; | ||
case 'sinnoh': | ||
offset = 386; | ||
break; | ||
case 'unova': | ||
offset = 493; | ||
break; | ||
case 'kalos': | ||
offset = 649; | ||
break; | ||
case 'alola': | ||
offset = 721; | ||
break; | ||
case 'galar': | ||
offset = 809; | ||
break; | ||
case 'paldea': | ||
offset = 905; | ||
break; | ||
// Add more cases for additional regions if needed | ||
default: | ||
return []; | ||
} | ||
|
||
const response = await axios.get(`${POKEMON_API_BASE_URL}/pokemon`, { | ||
params: { | ||
offset, | ||
limit: 20, // Adjust the limit as needed | ||
}, | ||
}); | ||
const pokemonData = response.data; | ||
|
||
// Additional logic if you need to fetch more details for each Pokémon | ||
// e.g., fetch details for each Pokémon using their URLs | ||
|
||
return pokemonData; | ||
} catch (error) { | ||
console.error('Error fetching Pokémon:', error); | ||
throw error; | ||
} | ||
}; |