2016-08-10 21:36:11 +02:00
|
|
|
// NPM imports
|
2016-07-07 23:23:18 +02:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import { bindActionCreators } from "redux";
|
|
|
|
import { connect } from "react-redux";
|
2016-08-06 16:46:54 +02:00
|
|
|
import { defineMessages, injectIntl, intlShape } from "react-intl";
|
2016-08-05 00:00:25 +02:00
|
|
|
import Immutable from "immutable";
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Local imports
|
2016-08-06 16:46:54 +02:00
|
|
|
import { messagesMap, handleErrorI18nObject } from "../utils";
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Actions
|
|
|
|
import * as actionCreators from "../actions";
|
|
|
|
|
|
|
|
// Components
|
2016-07-07 23:23:18 +02:00
|
|
|
import Artist from "../components/Artist";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Translations
|
2016-08-06 16:46:54 +02:00
|
|
|
import APIMessages from "../locales/messagesDescriptors/api";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Define translations
|
2016-08-06 16:46:54 +02:00
|
|
|
const artistMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Single artist page.
|
|
|
|
*/
|
2016-08-06 16:46:54 +02:00
|
|
|
class ArtistPageIntl extends Component {
|
2016-08-10 23:50:23 +02:00
|
|
|
componentWillMount() {
|
2016-07-07 23:23:18 +02:00
|
|
|
// Load the data
|
2016-08-10 21:36:11 +02:00
|
|
|
this.props.actions.loadArtist({
|
2016-07-07 23:23:18 +02:00
|
|
|
filter: this.props.params.id,
|
2016-08-10 23:50:23 +02:00
|
|
|
include: ["albums", "songs"],
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
componentWillUnmount() {
|
2016-08-10 21:36:11 +02:00
|
|
|
this.props.actions.decrementRefCount({
|
2016-08-10 23:50:23 +02:00
|
|
|
"artist": [this.props.artist.get("id")],
|
2016-08-10 21:36:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
render() {
|
2016-08-06 16:46:54 +02:00
|
|
|
const {formatMessage} = this.props.intl;
|
2016-08-10 21:36:11 +02:00
|
|
|
|
2016-08-06 16:46:54 +02:00
|
|
|
const error = handleErrorI18nObject(this.props.error, formatMessage, artistMessages);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
return (
|
2016-08-11 22:01:47 +02:00
|
|
|
<Artist playAction={this.props.actions.playSong} playNextAction={this.props.actions.pushSong} isFetching={this.props.isFetching} error={error} artist={this.props.artist} albums={this.props.albums} songs={this.props.songs} />
|
2016-08-06 15:30:03 +02:00
|
|
|
);
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
ArtistPageIntl.propTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get artist
|
|
|
|
let artist = state.entities.getIn(["entities", "artist", ownProps.params.id]);
|
|
|
|
let albums = new Immutable.List();
|
2016-08-06 15:30:03 +02:00
|
|
|
let songs = new Immutable.Map();
|
2016-08-10 21:36:11 +02:00
|
|
|
if (artist) {
|
2016-08-05 00:00:25 +02:00
|
|
|
// Get albums
|
2016-08-10 21:36:11 +02:00
|
|
|
let artistAlbums = artist.get("albums");
|
2016-08-05 00:00:25 +02:00
|
|
|
if (Immutable.List.isList(artistAlbums)) {
|
2016-08-10 21:36:11 +02:00
|
|
|
albums = artistAlbums.map(
|
|
|
|
id => state.entities.getIn(["entities", "album", id])
|
2016-08-05 00:00:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
// Get songs
|
2016-08-10 21:36:11 +02:00
|
|
|
let artistSongs = artist.get("songs");
|
2016-08-05 00:00:25 +02:00
|
|
|
if (Immutable.List.isList(artistSongs)) {
|
2016-08-10 21:36:11 +02:00
|
|
|
songs = state.entities.getIn(["entities", "song"]).filter(
|
|
|
|
song => artistSongs.includes(song.get("id"))
|
2016-08-05 00:00:25 +02:00
|
|
|
);
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
} else {
|
|
|
|
artist = new Immutable.Map();
|
2016-08-05 00:00:25 +02:00
|
|
|
}
|
|
|
|
return {
|
2016-08-10 21:36:11 +02:00
|
|
|
isFetching: state.entities.isFetching,
|
|
|
|
error: state.entities.error,
|
2016-08-05 00:00:25 +02:00
|
|
|
artist: artist,
|
|
|
|
albums: albums,
|
2016-08-10 23:50:23 +02:00
|
|
|
songs: songs,
|
2016-08-05 00:00:25 +02:00
|
|
|
};
|
|
|
|
};
|
2016-07-07 23:23:18 +02:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
2016-08-10 23:50:23 +02:00
|
|
|
actions: bindActionCreators(actionCreators, dispatch),
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
|
2016-08-06 16:46:54 +02:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ArtistPageIntl));
|