ampache_react/app/components/Artist.jsx

120 lines
4.0 KiB
React
Raw Permalink Normal View History

// NPM imports
2016-07-07 23:23:18 +02:00
import React, { Component, PropTypes } from "react";
2016-08-12 15:41:27 +02:00
import ReactDOM from "react-dom";
import CSSModules from "react-css-modules";
import { defineMessages, FormattedMessage } from "react-intl";
import FontAwesome from "react-fontawesome";
import Immutable from "immutable";
// Local imports
import { messagesMap } from "../utils/";
2016-07-07 23:23:18 +02:00
// Other components
2016-07-07 23:23:18 +02:00
import { AlbumRow } from "./Album";
import DismissibleAlert from "./elements/DismissibleAlert";
2016-07-07 23:23:18 +02:00
// Translations
import commonMessages from "../locales/messagesDescriptors/common";
// Styles
import css from "../styles/Artist.scss";
// Define translations
const artistMessages = defineMessages(messagesMap(Array.concat([], commonMessages)));
/**
* Single artist page
*/
class ArtistCSS extends Component {
2016-08-12 15:41:27 +02:00
constructor(props) {
super(props);
// Set state
this.state = {
hasScrolled: false, // Not scrolled initially
};
}
componentDidUpdate() {
// After each update, check if we need to scroll to a given element
// State prevents scrolling at each and every update
if (this.refs.scroll && !this.state.hasScrolled) {
$("html, body").animate({ scrollTop: $(ReactDOM.findDOMNode(this.refs.scroll)).offset().top }, 600);
this.setState({
hasScrolled: true,
});
}
}
render() {
// Define loading message
let loading = null;
if (this.props.isFetching) {
loading = (
<div className="row text-center">
<p>
<FontAwesome name="spinner" className="fa-pulse fa-3x fa-fw" aria-hidden="true" />
<span className="sr-only"><FormattedMessage {...artistMessages["app.common.loading"]} /></span>
</p>
</div>
);
}
// Handle error
let error = null;
if (this.props.error) {
error = (<DismissibleAlert type="danger" text={this.props.error} />);
}
// Build album rows
let albumsRows = [];
2016-08-12 15:41:27 +02:00
const { albums, songs, playAction, playNextAction, scrollToAlbum } = this.props;
if (albums && songs) {
albums.forEach(function (album) {
2016-08-12 15:41:27 +02:00
// Get songs of this album
const albumSongs = album.get("tracks").map(
id => songs.get(id)
);
2016-08-12 15:41:27 +02:00
// Handle scrolling to a specific album by applying a given ref
const ref = (scrollToAlbum == album.get("id")) ? "scroll" : null;
albumsRows.push(<AlbumRow playAction={playAction} playNextAction={playNextAction} album={album} songs={albumSongs} key={album.get("id")} ref={ref}/>);
2016-07-07 23:23:18 +02:00
});
}
2016-07-07 23:23:18 +02:00
return (
<div>
{ error }
<div className="row" styleName="name">
2016-09-18 22:50:59 +02:00
<div className="col-xs-12">
<h1>{this.props.artist.get("name")}</h1>
2016-07-07 23:23:18 +02:00
<hr/>
2016-07-27 13:51:30 +02:00
</div>
</div>
<div className="row">
2016-09-18 22:50:59 +02:00
<div className="col-xs-9">
<p>{this.props.artist.get("summary")}</p>
2016-07-07 23:23:18 +02:00
</div>
2016-09-18 22:50:59 +02:00
<div className="col-xs-3 text-center">
<p><img src={this.props.artist.get("art")} width="200" height="200" className="img-responsive img-circle" styleName="art" alt={this.props.artist.get("name")}/></p>
2016-07-07 23:23:18 +02:00
</div>
</div>
{ albumsRows }
{ loading }
2016-07-07 23:23:18 +02:00
</div>
);
}
}
ArtistCSS.propTypes = {
error: PropTypes.string,
isFetching: PropTypes.bool.isRequired,
playAction: PropTypes.func.isRequired,
playNextAction: PropTypes.func.isRequired,
artist: PropTypes.instanceOf(Immutable.Map),
albums: PropTypes.instanceOf(Immutable.List),
songs: PropTypes.instanceOf(Immutable.Map),
2016-08-12 15:41:27 +02:00
scrollToAlbum: PropTypes.number,
2016-07-07 23:23:18 +02:00
};
export default CSSModules(ArtistCSS, css);