-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
106 lines (100 loc) · 2.89 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { useState, useEffect } from "react";
import { StyleSheet, Text, View, FlatList } from "react-native";
import Header from "./src/components/Header";
import Spinner from "./src/components/Spinner";
import AccordionListItem from "./src/components/RocketListItem";
import Error from "./src/components/Error";
import { client } from "./src/graphql/Client";
import { infoMissions } from "./src/graphql/Queries";
const App = () => {
const [rocketsInfo, setRocketsInfo] = useState([]);
const [loading, setLoading] = useState(true);
const [errorMessage, setErrorMessage] = useState(false);
// console.log(rocketsInfo)
useEffect(() => {
requestRockets();
}, []);
const requestRockets = () => {
client
.query({
query: infoMissions,
})
.then((response) => {
let dataLaunches = response.data.launches;
let dataLaunchPads = response.data.launchpads;
let dataLaunchesWithlocation = dataLaunches.map((launch) => {
let mission = {};
let siteLocation = dataLaunchPads.find(
(launchPad) => launchPad.site_id === launch.launch_site.site_id
);
return (mission = { ...launch, location: siteLocation.location });
});
let dataRockets = response.data.rockets;
let allRocketsInfo = dataRockets.map((rocket) => {
let consolidatedRocketInfo = {};
let missions = dataLaunchesWithlocation.filter(
(launch) => launch.rocket.rocket_id === rocket.rocket_id
);
return (consolidatedRocketInfo = { ...rocket, missions });
});
setRocketsInfo(allRocketsInfo);
setLoading(response.loading);
// setErrorMessage(false);
})
.catch((error) => {
console.log(`Error message ${error}`);
setErrorMessage(true);
});
};
const renderItem = ({ item }) => (
<AccordionListItem
title={item.rocket_name}
picture={item.flickr_images}
description={item.description}
engine = {item.engines}
>
{item.missions}
</AccordionListItem>
);
return (
<View style={styles.container}>
<Header />
{errorMessage ? (
<Error />
) : (
<View style={styles.contentContainer}>
{loading ? (
<Spinner />
) : (
<View>
<FlatList
data={rocketsInfo}
renderItem={renderItem}
keyExtractor={(item) => item.rocket_id}
/>
</View>
)}
</View>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#000",
color: "#fff",
},
contentContainer: {
paddingHorizontal: 0,
paddingVertical: 20,
color: "#fff",
textTransform: "uppercase",
},
spinner: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
});
export default App;