Skip to content

Commit b95ba66

Browse files
authored
Merge pull request #217 from flexbox/feature/add-planets-screen
add plantet screen
2 parents 0d5fbf0 + 1ac49b4 commit b95ba66

File tree

12 files changed

+205
-8
lines changed

12 files changed

+205
-8
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
2-
"settings": {
3-
"editor.formatOnSave": true,
4-
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
5-
"editor.codeActionsOnSave": {
6-
"source.fixAll.eslint": true
7-
}
8-
}
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll.eslint": "always"
6+
},
97
}
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { FlatList, TouchableOpacity, View } from "react-native";
2+
import { Text } from "react-native-paper";
3+
4+
import { ScreenContainer } from "~/components/ScreenContainer";
5+
import { usePlanets } from "~/hooks/usePlanets";
6+
import { Routes } from "~/navigation/Routes";
7+
import { getTerrainColor } from "~/utils/getTerrainColor";
8+
9+
interface ExploreScreenProps {}
10+
11+
export function ExploreScreen({ navigation }: ExploreScreenProps) {
12+
const { data, isLoading, isError, error } = usePlanets();
13+
14+
if (isLoading) {
15+
return (
16+
<ScreenContainer title={"Explore"}>
17+
<Text>Loading...</Text>
18+
</ScreenContainer>
19+
);
20+
}
21+
22+
if (isError) {
23+
return (
24+
<ScreenContainer title={"Explore"}>
25+
<Text>{error?.message}</Text>
26+
</ScreenContainer>
27+
);
28+
}
29+
30+
const renderItem = ({ item }) => {
31+
if (!item || item.name === "Bespin") {
32+
return null;
33+
}
34+
35+
const backgroundColor = getTerrainColor(item.terrain);
36+
const size = item.diameter / 100;
37+
38+
return (
39+
<View style={{ padding: 64 }}>
40+
<TouchableOpacity
41+
onPress={() =>
42+
navigation.navigate(Routes.PLANET_DETAILS_SCREEN, { planet: item })
43+
}
44+
>
45+
<Text variant="headlineMedium" style={{ textAlign: "center" }}>
46+
{item.name}
47+
</Text>
48+
</TouchableOpacity>
49+
<View
50+
style={{
51+
width: size,
52+
height: size,
53+
backgroundColor: backgroundColor,
54+
borderRadius: "100%",
55+
margin: 8,
56+
}}
57+
/>
58+
</View>
59+
);
60+
};
61+
62+
return (
63+
<ScreenContainer title={"Explore"}>
64+
<FlatList
65+
data={data.results}
66+
renderItem={renderItem}
67+
keyExtractor={(ship) => ship.model}
68+
horizontal={true}
69+
/>
70+
</ScreenContainer>
71+
);
72+
}

0 commit comments

Comments
 (0)