Created
November 3, 2018 15:46
-
-
Save PaquitoSoft/25350ef9ecb6938fe47c7873b8e10f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import useExternalData from '../hooks/use-external-data'; | |
import { getAllPodcasts } from '../api/podcaster'; | |
import PodcastSummary from './product-summary'; | |
function onFilterChangedFactory(originalPodcasts, updater) { | |
return (event) => { | |
const regExp = new RegExp(event.target.value, 'i'); | |
updater( | |
originalPodcasts.filter(podcast => regExp.test(podcast.name + podcast.author)) | |
); | |
}; | |
} | |
function HomePage() { | |
const { isLoading, data: originalPodcasts } = useExternalData(getAllPodcasts); | |
if (isLoading) return null; | |
const [filteredPodcasts, setFilteredPodcasts] = useState(originalPodcasts); | |
return ( | |
<div className="podcasts-grid"> | |
<div className="filter"> | |
<span className="badge">{filteredPodcasts.length}</span> | |
<input type="text" name="filter-value" autoFocus | |
placeholder="Filter podcasts..." onChange={onFilterChangedFactory(originalPodcasts, setFilteredPodcasts)} /> | |
</div> | |
<div className="podcasts-list"> | |
{filteredPodcasts.map(podcast => { | |
return (<PodcastSummary key={podcast.id} podcast={podcast} />); | |
})} | |
</div> | |
</div> | |
); | |
} | |
export default HomePage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment