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'
|
|
|
|
|
import * as albumActions from '../actions/albumActions'
|
2016-10-03 14:44:27 +13:00
|
|
|
|
|
|
|
|
class Album extends React.Component{
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
album: false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// on render
|
|
|
|
|
componentDidMount(){
|
|
|
|
|
this.loadAlbum( this.props.params.id );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when props changed
|
|
|
|
|
componentWillReceiveProps( nextProps ){
|
|
|
|
|
if( nextProps.params.id != this.props.params.id ){
|
|
|
|
|
this.loadAlbum( nextProps.params.id );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadAlbum( id ){
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
cache: true,
|
|
|
|
|
url: 'https://api.spotify.com/v1/albums/'+id,
|
|
|
|
|
success: function(album){
|
|
|
|
|
self.setState({ album: album });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderAlbum(){
|
|
|
|
|
if( this.state.album ){
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h3>{ this.state.album.name }</h3>
|
2016-10-03 15:22:15 +13:00
|
|
|
<TrackList tracks={this.state.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-03 15:22:15 +13:00
|
|
|
albumActions: bindActionCreators(albumActions, dispatch)
|
2016-10-03 14:44:27 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Album)
|