add Weather state. not yet finish

This commit is contained in:
2021-09-02 13:14:15 +07:00
parent d5317482b9
commit 0dbcc78aba
3 changed files with 45 additions and 13 deletions
+18 -3
View File
@@ -8,6 +8,7 @@ const App = () => {
const [newSearch, setNewSerach] = useState('') const [newSearch, setNewSerach] = useState('')
const [CountryList, setCountryList] = useState([]) const [CountryList, setCountryList] = useState([])
const [ResultList, setResultList] = useState([]) const [ResultList, setResultList] = useState([])
const [WeatherData, setWeatherdata] = useState([])
useEffect(() => { useEffect(() => {
axios 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) => { const clickHandler = (index) => {
console.log(index)
const newresult = ResultList const newresult = ResultList
newresult[index].Show = !newresult[index].Show newresult[index].Show = !newresult[index].Show
setResultList([...newresult]) setResultList([...newresult])
@@ -31,14 +46,14 @@ const App = () => {
useEffect(() => { useEffect(() => {
const sfilter = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase())) const sfilter = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase()))
const filter = sfilter.map(item => ({...item,Show:false})) const filter = sfilter.map(item => ({...item,Show:false}))
setResultList(filter) setResultList([...filter])
}, [newSearch, CountryList]) }, [newSearch, CountryList])
return ( return (
<div> <div>
<h2>Country</h2> <h2>Country</h2>
<Filter value={newSearch} onChange={handleSearchChange} /> <Filter value={newSearch} onChange={handleSearchChange} />
<Country data={ResultList} onClick={clickHandler}/> <Country data={ResultList} onClick={clickHandler} weather={WeatherData}/>
</div> </div>
) )
} }
+12 -10
View File
@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import Weather from './Weather';
const Country = ({data,onClick}) => { const Country = ({data,onClick,weather}) => {
const list = data.length const list = data.length
const clickHandler = (index) => { const clickHandler = (index) => {
onClick(index) onClick(index)
@@ -8,35 +9,36 @@ const Country = ({data,onClick}) => {
return ( return (
<div> <div>
{list > 10 ? 'Too many matches, specific another filter' : {list > 10 ? 'Too many matches, specific another filter' :
list > 1 ? data.map((item, index) => <CountryLongList key={item.alpha2Code} data={item} onClick={() => clickHandler(index)}/>) : list > 1 ? data.map((item, index) => <CountryLongList key={item.alpha2Code} data={item} onClick={() => clickHandler(index)} weather={weather}/>) :
data.map(item => <CountryDetail key={item.alpha2Code} data={item}/>) data.map(item => <CountryDetail key={item.alpha2Code} data={item} weather={weather}/>)
} }
</div> </div>
) )
} }
const CountryLongList = ({data, onClick}) => { const CountryLongList = ({data, onClick,weather}) => {
const show = data.Show const show = data.Show
console.log(show) console.log(show)
return ( return (
<div> <div>
<p>
<span>{data.name}</span> <span>{data.name}</span>
<button onClick={onClick}> <button onClick={onClick}>
{show === false ? "Show" : "Hide"} {show === false ? "Show" : "Hide"}
</button> </button>
{show === true && <CountryDetail key={data.alpha2Code} data={data}/>} <div>{show === true && <CountryDetail key={data.alpha2Code} data={data}/>}
</p> </div>
</div> </div>
); );
} }
const CountryDetail = ({data}) => { const CountryDetail = ({data,weather}) => {
console.log(weather)
return ( return (
<div> <div>
<h2>{data.name}</h2> <h2>{data.name}</h2>
<p> <p>
Capital: {data.capital} Capital: {data.capital}
<br /> </p>
<p>
Population: {data.population} Population: {data.population}
</p> </p>
<h3>Languagues</h3> <h3>Languagues</h3>
@@ -44,7 +46,7 @@ const CountryDetail = ({data}) => {
{data.languages.map(item =><li key={item.name}>{item.name}</li>)} {data.languages.map(item =><li key={item.name}>{item.name}</li>)}
</ul> </ul>
<img src={data.flag} alt={data.name} width="200" /> <img src={data.flag} alt={data.name} width="200" />
<Weather weather={weather} capital={data.capital} />
</div> </div>
) )
} }
+15
View File
@@ -0,0 +1,15 @@
import React from 'react'
const Weather = ({weather,capital}) => {
console.log(weather)
return (
<div>
<p>Temperature: {weather.current.temperature} Celcius</p>
<img src={weather.current.weather_icons} alt="Weather" />
<p>Wind: {weather.current.wind_speed} mph {weather.current.wind_degree} {weather.current.wind_dir}</p>
</div>
)
}
export default Weather