ampache_react/app/views/ArtistPage.jsx

105 lines
3.1 KiB
React
Raw Normal View History

// NPM imports
import React, { Component, PropTypes } from "react";
2016-07-07 23:23:18 +02:00
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { defineMessages, injectIntl, intlShape } from "react-intl";
import Immutable from "immutable";
2016-07-07 23:23:18 +02:00
// Local imports
import { messagesMap, handleErrorI18nObject, filterInt } from "../utils";
2016-07-07 23:23:18 +02:00
// Actions
import * as actionCreators from "../actions";
// Components
2016-07-07 23:23:18 +02:00
import Artist from "../components/Artist";
// Translations
import APIMessages from "../locales/messagesDescriptors/api";
// Define translations
const artistMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
/**
* Single artist page.
*/
class ArtistPageIntl extends Component {
componentWillMount() {
2016-08-12 13:57:53 +02:00
const id = filterInt(this.props.params.artist.split("-")[0]);
if (isNaN(id)) {
// Redirect to homepage
this.context.router.replace({
pathname: "/",
});
}
2016-07-07 23:23:18 +02:00
// Load the data
this.props.actions.loadArtist({
filter: id,
include: ["albums", "songs"],
2016-07-07 23:23:18 +02:00
});
}
componentWillUnmount() {
this.props.actions.decrementRefCount({
"artist": [this.props.artist.get("id")],
});
}
render() {
const {formatMessage} = this.props.intl;
const error = handleErrorI18nObject(this.props.error, formatMessage, artistMessages);
return (
2016-08-12 15:41:27 +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} scrollToAlbum={parseInt(this.props.params.album)} />
);
2016-07-07 23:23:18 +02:00
}
}
ArtistPageIntl.propTypes = {
intl: intlShape.isRequired,
};
ArtistPageIntl.contextTypes = {
router: PropTypes.object.isRequired,
};
const mapStateToProps = (state, ownProps) => {
2016-08-12 13:57:53 +02:00
const id = ownProps.params.artist.split("-")[0];
// Get artist
let artist = state.entities.getIn(["entities", "artist", id]);
let albums = new Immutable.List();
let songs = new Immutable.Map();
if (artist) {
// Get albums
let artistAlbums = artist.get("albums");
if (Immutable.List.isList(artistAlbums)) {
albums = artistAlbums.map(
id => state.entities.getIn(["entities", "album", id])
);
}
// Get songs
let artistSongs = artist.get("songs");
if (Immutable.List.isList(artistSongs)) {
songs = state.entities.getIn(["entities", "song"]).filter(
song => artistSongs.includes(song.get("id"))
);
}
} else {
artist = new Immutable.Map();
}
return {
isFetching: state.entities.isFetching,
error: state.entities.error,
artist: artist,
albums: albums,
songs: songs,
};
};
2016-07-07 23:23:18 +02:00
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators(actionCreators, dispatch),
2016-07-07 23:23:18 +02:00
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ArtistPageIntl));