ampache_react/app/components/Songs.jsx

153 lines
5.5 KiB
React
Raw Normal View History

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";
import { defineMessages, FormattedMessage } from "react-intl";
import FontAwesome from "react-fontawesome";
import Immutable from "immutable";
2016-07-07 23:23:18 +02:00
import Fuse from "fuse.js";
import FilterBar from "./elements/FilterBar";
import Pagination from "./elements/Pagination";
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";
const songsMessages = defineMessages(messagesMap(Array.concat([], commonMessages, messages)));
2016-07-07 23:23:18 +02:00
class SongsTableRowCSS extends Component {
2016-07-07 23:23:18 +02:00
render () {
2016-08-02 13:07:12 +02:00
const length = formatLength(this.props.song.time);
2016-07-07 23:23:18 +02:00
const linkToArtist = "/artist/" + this.props.song.artist.id;
const linkToAlbum = "/album/" + this.props.song.album.id;
return (
<tr>
2016-08-02 13:07:12 +02:00
<td>
<button styleName="play">
<span className="sr-only">
<FormattedMessage {...songsMessages["app.common.play"]} />
</span>
<FontAwesome name="play-circle-o" aria-hidden="true" />
</button>
2016-08-02 13:07:12 +02:00
</td>
2016-07-07 23:23:18 +02:00
<td className="title">{this.props.song.name}</td>
<td className="artist"><Link to={linkToArtist}>{this.props.song.artist.name}</Link></td>
<td className="album"><Link to={linkToAlbum}>{this.props.song.album.name}</Link></td>
2016-07-07 23:23:18 +02:00
<td className="genre">{this.props.song.genre}</td>
<td className="length">{length}</td>
</tr>
);
}
}
SongsTableRowCSS.propTypes = {
2016-07-07 23:23:18 +02:00
song: PropTypes.object.isRequired
};
export let SongsTableRow = CSSModules(SongsTableRowCSS, css);
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 () {
var displayedSongs = this.props.songs;
if (this.props.filterText) {
// Use Fuse for the filter
displayedSongs = new Fuse(
2016-08-02 13:07:12 +02:00
this.props.songs.toArray(),
2016-07-07 23:23:18 +02:00
{
"keys": ["name"],
"threshold": 0.4,
"include": ["score"]
}).search(this.props.filterText);
// Keep only items in results
displayedSongs = displayedSongs.map(function (item) { return item.item; });
}
var rows = [];
displayedSongs.forEach(function (song) {
rows.push(<SongsTableRow song={song} key={song.id} />);
});
var loading = null;
if (rows.length == 0 && this.props.isFetching) {
// If we are fetching and there is nothing to show
loading = (
<p className="text-center">
<FontAwesome name="spinner" className="fa-pulse fa-3x fa-fw" aria-hidden="true" />
<span className="sr-only"><FormattedMessage {...songsMessages["app.common.loading"]} /></span>
</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>
<th>
<FormattedMessage {...songsMessages["app.songs.title"]} />
</th>
<th className="text-capitalize">
<FormattedMessage {...songsMessages["app.common.artist"]} values={{itemCount: 1}} />
</th>
<th className="text-capitalize">
<FormattedMessage {...songsMessages["app.common.album"]} values={{itemCount: 1}} />
</th>
<th>
<FormattedMessage {...songsMessages["app.songs.genre"]} />
</th>
<th>
<FormattedMessage {...songsMessages["app.songs.length"]} />
</th>
2016-07-27 13:51:30 +02:00
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
{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 = {
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({
filterText: filterText.trim()
});
}
render () {
return (
<div>
<FilterBar filterText={this.state.filterText} onUserInput={this.handleUserInput} />
<SongsTable isFetching={this.props.isFetching} songs={this.props.songs} filterText={this.state.filterText} />
<Pagination {...this.props.pagination} />
2016-07-07 23:23:18 +02:00
</div>
);
}
}
FilterablePaginatedSongsTable.propTypes = {
isFetching: PropTypes.bool.isRequired,
songs: PropTypes.instanceOf(Immutable.List).isRequired,
pagination: PropTypes.object.isRequired
2016-07-07 23:23:18 +02:00
};