<commit_before>import React, { Component } from 'react';
// let grossFactorHomeworld;
// let grossFactorSpecies;
// let grossFactorPerson;

class App extends Component {
  constructor () {
    super();
    this.state = {
      peopleArray: [],
      planetArray: [],
    }
  }

  getPeopleData(url){
    fetch(url)
    .then(raw => raw.json())
    .then(parsedData => {
      const unresolvedPromises = parsedData.results.map( (person, index, array) => {
        // declare the object to be manipulated - this is the result after promise resolution
        let tempObject = {name: person.name};
        // Put the promise at the end of the chain into the unresolvedPromises array (line 36)
        return fetch(person.homeworld) //get the data from the current person Homeworld URL
          .then(homeworldRawData => homeworldRawData.json()) //translate raw data
          .then(homeworldData => { //add data retrieved into the object
            Object.assign(tempObject, {
              homeworld: homeworldData.name,
              homeworldPop: homeworldData.population
              //return nothing, next .then is a new thing
            })
          })
          .then(totallynewstuff => { //start a totally new process in the same chain
            //Species can be an array of URLs, let's create a promise array of all URLs in the array
            const unresolvedSpeciesPromises = person.species.map((eachSpecies) => {
              // put this promise in the array
              return fetch(eachSpecies) //grab data
              .then(speciesRawData => speciesRawData.json()) //parse data promise - this is put in the array
            })
            // let's resolve the array of species promises - this will give an array of objects
            return Promise.all(unresolvedSpeciesPromises)
            //create a promise - This promise is not resolved and is placed in the unresolvedPromises array
            .then(resolvedSpecies => Object.assign(tempObject, {species: resolvedSpecies}))
          })
      })
      Promise.all(unresolvedPromises)
        .then(promiseAllResults => {
          console.log(promiseAllResults);
          this.setState({
            peopleArray: promiseAllResults
          })
        })
    })
  }

  getPlanetData(url){
    fetch(url)
    .then(raw => raw.json())
    .then(parsedData => {
      console.log(parsedData);
      const unresolvedPromises = parsedData.results.map( (planet, index, array) => {
        let tempObject = {
          name: planet.name,
          terrain: planet.terrain,
          population: planet.population,
          climate: planet.climate,
        };
        const unresolvedResidentPromises = planet.residents.map((eachResident) => {
          return fetch(eachResident)
          .then(speciesRawData => speciesRawData.json())
        })
        return Promise.all(unresolvedResidentPromises)
        .then(pendingResidents => Object.assign(tempObject, {residents: pendingResidents}))
          })
      Promise.all(unresolvedPromises)
        .then(promiseAllResults => {
          console.log(promiseAllResults);
          this.setState({
            planetArray: promiseAllResults
          })
        })
    })
  }

  componentDidMount() {
    this.getPeopleData('https://swapi.co/api/people');
    this.getPlanetData('https://swapi.co/api/planets')

  }

  // componentDidMount() {
  //   fetch('https://swapi.co/api/people/')
  //   .then(raw => raw.json())
  //   .then(parsedData => {
  //     console.log(parsedData)<commit_msg>Add third API call<commit_after>import React, { Component } from 'react';
// let grossFactorHomeworld;
// let grossFactorSpecies;
// let grossFactorPerson;

class App extends Component {
  constructor () {
    super();
    this.state = {
      peopleArray: [],
      planetArray: [],
      vehicleArray: []
    }
  }

  getPeopleData(url){
    fetch(url)
    .then(raw => raw.json())
    .then(parsedData => {
      const unresolvedPromises = parsedData.results.map( (person, index, array) => { //line XXXX
        // declare the object to be manipulated - this is the result after promise resolution
        let tempObject = {
          name: person.name,
          url: person.url
        };
        // Put the promise at the end of the chain into the unresolvedPromises array (Promise is on line YYYY)
        return fetch(person.homeworld) //get the data from the current person Homeworld URL
          .then(homeworldRawData => homeworldRawData.json()) //translate raw data
          .then(homeworldData => { //add data retrieved into the object
            Object.assign(tempObject, {
              homeworld: homeworldData.name,
              homeworldPop: homeworldData.population
            }) //return nothing, next .then is a new thing
          })
          .then(totallynewstuff => { //start a totally new process in the same chain
            //Species can be an array of URLs, let's create a promise array of all URLs in the array
            const unresolvedSpeciesPromises = person.species.map((eachSpecies) => {
              // put promise on line ZZZZ in the array for each species
              return fetch(eachSpecies) //grab data
              .then(speciesRawData => speciesRawData.json()) //parse data promise - this is put in the species array // Line ZZZZ
            })
            // let's resolve the array of species promises - this will give an array of objects
            return Promise.all(unresolvedSpeciesPromises) //return this promise chain to line XXXX
            //create a promise - This promise is not resolved and is placed in the unresolvedPromises array
            .then(resolvedSpecies => Object.assign(tempObject, {species: resolvedSpecies})) // line YYYY
          })
      })
      Promise.all(unresolvedPromises) // unresolved promises is currently an array of Object promises
        .then(promiseAllResults => { //resolve promises to get objects
          this.setState({
            peopleArray: promiseAllResults //put objects in state
          })
        })
    })
  }

  getPlanetData(url){
    fetch(url)
    .then(raw => raw.json())
    .then(parsedData => {
      // console.log(parsedData);
      const unresolvedPromises = parsedData.results.map( (planet, index, array) => {
        let tempObject = {
          name: planet.name,
          terrain: planet.terrain,
          population: planet.population,
          climate: planet.climate,
          url: planet.url
        };
        const unresolvedResidentPromises = planet.residents.map((eachResident) => {
          return fetch(eachResident)
          .then(speciesRawData => speciesRawData.json())
        })
        return Promise.all(unresolvedResidentPromises)
        .then(pendingResidents => Object.assign(tempObject, {residents: pendingResidents}))
          })
      Promise.all(unresolvedPromises)
        .then(promiseAllResults => {
          // console.log(promiseAllResults);
          this.setState({
            planetArray: promiseAllResults
          })
        })
    })
  }

  getVehicleData(url) {
    fetch(url)
    .then(rawVehiclesData => rawVehiclesData.json())
    .then(vehiclesData => {
      // console.log(vehiclesData);
      return vehiclesData.results.map( (vehicle) => {
        return Object.assign({}, {
          name: vehicle.name,
          model: vehicle.model,
          class: vehicle.vehicle_class,
          passengers: vehicle.passengers,
          url: vehicle.url
        })

      })
    })
    .then(vehiclesResolvedPromises => {
      this.setState({
        vehicleArray: vehiclesResolvedPromises
      })
    });
  }

  componentDidMount() {
    this.getPeopleData('https://swapi.co/api/people');
    this.getPlanetData('https://swapi.co/api/planets');
    this.getVehicleData('https://swapi.co/api/vehicles')

  }

  // componentDidMount() {
  //   fetch('https://swapi.co/api/people/')
  //   .then(raw => raw.json())
  //   .then(parsedData => {
  //     console.log(parsedData)
{'fraction_non_alphanumeric': 0.1096079168232494, 'fraction_numerical': 0.0002505323813102844, 'mean_word_length': 2.2206534893102057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4860693', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2153, 'n_words': 556}