From 0dbcc78abaecc6a8597f4b23a4ed8da89d79ea23 Mon Sep 17 00:00:00 2001 From: Thuan Bui Date: Thu, 2 Sep 2021 13:14:15 +0700 Subject: [PATCH] add Weather state. not yet finish --- part2/2.12-2.14/src/App.js | 21 ++++++++++++++++++--- part2/2.12-2.14/src/components/Country.js | 22 ++++++++++++---------- part2/2.12-2.14/src/components/Weather.js | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 part2/2.12-2.14/src/components/Weather.js diff --git a/part2/2.12-2.14/src/App.js b/part2/2.12-2.14/src/App.js index e94df42..27148bf 100644 --- a/part2/2.12-2.14/src/App.js +++ b/part2/2.12-2.14/src/App.js @@ -8,6 +8,7 @@ const App = () => { const [newSearch, setNewSerach] = useState('') const [CountryList, setCountryList] = useState([]) const [ResultList, setResultList] = useState([]) + const [WeatherData, setWeatherdata] = useState([]) useEffect(() => { axios @@ -17,8 +18,22 @@ const App = () => { }) }, []) + useEffect(() => { + console.log(ResultList) + if (ResultList.length > 0 ) { + const capital = ResultList[0].capital + const apistring = `http://api.weatherstack.com/current?access_key=9482b8f61c984f8e1988759e020d133e&query=${capital}` + console.log(capital, apistring) + axios + .get(apistring) + .then(response => { + setWeatherdata(response.data) + }) + console.log(WeatherData) + } + }, [ResultList]) + const clickHandler = (index) => { - console.log(index) const newresult = ResultList newresult[index].Show = !newresult[index].Show setResultList([...newresult]) @@ -31,14 +46,14 @@ const App = () => { useEffect(() => { const sfilter = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase())) const filter = sfilter.map(item => ({...item,Show:false})) - setResultList(filter) + setResultList([...filter]) }, [newSearch, CountryList]) return (

Country

- +
) } diff --git a/part2/2.12-2.14/src/components/Country.js b/part2/2.12-2.14/src/components/Country.js index 2e16af0..71f7e32 100644 --- a/part2/2.12-2.14/src/components/Country.js +++ b/part2/2.12-2.14/src/components/Country.js @@ -1,6 +1,7 @@ import React from 'react'; +import Weather from './Weather'; -const Country = ({data,onClick}) => { +const Country = ({data,onClick,weather}) => { const list = data.length const clickHandler = (index) => { onClick(index) @@ -8,35 +9,36 @@ const Country = ({data,onClick}) => { return (
{list > 10 ? 'Too many matches, specific another filter' : - list > 1 ? data.map((item, index) => clickHandler(index)}/>) : - data.map(item => ) + list > 1 ? data.map((item, index) => clickHandler(index)} weather={weather}/>) : + data.map(item => ) }
) } -const CountryLongList = ({data, onClick}) => { +const CountryLongList = ({data, onClick,weather}) => { const show = data.Show console.log(show) return (
-

{data.name} - {show === true && } -

+
{show === true && } +
); } -const CountryDetail = ({data}) => { +const CountryDetail = ({data,weather}) => { + console.log(weather) return (

{data.name}

Capital: {data.capital} -
+

+

Population: {data.population}

Languagues

@@ -44,7 +46,7 @@ const CountryDetail = ({data}) => { {data.languages.map(item =>
  • {item.name}
  • )} {data.name} - +
    ) } diff --git a/part2/2.12-2.14/src/components/Weather.js b/part2/2.12-2.14/src/components/Weather.js new file mode 100644 index 0000000..f72507d --- /dev/null +++ b/part2/2.12-2.14/src/components/Weather.js @@ -0,0 +1,15 @@ +import React from 'react' + +const Weather = ({weather,capital}) => { + console.log(weather) + + return ( +
    +

    Temperature: {weather.current.temperature} Celcius

    + Weather +

    Wind: {weather.current.wind_speed} mph {weather.current.wind_degree} {weather.current.wind_dir}

    +
    + ) +} + +export default Weather \ No newline at end of file