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 (

Star Wars Characters

-

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) => + ) + } {/* ❗ Map over the data in state, rendering a Character at each iteration */}
) diff --git a/frontend/components/Character.js b/frontend/components/Character.js index bba3747d8..24176dd59 100644 --- a/frontend/components/Character.js +++ b/frontend/components/Character.js @@ -1,11 +1,40 @@ import React from 'react' +import { useState } from 'react' + +function Character(props) { + const {character} = props; + const [planetToggle, setPlanetToggle] = useState(false); + + const toggle= () => { + setPlanetToggle(!planetToggle); + } -function Character() { // ❗ Add the props - // ❗ Create a state to hold whether the homeworld is rendering or not - // ❗ Create a "toggle" click handler to show or remove the homeworld return ( -
- {/* Use the same markup with the same attributes as in the mock */} +
toggle()}> +

{character.name}

+

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? +
+

{character.homeworld.name}

+

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}

+
+ : + <> + }
) }