Completed 2.13 (data for countries, step 2)

This commit is contained in:
2021-09-02 11:02:24 +07:00
parent 50c2746682
commit d5317482b9
2 changed files with 41 additions and 21 deletions
+19 -9
View File
@@ -7,10 +7,7 @@ const App = () => {
const [newSearch, setNewSerach] = useState('') const [newSearch, setNewSerach] = useState('')
const [CountryList, setCountryList] = useState([]) const [CountryList, setCountryList] = useState([])
const [ResultList, setResultList] = useState([])
const handleSearchChange =(event) => {
setNewSerach(event.target.value)
}
useEffect(() => { useEffect(() => {
axios axios
@@ -20,15 +17,28 @@ const App = () => {
}) })
}, []) }, [])
const clickHandler = (index) => {
const filtered = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase())) console.log(index)
console.log(newSearch.toLowerCase()) const newresult = ResultList
newresult[index].Show = !newresult[index].Show
setResultList([...newresult])
}
const handleSearchChange = (event) => {
setNewSerach(event.target.value)
}
useEffect(() => {
const sfilter = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase()))
const filter = sfilter.map(item => ({...item,Show:false}))
setResultList(filter)
}, [newSearch, CountryList])
return ( return (
<div> <div>
<h2>Country</h2> <h2>Country</h2>
<Filter value={newSearch} onChange={handleSearchChange} /> <Filter value={newSearch} onChange={handleSearchChange} />
<Country data={filtered} /> <Country data={ResultList} onClick={clickHandler}/>
</div> </div>
) )
} }
+22 -12
View File
@@ -1,26 +1,36 @@
import React from 'react'; import React from 'react';
const Country = (props) => { const Country = ({data,onClick}) => {
const filtered = props.data const list = data.length
const clickHandler = (index) => {
onClick(index)
}
return ( return (
<div> <div>
{filtered.length > 10 ? {list > 10 ? 'Too many matches, specific another filter' :
'Too many matches, specific another filter' : filtered.length !== 1 ? list > 1 ? data.map((item, index) => <CountryLongList key={item.alpha2Code} data={item} onClick={() => clickHandler(index)}/>) :
filtered.map(item => <CountryLongList key={item.alpha2Code} data={item}/>): data.map(item => <CountryDetail key={item.alpha2Code} data={item}/>)
filtered.map(item => <CountryDetail key={item.alpha2Code} data={item}/>)} }
</div> </div>
) )
} }
const CountryLongList = ({data}) => { const CountryLongList = ({data, onClick}) => {
const show = data.Show
console.log(show)
return ( return (
<div> <div>
<p>{data.name}</p> <p>
</div> <span>{data.name}</span>
) <button onClick={onClick}>
{show === false ? "Show" : "Hide"}
</button>
{show === true && <CountryDetail key={data.alpha2Code} data={data}/>}
</p>
</div>
);
} }
const CountryDetail = ({data}) => { const CountryDetail = ({data}) => {
console.log(data)
return ( return (
<div> <div>
<h2>{data.name}</h2> <h2>{data.name}</h2>