diff --git a/frontend/components/App.js b/frontend/components/App.js index 3ec074a12..7b0df4d17 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,4 +1,5 @@ import React from 'react' +import { useState, useEffect } from 'react' import axios from 'axios' import Character from './Character' @@ -6,12 +7,45 @@ const urlPlanets = 'http://localhost:9009/api/planets' const urlPeople = 'http://localhost:9009/api/people' function App() { - // ❗ Create state to hold the data from the API - // ❗ Create effects to fetch the data and put it in state + + const [characters, setCharacters] = useState([]); + + useEffect(() => { + fetchData(); + }, []); + + const fetchData = () => { + let requestPlanets = axios.get(urlPlanets); + let requestPeople = axios.get(urlPeople); + let Datas = []; + Promise.all([requestPlanets, requestPeople]) + .then(responses => { + const [responsePlanets, responsePeople] = responses; + responsePeople.data.forEach((data1) => { + responsePlanets.data.forEach((data2) => { + if (data1.homeworld === data2.id) + { + let newData = data1; + newData.homeworld = data2; + Datas.push(newData); + } + }) + }) + setCharacters(Datas); + }) + .catch(error => { + console.log('Error: ', error); + }); + } return (
See the README of the project for instructions on completing this challenge
+ {/*See the README of the project for instructions on completing this challenge
*/} + { + characters.map( + (character, index) =>Birth_year: {character.birth_year}
+Eye_color: {character.eye_color}
+Gender: {character.gender}
+Hair_color: {character.hair_color}
+Height: {character.height}
+Mass: {character.mass}
+Skin_color: {character.skin_color}
+ { + planetToggle? +Climate: {character.homeworld.climate}
+Diameter: {character.homeworld.diameter}
+Gravity: {character.homeworld.gravity}
+Orbital_period: {character.homeworld.orbital_period}
+Population: {character.homeworld.population}
+Rotation_period: {character.homeworld.rotation_period}
+Surface_water: {character.homeworld.surface_water}
+Terrain: {character.homeworld.terrain}
+