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 Songs from "../components/Songs";
|
|
|
|
|
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 songsMessages = defineMessages(messagesMap(Array.concat([], APIMessages)));
|
2016-08-01 00:26:52 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Paginated table of available songs
|
|
|
|
*/
|
2016-08-01 00:26:52 +02:00
|
|
|
class SongsPageIntl 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.loadPaginatedSongs({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-08-10 21:36:11 +02:00
|
|
|
this.props.actions.loadPaginatedSongs({pageNumber: nextPage});
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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, songsMessages);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
2016-08-11 22:01:47 +02:00
|
|
|
<Songs playAction={this.props.actions.playSong} playNextAction={this.props.actions.pushSong} 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();
|
2016-08-10 21:36:11 +02:00
|
|
|
if (state.paginated.type == "song" && state.paginated.result.size > 0) {
|
|
|
|
songsList = state.paginated.result.map(function (id) {
|
|
|
|
let song = state.entities.getIn(["entities", "song", id]);
|
|
|
|
// Add artist and album infos to song
|
2016-08-12 16:29:13 +02:00
|
|
|
const artist = state.entities.getIn(["entities", "artist", song.get("artist")]); // TODO: get on undefined
|
2016-08-10 21:36:11 +02:00
|
|
|
const album = state.entities.getIn(["entities", "album", song.get("album")]);
|
|
|
|
return (
|
|
|
|
song
|
|
|
|
.set("artist", new Immutable.Map({id: artist.get("id"), name: artist.get("name")}))
|
|
|
|
.set("album", new Immutable.Map({id: album.get("id"), name: album.get("name")}))
|
|
|
|
);
|
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
|
|
|
songsList: songsList,
|
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(SongsPageIntl));
|