Initial commit

Generated by create-expo-app 3.2.0.
This commit is contained in:
Dennis Hundertmark
2025-02-25 11:38:57 +01:00
commit 0e7f7e1dfd
31 changed files with 18794 additions and 0 deletions
+89
View File
@@ -0,0 +1,89 @@
import { COLORS } from "@/constants/colors";
import { Person } from "@/types/person";
import { useLocalSearchParams } from "expo-router";
import React, { useEffect, useState } from "react";
import { ScrollView, StyleSheet, Text, View } from "react-native";
const Page = () => {
const { id } = useLocalSearchParams();
const [person, setPerson] = useState<Person | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchPeople = async () => {
try {
setLoading(true);
const response = await fetch(`https://swapi.dev/api/people/${id}`);
const data = await response.json();
setPerson(data);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
fetchPeople();
}, [id]);
if (loading) {
return (
<View>
<Text style={{ color: "#fff" }}>Loading...</Text>
</View>
);
}
if (!person) {
return (
<View style={styles.container}>
<Text style={{ color: "#fff" }}>Film not found</Text>
</View>
);
}
return (
<ScrollView style={styles.container}>
<View style={styles.header}>
<Text style={styles.name}>{person.name}</Text>
</View>
<Text style={styles.detail}>Height: {person.height} cm</Text>
<Text style={styles.detail}>Mass: {person.mass} kg</Text>
<Text style={styles.detail}>Hair Color: {person.hair_color}</Text>
<Text style={styles.detail}>Skin Color: {person.skin_color}</Text>
<Text style={styles.detail}>Eye Color: {person.eye_color}</Text>
<Text style={styles.detail}>Birth Year: {person.birth_year}</Text>
<Text style={styles.detail}>Gender: {person.gender}</Text>
</ScrollView>
);
};
export default Page;
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
backgroundColor: COLORS.background,
},
header: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: 16,
},
name: {
fontSize: 24,
fontWeight: "bold",
color: COLORS.text,
flex: 1,
},
favoriteButton: {
padding: 8,
},
detail: {
fontSize: 16,
color: COLORS.text,
marginBottom: 8,
},
});
+36
View File
@@ -0,0 +1,36 @@
import { StyleSheet, Text, View } from "react-native";
import React from "react";
import { Stack } from "expo-router";
import { COLORS } from "@/constants/colors";
const Layout = () => {
return (
<Stack
screenOptions={{
headerShadowVisible: false,
headerStyle: {
backgroundColor: COLORS.background,
},
headerTintColor: COLORS.text,
headerTitleStyle: {
fontWeight: "bold",
},
contentStyle: {
backgroundColor: COLORS.background,
},
}}
>
<Stack.Screen name="index" options={{ title: "All Characters" }} />
<Stack.Screen
name="[id]"
options={{
title: "Character Details",
}}
/>
</Stack>
);
};
export default Layout;
const styles = StyleSheet.create({});
+158
View File
@@ -0,0 +1,158 @@
import ListEmptyComponent from "@/components/ListEmptyComponent";
import PersonItem from "@/components/PersonItem";
import { COLORS } from "@/constants/colors";
import { Person } from "@/types/person";
import React, { useEffect, useState } from "react";
import {
ActivityIndicator,
Button,
FlatList,
RefreshControl,
StyleSheet,
TextInput,
View,
} from "react-native";
interface ApiResponse {
results: Person[];
next: string | null;
result?: Person[];
}
const Page = () => {
const [people, setPeople] = useState<Person[]>([]);
const [refreshing, setRefreshing] = useState(false);
const [loading, setLoading] = useState(true);
const [searchQuery, setSearchQuery] = useState("");
const [nextPage, setNextPage] = useState<string | null>(null);
const [loadingMore, setLoadingMore] = useState(false);
const fetchPeople = async (query: string = "", url: string | null = null) => {
setLoading(true);
try {
let apiUrl = url || `https://swapi.dev/api/people`;
if (query) {
apiUrl = `https://swapi.tech/api/people/?name=${query}`;
}
const response = await fetch(apiUrl);
const data: ApiResponse = await response.json();
if (query && data.result) {
const transformedResults = data.result.map((item: any) => ({
name: item.properties.name,
birth_year: item.properties.birth_year,
gender: item.properties.gender,
url: item.properties.url,
}));
setPeople(transformedResults);
} else {
setPeople((prevPeople) =>
url ? [...prevPeople, ...data.results] : data.results,
);
}
setNextPage(data.next);
} catch (error) {
console.error("Error fetching people:", error);
} finally {
setRefreshing(false);
setLoading(false);
setLoadingMore(false);
}
};
useEffect(() => {
fetchPeople();
}, []);
const onRefresh = () => {
setRefreshing(true);
fetchPeople(searchQuery);
};
const handleSearch = () => {
fetchPeople(searchQuery);
};
const handleLoadMore = () => {
if (nextPage && !loadingMore) {
setLoadingMore(true);
fetchPeople(searchQuery, nextPage);
}
};
const renderFooter = () => {
if (!loadingMore) return null;
return (
<View style={styles.footerLoader}>
<ActivityIndicator size="small" color="#FFE81F" />
</View>
);
};
return (
<View style={styles.container}>
<View style={styles.searchContainer}>
<TextInput
style={styles.searchBar}
placeholder="Search characters..."
placeholderTextColor={COLORS.inactive}
value={searchQuery}
onChangeText={setSearchQuery}
/>
<Button title="Search" onPress={handleSearch} color={COLORS.text} />
</View>
<FlatList
data={people}
renderItem={({ item }) => <PersonItem item={item} />}
keyExtractor={(item) => item.url}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
tintColor={COLORS.text}
/>
}
ListEmptyComponent={
<ListEmptyComponent
loading={loading}
message={
searchQuery
? "No matching characters found"
: "No characters found"
}
/>
}
onEndReached={handleLoadMore}
onEndReachedThreshold={0.1}
ListFooterComponent={renderFooter}
/>
</View>
);
};
export default Page;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: COLORS.containerBackground,
},
searchContainer: {
flexDirection: "row",
alignItems: "center",
padding: 10,
},
searchBar: {
flex: 1,
backgroundColor: COLORS.itemBackground,
color: COLORS.text,
padding: 10,
marginRight: 10,
borderRadius: 5,
fontSize: 16,
},
footerLoader: {
alignItems: "center",
paddingVertical: 20,
},
});