2016-08-10 21:36:11 +02:00
|
|
|
// NPM imports
|
2016-08-06 16:46:54 +02:00
|
|
|
import React, { Component } 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
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Local imports
|
2016-08-06 16:46:54 +02:00
|
|
|
import { buildPaginationObject, 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 Albums from "../components/Albums";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Translations
|
2016-08-01 00:26:52 +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 albumsMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
|
2016-08-01 00:26:52 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Albums page, grid layout of albums arts.
|
|
|
|
*/
|
2016-08-01 00:26:52 +02:00
|
|
|
class AlbumsPageIntl extends Component {
|
2016-08-10 23:50:23 +02:00
|
|
|
componentWillMount() {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Load the data for current page
|
2016-07-07 23:23:18 +02:00
|
|
|
const currentPage = parseInt(this.props.location.query.page) || 1;
|
2016-08-10 21:36:11 +02:00
|
|
|
this.props.actions.loadPaginatedAlbums({ pageNumber: currentPage });
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Load the data if page has changed
|
2016-07-07 23:23:18 +02:00
|
|
|
const currentPage = parseInt(this.props.location.query.page) || 1;
|
|
|
|
const nextPage = parseInt(nextProps.location.query.page) || 1;
|
|
|
|
if (currentPage != nextPage) {
|
2016-08-11 01:05:33 +02:00
|
|
|
// Unload data on page change
|
|
|
|
this.props.actions.clearPaginatedResults();
|
|
|
|
// Load new data
|
2016-07-07 23:23:18 +02:00
|
|
|
this.props.actions.loadAlbums({pageNumber: nextPage});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
componentWillUnmount() {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Unload data on page change
|
2016-08-10 23:50:23 +02:00
|
|
|
this.props.actions.clearPaginatedResults();
|
2016-08-10 21:36:11 +02:00
|
|
|
}
|
2016-08-06 16:46:54 +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
|
|
|
|
|
|
|
const pagination = buildPaginationObject(this.props.location, this.props.currentPage, this.props.nPages, this.props.actions.goToPage);
|
|
|
|
|
2016-08-06 16:46:54 +02:00
|
|
|
const error = handleErrorI18nObject(this.props.error, formatMessage, albumsMessages);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
2016-08-06 16:46:54 +02:00
|
|
|
<Albums isFetching={this.props.isFetching} error={error} albums={this.props.albumsList} pagination={pagination} />
|
2016-07-07 23:23:18 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
AlbumsPageIntl.propTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
let albumsList = new Immutable.List();
|
2016-08-10 21:36:11 +02:00
|
|
|
if (state.paginated.type == "album" && state.paginated.result.size > 0) {
|
|
|
|
albumsList = state.paginated.result.map(
|
|
|
|
id => state.entities.getIn(["entities", "album", id])
|
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
|
|
|
albumsList: albumsList,
|
2016-08-10 21:36:11 +02:00
|
|
|
currentPage: state.paginated.currentPage,
|
2016-08-10 23:50:23 +02:00
|
|
|
nPages: state.paginated.nPages,
|
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-01 00:26:52 +02:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(AlbumsPageIntl));
|