Files
Iris/src/js/views/User.js

140 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-11-18 15:31:06 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
import Thumbnail from '../components/Thumbnail'
import PlaylistGrid from '../components/PlaylistGrid'
import FollowButton from '../components/FollowButton'
import LazyLoadListener from '../components/LazyLoadListener'
import SidebarToggleButton from '../components/SidebarToggleButton'
import Parallax from '../components/Parallax'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2016-11-18 15:31:06 +13:00
import * as helpers from '../helpers'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class User extends React.Component{
constructor(props) {
super(props);
}
componentDidMount(){
this.loadUser();
}
componentWillReceiveProps( nextProps ){
if( nextProps.params.uri != this.props.params.uri ) this.loadUser( nextProps )
}
loadUser( props = this.props ){
2017-01-19 08:56:12 +13:00
if (!props.user) this.props.spotifyActions.getUser( props.params.uri )
2016-11-18 15:31:06 +13:00
}
loadMore(){
2017-01-19 08:56:12 +13:00
this.props.spotifyActions.getURL( this.props.user.playlists_more, 'SPOTIFY_USER_PLAYLISTS_LOADED', this.props.params.uri )
2016-11-18 15:31:06 +13:00
}
2017-02-08 08:34:14 +13:00
isMe(){
let userid = helpers.getFromUri('userid',this.props.params.uri)
switch (this.props.spotify_authentication_provider){
case 'backend':
if (this.props.spotify_authorized && this.props.me){
return userid == this.props.me.id
}
return false
break
case 'http_api':
return userid == this.props.spotify_backend_username
default:
return false
}
2016-11-18 15:31:06 +13:00
}
render(){
var user_id = helpers.getFromUri('userid',this.props.params.uri)
if (helpers.isLoading(this.props.load_queue,['spotify_users/'+user_id,'spotify_users/'+user_id+'/playlists/?'])){
2017-02-26 18:33:20 +13:00
return (
<div className="body-loader">
<div className="loader"></div>
2017-02-26 18:33:20 +13:00
</div>
)
}
if (!this.props.user) return null
var playlists = []
if (this.props.user.playlists_uris){
for (var i = 0; i < this.props.user.playlists_uris.length; i++){
var uri = this.props.user.playlists_uris[i]
if (this.props.playlists.hasOwnProperty(uri)){
playlists.push(this.props.playlists[uri])
}
}
}
if (this.props.user && this.props.user.images ){
var image = helpers.sizedImages(this.props.user.images).huge
2017-02-26 18:33:20 +13:00
} else {
var image = null
}
2017-02-26 18:33:20 +13:00
return (
<div className="view user-view">
<SidebarToggleButton />
<div className="intro">
<Parallax image={image} />
<div className="liner">
<h1>{ this.props.user.display_name ? this.props.user.display_name : this.props.user.id }</h1>
<h2>
<ul className="details">
<li>{this.props.user.playlists_total ? this.props.user.playlists_total.toLocaleString() : 0} playlists</li>
<li>{this.props.user.followers.total.toLocaleString()} followers</li>
{this.isMe() ? <li>You</li> : null}
</ul>
</h2>
2017-02-26 18:33:20 +13:00
<div className="actions">
<FollowButton className="primary" uri={this.props.params.uri} addText="Follow" removeText="Unfollow" />
2017-02-26 18:33:20 +13:00
</div>
</div>
2016-11-18 15:31:06 +13:00
</div>
<div className="content-wrapper">
<section className="grid-wrapper">
<h4>Playlists</h4>
<PlaylistGrid playlists={playlists} />
<LazyLoadListener enabled={this.props.user.playlists_more} loadMore={ () => this.loadMore() }/>
</section>
</div>
</div>
)
2016-11-18 15:31:06 +13:00
}
}
const mapStateToProps = (state, ownProps) => {
return {
load_queue: state.ui.load_queue,
2016-11-18 15:31:06 +13:00
spotify_authorized: state.spotify.authorized,
spotify_authentication_provider: state.spotify.authentication_provider,
spotify_backend_username: state.spotify.backend_username,
2016-11-18 15:31:06 +13:00
me: state.spotify.me,
playlists: state.core.playlists,
user: (state.core.users && state.core.users[ownProps.params.uri] !== undefined ? state.core.users[ownProps.params.uri] : false),
users: state.core.users
2016-11-18 15:31:06 +13:00
};
}
const mapDispatchToProps = (dispatch) => {
return {
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(User)