Upload 2.20

This commit is contained in:
AndrewTrieu
2023-03-24 12:52:05 +02:00
parent 0ec5b0e466
commit 41757b9830
4 changed files with 42 additions and 14 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.vscode/settings.json
part2/countries/.env

View File

@@ -1,23 +1,17 @@
import { useState } from "react";
import CountryData from "./CountryData";
const Countries = ({ showedCountries }) => {
const [country, setCountry] = useState("");
const Countries = ({ showedCountries, setShowedCountries }) => {
if (showedCountries.length === 1) {
return <CountryData country={showedCountries[0]} />;
} else if (showedCountries.length <= 10) {
return (
<div>
{showedCountries.map((country) => (
return showedCountries.map((country) => (
<div key={country.cca3}>
{country.name.common}{" "}
<button onClick={() => setCountry(country)}>show</button>
{country.name.official}
{""}
<button onClick={() => setShowedCountries([country])}>Show</button>
</div>
))}
{country && <CountryData country={country} />}
</div>
);
} else if (showedCountries.length > 10) {
));
} else {
return <div>Too many matches, specify another filter</div>;
}
};

View File

@@ -1,3 +1,5 @@
import Weather from "./Weather";
const CountryData = ({ country }) => {
return (
<div>
@@ -11,6 +13,7 @@ const CountryData = ({ country }) => {
))}
</ul>
<img src={country.flags.png} alt={`${country.name.common} flag`} />
<Weather capital={country.capital} />
</div>
);
};

View File

@@ -0,0 +1,30 @@
import { useState, useEffect } from "react";
import axios from "axios";
const Weather = ({ capital }) => {
const OPENWEATHER_API_KEY = process.env.REACT_APP_OPENWEATHER_API_KEY;
const [weather, setWeather] = useState([]);
useEffect(() => {
axios
.get(
`https://api.openweathermap.org/data/2.5/weather?q=${capital}&units=metric&appid=${OPENWEATHER_API_KEY}`
)
.then((response) => {
setWeather(response.data);
});
}, [OPENWEATHER_API_KEY, capital]);
return weather.weather ? (
<div>
<h2>Weather in {capital}</h2>
<div>Temperature: {weather.main.temp}°C</div>
<img
alt="weather icon"
src={`http://openweathermap.org/img/wn/${weather.weather[0].icon}@2x.png`}
/>
<div>Wind: {weather.wind.speed}m/s</div>
</div>
) : null;
};
export default Weather;