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
|
|
|
|
|
|
|
import * as actionCreators from "../actions";
|
2016-08-06 16:46:54 +02:00
|
|
|
import { buildPaginationObject, messagesMap, handleErrorI18nObject } 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";
|
|
|
|
|
2016-08-06 16:46:54 +02:00
|
|
|
const songsMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
|
2016-08-01 00:26:52 +02:00
|
|
|
|
|
|
|
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 pagination = buildPaginationObject(this.props.location, this.props.currentPage, this.props.nPages, this.props.actions.goToPageAction);
|
2016-08-06 16:46:54 +02:00
|
|
|
|
|
|
|
const {formatMessage} = this.props.intl;
|
|
|
|
const error = handleErrorI18nObject(this.props.error, formatMessage, songsMessages);
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
2016-08-06 16:46:54 +02:00
|
|
|
<Songs isFetching={this.props.isFetching} error={error} songs={this.props.songsList} pagination={pagination} />
|
2016-07-07 23:23:18 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
SongsPageIntl.propTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
let songsList = new Immutable.List();
|
|
|
|
if (state.api.result.get("song")) {
|
|
|
|
songsList = state.api.result.get("song").map(function (id) {
|
|
|
|
let song = state.api.entities.getIn(["track", id]);
|
|
|
|
// Add artist and album infos
|
|
|
|
const artist = state.api.entities.getIn(["artist", song.get("artist")]);
|
|
|
|
const album = state.api.entities.getIn(["album", song.get("album")]);
|
|
|
|
song = song.set("artist", new Immutable.Map({id: artist.get("id"), name: artist.get("name")}));
|
|
|
|
song = song.set("album", new Immutable.Map({id: album.get("id"), name: album.get("name")}));
|
|
|
|
return song;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isFetching: state.api.isFetching,
|
|
|
|
error: state.api.error,
|
|
|
|
artistsList: state.api.entities.get("artist"),
|
|
|
|
albumsList: state.api.entities.get("album"),
|
|
|
|
songsList: songsList,
|
|
|
|
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(SongsPageIntl));
|