Skip to content

Commit

Permalink
Search Component and Lifting State Up in Callback Props
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 20, 2022
1 parent a8701ec commit 3e63fe1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 51 deletions.
105 changes: 75 additions & 30 deletions music-master/dist/index.975ef6c8.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion music-master/dist/index.975ef6c8.js.map

Large diffs are not rendered by default.

25 changes: 5 additions & 20 deletions music-master/src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import React, { Component } from 'react';
import Search from './Search';
import Artist from './Artist';
import Tracks from './Tracks';

const API_ADDRESS = 'https://spotify-api-wrapper.appspot.com';

class App extends Component {
state = { artistQuery: '', artist: null, tracks: [] };
state = { artist: null, tracks: [] };

updateArtistQuery = event => {
console.log('event.target.value', event.target.value);
this.setState({ artistQuery: event.target.value });
}

handleKeyPress = event => {
if (event.key === 'Enter') {
this.searchArtist();
}
}

searchArtist = () => {
fetch(`${API_ADDRESS}/artist/${this.state.artistQuery}`)
searchArtist = artistQuery => {
fetch(`${API_ADDRESS}/artist/${artistQuery}`)
.then(response => response.json())
.then(json => {
if (json.artists.total > 0) {
Expand All @@ -42,12 +32,7 @@ class App extends Component {
return (
<div>
<h2>Music Master</h2>
<input
onChange={this.updateArtistQuery}
onKeyPress={this.handleKeyPress}
placeholder='Search for an artist'
/>
<button onClick={this.searchArtist}>Search</button>
<Search searchArtist={this.searchArtist} />
<Artist artist={this.state.artist} />
<Tracks tracks={this.state.tracks} />
</div>
Expand Down
36 changes: 36 additions & 0 deletions music-master/src/components/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component } from 'react';

class Search extends Component {

state = { artistQuery: '' };

updateArtistQuery = event => {
console.log('event.target.value', event.target.value);
this.setState({ artistQuery: event.target.value });
}

handleKeyPress = event => {
if (event.key === 'Enter') {
this.searchArtist();
}
}

searchArtist = () => {
this.props.searchArtist(this.state.artistQuery);
}

render() {
return (
<div>
<input
onChange={this.updateArtistQuery}
onKeyPress={this.handleKeyPress}
placeholder='Search for an artist'
/>
<button onClick={this.searchArtist}>Search</button>
</div>
)
}
}

export default Search;

0 comments on commit 3e63fe1

Please sign in to comment.