2016-08-01 00:26:52 +02:00
|
|
|
import React, { Component, PropTypes } from "react";
|
2016-07-07 23:23:18 +02:00
|
|
|
import { bindActionCreators } from "redux";
|
|
|
|
import { connect } from "react-redux";
|
2016-08-01 00:26:52 +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
|
|
|
|
|
|
|
import * as actionCreators from "../actions";
|
2016-08-01 00:26:52 +02:00
|
|
|
import { i18nRecord } from "../models/i18n";
|
|
|
|
import { buildPaginationObject, messagesMap } from "../utils";
|
2016-07-07 23:23:18 +02:00
|
|
|
|
|
|
|
import Artists from "../components/Artists";
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
import APIMessages from "../locales/messagesDescriptors/api";
|
|
|
|
|
|
|
|
const artistsMessages = defineMessages(messagesMap(APIMessages));
|
|
|
|
|
|
|
|
class ArtistsPageIntl extends Component {
|
2016-07-07 23:23:18 +02:00
|
|
|
componentWillMount () {
|
|
|
|
const currentPage = parseInt(this.props.location.query.page) || 1;
|
|
|
|
// Load the data
|
|
|
|
this.props.actions.loadArtists({pageNumber: currentPage});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
const currentPage = parseInt(this.props.location.query.page) || 1;
|
|
|
|
const nextPage = parseInt(nextProps.location.query.page) || 1;
|
|
|
|
if (currentPage != nextPage) {
|
|
|
|
// Load the data
|
|
|
|
this.props.actions.loadArtists({pageNumber: nextPage});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-08-01 00:26:52 +02:00
|
|
|
const {formatMessage} = this.props.intl;
|
|
|
|
if (this.props.error) {
|
2016-08-05 00:00:25 +02:00
|
|
|
let errorMessage = this.props.error;
|
2016-08-01 00:26:52 +02:00
|
|
|
if (this.props.error instanceof i18nRecord) {
|
|
|
|
errorMessage = formatMessage(artistsMessages[this.props.error.id], this.props.error.values);
|
|
|
|
}
|
|
|
|
alert(errorMessage);
|
|
|
|
this.context.router.replace("/");
|
2016-08-05 00:00:25 +02:00
|
|
|
return (<div></div>);
|
2016-08-01 00:26:52 +02:00
|
|
|
}
|
|
|
|
const pagination = buildPaginationObject(this.props.location, this.props.currentPage, this.props.nPages, this.props.actions.goToPageAction);
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
2016-08-01 00:26:52 +02:00
|
|
|
<Artists isFetching={this.props.isFetching} artists={this.props.artistsList} pagination={pagination} />
|
2016-07-07 23:23:18 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
ArtistsPageIntl.contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
ArtistsPageIntl.propTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
let artistsList = new Immutable.List();
|
|
|
|
if (state.api.result.get("artist")) {
|
|
|
|
artistsList = state.api.result.get("artist").map(
|
|
|
|
id => state.api.entities.getIn(["artist", id])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isFetching: state.api.isFetching,
|
|
|
|
error: state.api.error,
|
|
|
|
artistsList: artistsList,
|
|
|
|
currentPage: state.api.currentPage,
|
|
|
|
nPages: state.api.nPages,
|
|
|
|
};
|
|
|
|
};
|
2016-07-07 23:23:18 +02:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
actions: bindActionCreators(actionCreators, dispatch)
|
|
|
|
});
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ArtistsPageIntl));
|