ampache_react/app/views/AlbumsPage.jsx

71 lines
2.4 KiB
React
Raw Normal View History

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";
2016-07-07 23:23:18 +02:00
import * as actionCreators from "../actions";
import { i18nRecord } from "../models/i18n";
import { buildPaginationObject, messagesMap } from "../utils";
2016-07-07 23:23:18 +02:00
import Albums from "../components/Albums";
import APIMessages from "../locales/messagesDescriptors/api";
const albumsMessages = defineMessages(messagesMap(APIMessages));
class AlbumsPageIntl 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.loadAlbums({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.loadAlbums({pageNumber: nextPage});
}
}
render () {
const {formatMessage} = this.props.intl;
if (this.props.error) {
var errorMessage = this.props.error;
if (this.props.error instanceof i18nRecord) {
errorMessage = formatMessage(albumsMessages[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 (
<Albums isFetching={this.props.isFetching} albums={this.props.albumsList} pagination={pagination} />
2016-07-07 23:23:18 +02:00
);
}
}
AlbumsPageIntl.contextTypes = {
router: PropTypes.object.isRequired
};
AlbumsPageIntl.propTypes = {
intl: intlShape.isRequired,
};
2016-07-07 23:23:18 +02:00
const mapStateToProps = (state) => ({
isFetching: state.pagination.albums.isFetching,
error: state.pagination.albums.error,
2016-07-07 23:23:18 +02:00
albumsList: state.pagination.albums.items,
currentPage: state.pagination.albums.currentPage,
nPages: state.pagination.albums.nPages
2016-07-07 23:23:18 +02:00
});
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators(actionCreators, dispatch)
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(AlbumsPageIntl));