Skip to content

Instantly share code, notes, and snippets.

@PaquitoSoft
Created November 3, 2018 15:37
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/c1a3489c788a6e427da8e787fb6884e7 to your computer and use it in GitHub Desktop.
Save PaquitoSoft/c1a3489c788a6e427da8e787fb6884e7 to your computer and use it in GitHub Desktop.
import React from 'react';
import PodcastSummary from './product-summary';
import { getAllPodcasts } from '../api/podcaster';
import ExternalDataLoader from './external-data-loader';
class HomePage extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: ''
};
this.onFilterChanged = this.onFilterChanged.bind(this);
}
onFilterChanged(event) {
this.setState({ filter: event.target.value });
}
filterPodcasts(originalPodcasts, filter) {
const regExp = new RegExp(filter, 'i');
return originalPodcasts.filter(
podcast => regExp.test(podcast.name + podcast.author)
);
}
render() {
const { filter } = this.state;
return (
<ExternalDataLoader loader={getAllPodcasts}>
{({ isLoading, externalData: originalPodcasts}) => {
if (isLoading) return null;
const filteredPodcasts = this.filterPodcasts(originalPodcasts || [], filter);
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={this.onFilterChanged}/>
</div>
<div className="podcasts-list">
{filteredPodcasts.map(podcast => {
return (<PodcastSummary key={podcast.id} podcast={podcast} />);
})}
</div>
</div>
);
}}
</ExternalDataLoader>
);
}
}
export default HomePage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment