Files
Iris/src/js/components/Album.js

81 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-10-03 14:44:27 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2016-10-03 15:22:15 +13:00
import TrackList from './TrackList'
2016-10-16 13:28:21 +13:00
import * as spotifyActions from '../services/spotify/actions'
2016-10-03 14:44:27 +13:00
class Album extends React.Component{
constructor(props) {
super(props);
}
// on render
componentDidMount(){
2016-10-16 13:28:21 +13:00
this.props.spotifyActions.loadAlbum( this.props.params.uri );
2016-10-03 14:44:27 +13:00
}
// when props changed
componentWillReceiveProps( nextProps ){
if( nextProps.params.uri != this.props.params.uri ){
2016-10-16 13:28:21 +13:00
this.props.spotifyActions.loadAlbum( nextProps.params.uri );
2016-10-03 14:44:27 +13:00
}
}
loadAlbum( uri ){
2016-10-03 14:44:27 +13:00
let self = this;
var id = uri.replace('spotify:album:','');
2016-10-03 14:44:27 +13:00
$.ajax({
method: 'GET',
cache: true,
url: 'https://api.spotify.com/v1/albums/'+id,
success: function(album){
self.setState({ album: album });
}
});
}
renderAlbum(){
2016-10-16 13:28:21 +13:00
if( this.props.spotify.album ){
2016-10-03 14:44:27 +13:00
return (
<div>
2016-10-16 13:28:21 +13:00
<h3>{ this.props.spotify.album.name }</h3>
<TrackList tracks={this.props.spotify.album.tracks.items} />
2016-10-03 14:44:27 +13:00
</div>
);
}
return null;
}
render(){
return (
<div>
<h3>Single album</h3>
{ this.renderAlbum() }
</div>
);
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return state;
}
const mapDispatchToProps = (dispatch) => {
return {
2016-10-16 13:28:21 +13:00
spotifyActions: bindActionCreators(spotifyActions, dispatch)
2016-10-03 14:44:27 +13:00
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Album)