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-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 Songs from "../components/Songs";
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
import APIMessages from "../locales/messagesDescriptors/api";
|
|
|
|
|
|
|
|
const songsMessages = defineMessages(messagesMap(APIMessages));
|
|
|
|
|
|
|
|
class SongsPageIntl 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.loadSongs({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.loadSongs({pageNumber: nextPage});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-08-01 00:26:52 +02:00
|
|
|
const {formatMessage} = this.props.intl;
|
|
|
|
if (this.props.error) {
|
|
|
|
var errorMessage = this.props.error;
|
|
|
|
if (this.props.error instanceof i18nRecord) {
|
|
|
|
errorMessage = formatMessage(songsMessages[this.props.error.id], this.props.error.values);
|
|
|
|
}
|
|
|
|
alert(errorMessage);
|
|
|
|
this.context.router.replace("/");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
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
|
|
|
<Songs isFetching={this.props.isFetching} songs={this.props.songsList} pagination={pagination} />
|
2016-07-07 23:23:18 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
SongsPageIntl.contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
SongsPageIntl.propTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
const mapStateToProps = (state) => ({
|
2016-08-01 00:26:52 +02:00
|
|
|
isFetching: state.pagination.songs.isFetching,
|
2016-07-07 23:23:18 +02:00
|
|
|
songsList: state.pagination.songs.items,
|
2016-08-01 00:26:52 +02:00
|
|
|
currentPage: state.pagination.songs.currentPage,
|
|
|
|
nPages: state.pagination.songs.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(SongsPageIntl));
|