Skip to content

Instantly share code, notes, and snippets.

@yeti-detective
Created September 27, 2018 19:26
Show Gist options
  • Save yeti-detective/81385d599356e3e4209de2e6528e24d5 to your computer and use it in GitHub Desktop.
Save yeti-detective/81385d599356e3e4209de2e6528e24d5 to your computer and use it in GitHub Desktop.
Method of a React web component that displays search results
// this is one method of a component from my SoundClone app
buildSearchPool () {
if (this.state.query === '') {
this.setState({
searchPool: []
})
} else {
const pool = [];
Object.keys(this.props.songs).filter((songId) => {
const song = this.props.songs[songId];
if (song.title.toUpperCase().includes(this.state.query.toUpperCase())) {
pool.push({
name: song.title,
img_url: song.image_url,
url: `/users/${song.user_id}/${song.id}`,
type: 'song'
})
};
})
Object.keys(this.props.users).forEach((userId) => {
const user = this.props.users[userId];
if (user.username.toUpperCase().includes(this.state.query.toUpperCase())) {
pool.push({
name: user.username,
img_url: user.icon_url,
url: `/users/${user.id}`,
type: 'user'
});
}
})
this.setState({
searchPool: pool
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment