Upload 2.20
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
.vscode/settings.json
|
||||
part2/countries/.env
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
};
|
||||
|
||||
30
part2/countries/src/components/Weather.js
Normal file
30
part2/countries/src/components/Weather.js
Normal 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;
|
||||
Reference in New Issue
Block a user