Skip to content

Commit 477f870

Browse files
committed
add plantet screen
1 parent 070404a commit 477f870

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
3+
export const fetchPlanets = async () => {
4+
const response = await fetch("https://swapi.py4e.com/api/planets/");
5+
const data = await response.json();
6+
return data;
7+
};
8+
9+
export const usePlanets = () => {
10+
return useQuery({
11+
queryKey: ["planets"],
12+
queryFn: fetchPlanets,
13+
});
14+
};

hackathon/spacecraft/src/navigation/BottomTabNavigator.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* eslint-disable react/no-unstable-nested-components */
22
import React from "react";
33
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
4-
import { FontAwesome5 } from "@expo/vector-icons";
4+
import { FontAwesome5, Ionicons } from "@expo/vector-icons";
55
import { useTheme } from "react-native-paper";
66
import { getFocusedRouteNameFromRoute } from "@react-navigation/native";
77

8+
import { ExploreNavigator } from "~/navigation/ExploreNavigator";
89
import { Routes } from "~/navigation/Routes";
910
import { PilotNavigator } from "~/navigation/PilotNavigator";
1011
import { StarshipNavigator } from "~/navigation/StarshipNavigator";
@@ -44,6 +45,16 @@ export const BottomTabNavigator = () => {
4445
),
4546
}}
4647
/>
48+
<Tab.Screen
49+
name={Routes.EXPLORE_SCREEN}
50+
component={ExploreNavigator}
51+
options={{
52+
tabBarLabel: "Explore",
53+
tabBarIcon: ({ color }) => (
54+
<Ionicons name="planet" size={22} color={color} />
55+
),
56+
}}
57+
/>
4758
<Tab.Screen
4859
name={Routes.PLUS_STACK}
4960
component={PlusNavigator}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createNativeStackNavigator } from "@react-navigation/native-stack";
2+
3+
import { Routes } from "./Routes";
4+
5+
import { ExploreScreen } from "~/screens/ExploreScreen";
6+
import { PlanetDetailsScreen } from "~/screens/PlanetDetailsScreen";
7+
8+
const Stack = createNativeStackNavigator();
9+
10+
export const ExploreNavigator = () => {
11+
return (
12+
<Stack.Navigator
13+
screenOptions={{
14+
headerShown: false,
15+
}}
16+
>
17+
<Stack.Screen name={Routes.EXPLORE_SCREEN} component={ExploreScreen} />
18+
<Stack.Screen
19+
name={Routes.PLANET_DETAILS_SCREEN}
20+
component={PlanetDetailsScreen}
21+
/>
22+
</Stack.Navigator>
23+
);
24+
};

hackathon/spacecraft/src/navigation/Routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const Routes = {
77
PILOT_SCREEN: "Pilots",
88
PILOT_STACK: "Pilots Stack",
99
PILOT_DETAILS_SCREEN: "Pilot",
10+
EXPLORE_SCREEN: "Explore",
11+
PLANET_DETAILS_SCREEN: "Planet",
1012
PLUS_STACK: "Plus Stack",
1113
PLUS_SCREEN: "Plus",
1214
DO_YOU_LIKE_SCREEN: "Do You Like",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Text, TouchableOpacity, View } from "react-native";
2+
3+
import { ScreenContainer } from "~/components/ScreenContainer";
4+
import { usePlanets } from "~/hooks/usePlanets";
5+
import { Routes } from "~/navigation/Routes";
6+
7+
interface ExploreScreenProps {}
8+
9+
export function ExploreScreen({ navigation }: ExploreScreenProps) {
10+
const { data, isLoading, isError, error } = usePlanets();
11+
12+
if (isLoading) {
13+
return (
14+
<ScreenContainer title={"Explore"}>
15+
<Text>Loading...</Text>
16+
</ScreenContainer>
17+
);
18+
}
19+
20+
if (isError) {
21+
return (
22+
<ScreenContainer title={"Explore"}>
23+
<Text>{error?.message}</Text>
24+
</ScreenContainer>
25+
);
26+
}
27+
28+
return (
29+
<ScreenContainer title={"Explore"}>
30+
<View>
31+
{data?.results.map((planet) => (
32+
<TouchableOpacity
33+
key={planet.name}
34+
onPress={() =>
35+
navigation.navigate(Routes.PLANET_DETAILS_SCREEN, { planet })
36+
}
37+
>
38+
<Text>{planet.name}</Text>
39+
</TouchableOpacity>
40+
))}
41+
</View>
42+
</ScreenContainer>
43+
);
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Text, View } from "react-native";
2+
3+
import { ScreenContainer } from "~/components/ScreenContainer";
4+
5+
/**
6+
* @deprecated move this to typ please
7+
*/
8+
interface Planet {
9+
name: string;
10+
rotation_period: string;
11+
orbital_period: string;
12+
diameter: string;
13+
climate: string;
14+
gravity: string;
15+
terrain: string;
16+
surface_water: string;
17+
population: string;
18+
residents: string[];
19+
films: string[];
20+
created: string;
21+
edited: string;
22+
url: string;
23+
}
24+
25+
interface Params {
26+
planet: Planet;
27+
}
28+
29+
interface Route {
30+
key: string;
31+
name: string;
32+
params: Params;
33+
}
34+
35+
export interface PlanetDetailsScreenProps {
36+
route: Route;
37+
}
38+
39+
export function PlanetDetailsScreen(props: PlanetDetailsScreenProps) {
40+
console.log(
41+
"🚀 ~ file: PlanetDetailsScreen.tsx:6 ~ PlanetDetailsScreen ~ props:",
42+
props.route
43+
);
44+
45+
46+
return (
47+
<ScreenContainer title={props.route.params.planet.name} withGoBack>
48+
<Text>PlanetDetailsScreen</Text>
49+
50+
<Text>{JSON.stringify(props.route.params.planet.name)}</Text>
51+
</ScreenContainer>
52+
);
53+
}

0 commit comments

Comments
 (0)