ampache_react/app/views/AlbumsPage.jsx

97 lines
3.1 KiB
React
Raw Permalink Normal View History

// NPM imports
import React, { Component } 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 { buildPaginationObject, messagesMap, handleErrorI18nObject } 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 Albums from "../components/Albums";
// Translations
import APIMessages from "../locales/messagesDescriptors/api";
// Define translations
const albumsMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
/**
* Albums page, grid layout of albums arts.
*/
class AlbumsPageIntl extends Component {
componentWillMount() {
// Load the data for current page
2016-07-07 23:23:18 +02:00
const currentPage = parseInt(this.props.location.query.page) || 1;
this.props.actions.loadPaginatedAlbums({ pageNumber: currentPage });
2016-07-07 23:23:18 +02:00
}
componentWillReceiveProps(nextProps) {
// 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
this.props.actions.loadPaginatedAlbums({pageNumber: nextPage});
2016-07-07 23:23:18 +02:00
}
}
componentWillUnmount() {
// Unload data on page change
this.props.actions.clearPaginatedResults();
}
render() {
const {formatMessage} = this.props.intl;
const pagination = buildPaginationObject(this.props.location, this.props.currentPage, this.props.nPages, this.props.actions.goToPage);
const error = handleErrorI18nObject(this.props.error, formatMessage, albumsMessages);
2016-07-07 23:23:18 +02:00
return (
2016-08-12 13:57:53 +02:00
<Albums isFetching={this.props.isFetching} error={error} albums={this.props.albumsList} artists={this.props.artistsList} pagination={pagination} />
2016-07-07 23:23:18 +02:00
);
}
}
AlbumsPageIntl.propTypes = {
intl: intlShape.isRequired,
};
const mapStateToProps = (state) => {
let albumsList = new Immutable.List();
2016-08-12 13:57:53 +02:00
let artistsList = new Immutable.Map();
if (state.paginated.type == "album" && state.paginated.result.size > 0) {
albumsList = state.paginated.result.map(
id => state.entities.getIn(["entities", "album", id])
);
2016-08-12 13:57:53 +02:00
albumsList.forEach(function (album) {
2016-09-18 22:57:54 +02:00
if (album) {
const albumArtist = album.get("artist");
artistsList = artistsList.set(albumArtist, state.entities.getIn(["entities", "artist", albumArtist]));
}
2016-08-12 13:57:53 +02:00
});
}
return {
isFetching: state.entities.isFetching,
error: state.entities.error,
albumsList: albumsList,
2016-08-12 13:57:53 +02:00
artistsList: artistsList,
currentPage: state.paginated.currentPage,
nPages: state.paginated.nPages,
};
};
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(AlbumsPageIntl));