Skip to content

Commit

Permalink
Tracks Component
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 20, 2022
1 parent 829e344 commit a8701ec
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
106 changes: 100 additions & 6 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.

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

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

Expand Down Expand Up @@ -48,6 +49,7 @@ class App extends Component {
/>
<button onClick={this.searchArtist}>Search</button>
<Artist artist={this.state.artist} />
<Tracks tracks={this.state.tracks} />
</div>
);
}
Expand Down
46 changes: 46 additions & 0 deletions music-master/src/components/Tracks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { Component } from 'react';

class Tracks extends Component {
state = { playing: false, audio: null, playingPreviewUrl: null };

playAudio = previewUrl => () => {
const audio = new Audio(previewUrl);

if (!this.state.playing) {
audio.play();
this.setState({ playing: true, audio, playingPreviewUrl: previewUrl });
} else {
this.state.audio.pause();

if (this.state.playingPreviewUrl === previewUrl) {
this.setState({ playing: false });
} else {
audio.play();
this.setState({ audio, playingPreviewUrl: previewUrl });
}
}
}

render() {
const { tracks } = this.props;

return (
<div>
{
tracks.map(track => {
const { id, name, album, preview_url } = track;

return (
<div key={id} onClick={this.playAudio(preview_url)}>
<img src={album.images[0].url} alt='track-image' />
<p>{name}</p>
</div>
)
})
}
</div>
)
}
}

export default Tracks;

0 comments on commit a8701ec

Please sign in to comment.