Skip to content

Instantly share code, notes, and snippets.

@PaquitoSoft
Created November 3, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaquitoSoft/25350ef9ecb6938fe47c7873b8e10f7e to your computer and use it in GitHub Desktop.
Save PaquitoSoft/25350ef9ecb6938fe47c7873b8e10f7e to your computer and use it in GitHub Desktop.
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