2016-07-07 23:23:18 +02:00
|
|
|
import React, { Component, PropTypes } from "react";
|
|
|
|
import { Link} from "react-router";
|
2016-07-30 01:05:25 +02:00
|
|
|
import CSSModules from "react-css-modules";
|
2016-08-07 00:58:36 +02:00
|
|
|
import { defineMessages, injectIntl, intlShape, FormattedMessage } from "react-intl";
|
2016-08-04 15:28:07 +02:00
|
|
|
import FontAwesome from "react-fontawesome";
|
2016-08-01 00:26:52 +02:00
|
|
|
import Immutable from "immutable";
|
2016-07-07 23:23:18 +02:00
|
|
|
import Fuse from "fuse.js";
|
|
|
|
|
2016-08-06 16:46:54 +02:00
|
|
|
import DismissibleAlert from "./elements/DismissibleAlert";
|
2016-07-07 23:23:18 +02:00
|
|
|
import FilterBar from "./elements/FilterBar";
|
|
|
|
import Pagination from "./elements/Pagination";
|
2016-07-28 23:14:52 +02:00
|
|
|
import { formatLength, messagesMap } from "../utils";
|
|
|
|
|
|
|
|
import commonMessages from "../locales/messagesDescriptors/common";
|
|
|
|
import messages from "../locales/messagesDescriptors/Songs";
|
|
|
|
|
2016-07-30 01:05:25 +02:00
|
|
|
import css from "../styles/Songs.scss";
|
|
|
|
|
2016-07-28 23:14:52 +02:00
|
|
|
const songsMessages = defineMessages(messagesMap(Array.concat([], commonMessages, messages)));
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
class SongsTableRowCSSIntl extends Component {
|
2016-07-07 23:23:18 +02:00
|
|
|
render () {
|
2016-08-07 00:58:36 +02:00
|
|
|
const { formatMessage } = this.props.intl;
|
2016-08-05 00:00:25 +02:00
|
|
|
const length = formatLength(this.props.song.get("time"));
|
|
|
|
const linkToArtist = "/artist/" + this.props.song.getIn(["artist", "id"]);
|
|
|
|
const linkToAlbum = "/album/" + this.props.song.getIn(["album", "id"]);
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
|
|
|
<tr>
|
2016-08-02 13:07:12 +02:00
|
|
|
<td>
|
2016-08-07 00:58:36 +02:00
|
|
|
<button styleName="play" title={formatMessage(songsMessages["app.common.play"])} onClick={() => this.props.playAction(this.props.song.get("id"))}>
|
2016-08-03 15:44:29 +02:00
|
|
|
<span className="sr-only">
|
|
|
|
<FormattedMessage {...songsMessages["app.common.play"]} />
|
|
|
|
</span>
|
2016-08-04 15:28:07 +02:00
|
|
|
<FontAwesome name="play-circle-o" aria-hidden="true" />
|
2016-08-03 15:44:29 +02:00
|
|
|
</button>
|
2016-08-02 13:07:12 +02:00
|
|
|
</td>
|
2016-08-05 00:00:25 +02:00
|
|
|
<td className="title">{this.props.song.get("name")}</td>
|
|
|
|
<td className="artist"><Link to={linkToArtist}>{this.props.song.getIn(["artist", "name"])}</Link></td>
|
|
|
|
<td className="album"><Link to={linkToAlbum}>{this.props.song.getIn(["album", "name"])}</Link></td>
|
|
|
|
<td className="genre">{this.props.song.get("genre")}</td>
|
2016-07-07 23:23:18 +02:00
|
|
|
<td className="length">{length}</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
SongsTableRowCSSIntl.propTypes = {
|
|
|
|
playAction: PropTypes.func.isRequired,
|
|
|
|
song: PropTypes.instanceOf(Immutable.Map).isRequired,
|
|
|
|
intl: intlShape.isRequired
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
export let SongsTableRow = injectIntl(CSSModules(SongsTableRowCSSIntl, css));
|
2016-08-03 15:44:29 +02:00
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-07-30 01:05:25 +02:00
|
|
|
class SongsTableCSS extends Component {
|
2016-07-07 23:23:18 +02:00
|
|
|
render () {
|
2016-08-05 00:00:25 +02:00
|
|
|
let displayedSongs = this.props.songs;
|
2016-07-07 23:23:18 +02:00
|
|
|
if (this.props.filterText) {
|
|
|
|
// Use Fuse for the filter
|
|
|
|
displayedSongs = new Fuse(
|
2016-08-08 12:35:21 +02:00
|
|
|
this.props.songs.toJS(),
|
2016-07-07 23:23:18 +02:00
|
|
|
{
|
|
|
|
"keys": ["name"],
|
|
|
|
"threshold": 0.4,
|
|
|
|
"include": ["score"]
|
|
|
|
}).search(this.props.filterText);
|
|
|
|
// Keep only items in results
|
2016-08-08 12:35:21 +02:00
|
|
|
displayedSongs = displayedSongs.map(function (item) { return new Immutable.Map(item.item); });
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
let rows = [];
|
2016-08-07 00:58:36 +02:00
|
|
|
const { playAction } = this.props;
|
2016-07-07 23:23:18 +02:00
|
|
|
displayedSongs.forEach(function (song) {
|
2016-08-07 00:58:36 +02:00
|
|
|
rows.push(<SongsTableRow playAction={playAction} song={song} key={song.get("id")} />);
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
2016-08-05 00:00:25 +02:00
|
|
|
let loading = null;
|
2016-08-01 00:26:52 +02:00
|
|
|
if (rows.length == 0 && this.props.isFetching) {
|
|
|
|
// If we are fetching and there is nothing to show
|
|
|
|
loading = (
|
|
|
|
<p className="text-center">
|
2016-08-04 15:28:07 +02:00
|
|
|
<FontAwesome name="spinner" className="fa-pulse fa-3x fa-fw" aria-hidden="true" />
|
|
|
|
<span className="sr-only"><FormattedMessage {...songsMessages["app.common.loading"]} /></span>
|
2016-08-01 00:26:52 +02:00
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
2016-07-27 13:51:30 +02:00
|
|
|
<div className="table-responsive">
|
2016-07-30 01:05:25 +02:00
|
|
|
<table className="table table-hover" styleName="songs">
|
2016-07-27 13:51:30 +02:00
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th></th>
|
2016-07-28 23:14:52 +02:00
|
|
|
<th>
|
|
|
|
<FormattedMessage {...songsMessages["app.songs.title"]} />
|
|
|
|
</th>
|
2016-08-01 00:26:52 +02:00
|
|
|
<th className="text-capitalize">
|
2016-07-29 23:57:21 +02:00
|
|
|
<FormattedMessage {...songsMessages["app.common.artist"]} values={{itemCount: 1}} />
|
2016-07-28 23:14:52 +02:00
|
|
|
</th>
|
2016-08-01 00:26:52 +02:00
|
|
|
<th className="text-capitalize">
|
2016-07-29 23:57:21 +02:00
|
|
|
<FormattedMessage {...songsMessages["app.common.album"]} values={{itemCount: 1}} />
|
2016-07-28 23:14:52 +02:00
|
|
|
</th>
|
|
|
|
<th>
|
2016-07-29 23:57:21 +02:00
|
|
|
<FormattedMessage {...songsMessages["app.songs.genre"]} />
|
2016-07-28 23:14:52 +02:00
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
<FormattedMessage {...songsMessages["app.songs.length"]} />
|
|
|
|
</th>
|
2016-07-27 13:51:30 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>{rows}</tbody>
|
|
|
|
</table>
|
2016-08-01 00:26:52 +02:00
|
|
|
{loading}
|
2016-07-27 13:51:30 +02:00
|
|
|
</div>
|
2016-07-07 23:23:18 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 01:05:25 +02:00
|
|
|
SongsTableCSS.propTypes = {
|
2016-08-07 00:58:36 +02:00
|
|
|
playAction: PropTypes.func.isRequired,
|
2016-08-01 00:26:52 +02:00
|
|
|
songs: PropTypes.instanceOf(Immutable.List).isRequired,
|
2016-07-07 23:23:18 +02:00
|
|
|
filterText: PropTypes.string
|
|
|
|
};
|
|
|
|
|
2016-07-30 01:05:25 +02:00
|
|
|
export let SongsTable = CSSModules(SongsTableCSS, css);
|
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
|
|
|
|
export default class FilterablePaginatedSongsTable extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
filterText: ""
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleUserInput = this.handleUserInput.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUserInput (filterText) {
|
|
|
|
this.setState({
|
2016-08-08 12:35:21 +02:00
|
|
|
filterText: filterText
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-08-06 16:46:54 +02:00
|
|
|
let error = null;
|
|
|
|
if (this.props.error) {
|
|
|
|
error = (<DismissibleAlert type="danger" text={this.props.error} />);
|
|
|
|
}
|
|
|
|
|
2016-07-07 23:23:18 +02:00
|
|
|
return (
|
|
|
|
<div>
|
2016-08-06 16:46:54 +02:00
|
|
|
{ error }
|
2016-07-07 23:23:18 +02:00
|
|
|
<FilterBar filterText={this.state.filterText} onUserInput={this.handleUserInput} />
|
2016-08-07 00:58:36 +02:00
|
|
|
<SongsTable playAction={this.props.playAction} isFetching={this.props.isFetching} songs={this.props.songs} filterText={this.state.filterText} />
|
2016-08-01 00:26:52 +02:00
|
|
|
<Pagination {...this.props.pagination} />
|
2016-07-07 23:23:18 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterablePaginatedSongsTable.propTypes = {
|
2016-08-07 00:58:36 +02:00
|
|
|
playAction: PropTypes.func.isRequired,
|
2016-08-01 00:26:52 +02:00
|
|
|
isFetching: PropTypes.bool.isRequired,
|
2016-08-06 16:46:54 +02:00
|
|
|
error: PropTypes.string,
|
2016-08-01 00:26:52 +02:00
|
|
|
songs: PropTypes.instanceOf(Immutable.List).isRequired,
|
|
|
|
pagination: PropTypes.object.isRequired
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|