From e7ab98323d76202f5694075dfadb47dbae428bdb Mon Sep 17 00:00:00 2001 From: Savy Date: Fri, 14 Apr 2023 16:50:02 +0530 Subject: [PATCH] Finished Ex 2.20 --- Part_2/countrydata/.gitignore | 1 + Part_2/countrydata/src/App.js | 9 ++-- Part_2/countrydata/src/components/Details.js | 51 +++++++++++++++++--- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/Part_2/countrydata/.gitignore b/Part_2/countrydata/.gitignore index 4d29575..cc964cb 100644 --- a/Part_2/countrydata/.gitignore +++ b/Part_2/countrydata/.gitignore @@ -12,6 +12,7 @@ /build # misc +.env .DS_Store .env.local .env.development.local diff --git a/Part_2/countrydata/src/App.js b/Part_2/countrydata/src/App.js index 4a5eb6a..ba4cf7c 100644 --- a/Part_2/countrydata/src/App.js +++ b/Part_2/countrydata/src/App.js @@ -22,14 +22,12 @@ const App = () => { const handleSearch = (event) => { setSearch(event.target.value) + setDisplay(false) } const handleClick = (country) => { - console.log('Clicked') - console.log(country) setDisplayCountry(country) - setDisplay(current => !current) - console.log(displayCountry) + setDisplay(true) } return ( @@ -41,7 +39,7 @@ const App = () => {
{searchResults.length > 10 &&

Too Many Results to Show, Please specify query

} - {searchResults.length <10 && searchResults.length > 1 && searchResults.map(country => ( + {searchResults.length < 10 && searchResults.length > 1 && searchResults.map(country => (
  • {country.name} @@ -52,7 +50,6 @@ const App = () => { ))} {searchResults.length === 1 && (
    - {console.log('Entering Details')}
    )} diff --git a/Part_2/countrydata/src/components/Details.js b/Part_2/countrydata/src/components/Details.js index 6990aa2..d8e14dc 100644 --- a/Part_2/countrydata/src/components/Details.js +++ b/Part_2/countrydata/src/components/Details.js @@ -1,3 +1,37 @@ +import { useEffect, useState } from "react" +import axios from "axios" + +const WeatherData = ({ country }) => { + const baseURL = 'http://api.openweathermap.org/data/2.5/weather' + const api_key = process.env.REACT_APP_API_KEY + const [ weather, setWeather ] = useState([]) + const city = country.capital[0].replace(/ /g,"+"); + + useEffect(() => { + console.log('Getting Data') + axios + .get(`${baseURL}?q=${city}&units=metric&appid=${api_key}`) + .then(response => { + setWeather(Object.assign(weather, response.data)) + console.log("Response recieved", weather) + }) + }, [city]) + + if (weather.name === undefined) { + return

    Loading...

    + } + + return ( +
    +

    Temperature: {weather.main.temp}oC

    +

    Weather: {weather.weather[0].main}

    +

    Wind: {weather.wind.speed}m/s

    + {weather.weather[0].main} + +
    + ) +} + const Details = ({ country }) => { if (country.length === 0) { return null @@ -5,19 +39,22 @@ const Details = ({ country }) => { return (
    -

    {country.name} {country.emoji}

    -

    Capital: {country.capital}

    -

    Area: {country.area} km²

    -

    Population: {country.population}

    -

    Region: {country.region}

    -

    Sub-Region: {country.subregion}

    -

    Languages:

    +

    {country.name} {country.emoji}

    +

    Capital: {country.capital}

    +

    Area: {country.area} km²

    +

    Population: {country.population}

    +

    Region: {country.region}

    +

    Sub-Region: {country.subregion}

    +

    Languages:

      {Object.values(country.languages).map(item =>
    • {item}
    • )}
    {country.name} + +

    Weather in {country.capital}

    +
    ) } -- 2.51.2