import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { hashHistory, Link } from 'react-router'
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
import SpotifyAuthenticationFrame from '../components/SpotifyAuthenticationFrame'
import ConfirmationButton from '../components/ConfirmationButton'
import PusherConnectionList from '../components/PusherConnectionList'
import URISchemesList from '../components/URISchemesList'
import VersionManager from '../components/VersionManager'
import Header from '../components/Header'
import Parallax from '../components/Parallax'
import Icon from '../components/Icon'
import Thumbnail from '../components/Thumbnail'
import * as coreActions from '../services/core/actions'
import * as uiActions from '../services/ui/actions'
import * as pusherActions from '../services/pusher/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class Settings extends React.Component {
constructor(props) {
super(props);
this.state = {
country: this.props.core.country,
locale: this.props.core.locale,
mopidy_host: this.props.mopidy.host,
mopidy_port: this.props.mopidy.port,
pusher_username: this.props.pusher.username,
input_in_focus: null
}
}
componentWillReceiveProps(newProps){
var changed = false
var state = this.state
if (newProps.spotify.country != this.state.spotify_country && this.state.input_in_focus != 'spotify_country'){
state.spotify_country = newProps.spotify.country
changed = true
}
if (newProps.spotify.locale != this.state.spotify_locale && this.state.input_in_focus != 'spotify_locale'){
state.spotify_locale = newProps.spotify.locale
changed = true
}
if (newProps.pusher.username != this.state.pusher_username && this.state.input_in_focus != 'pusher_username'){
state.pusher_username = newProps.pusher.username
changed = true
}
if (changed){
this.setState(state)
}
}
resetAllSettings(){
localStorage.clear();
window.location = '#'
window.location.reload(true)
return false;
}
setMopidyConfig(e){
this.setState({input_in_focus: null})
e.preventDefault();
this.props.mopidyActions.setConfig({ host: this.state.mopidy_host, port: this.state.mopidy_port });
window.location.reload(true);
return false;
}
handleBlur(name, value){
this.setState({input_in_focus: null})
var data = {}
data[name] = value
this.props.coreActions.set(data)
}
handleUsernameChange(username){
this.setState({pusher_username: username.replace(/\W/g, '')})
}
handleUsernameBlur(e){
this.setState({input_in_focus: null})
this.props.pusherActions.setUsername(this.state.pusher_username)
}
renderApplyButton(){
if (this.props.mopidy.host == this.state.mopidy_host && this.props.mopidy.port == this.state.mopidy_port) return null
return (
)
}
renderSpotifyUser(){
var user = null
if (this.props.spotify.me && this.props.spotify.authorization){
user = this.props.spotify.me
} else if (this.props.spotify.backend_username){
if (this.props.core.users && this.props.core.users['spotify:user:'+this.props.spotify.backend_username] !== undefined){
user = this.props.core.users['spotify:user:'+this.props.spotify.backend_username]
}
}
if (user){
return (
{user.display_name ? user.display_name : user.id}
)
} else if (this.props.spotify.backend_username){
return (
{this.props.spotify.backend_username}
)
} else {
return (
Unknown
)
}
}
renderSendAuthorizationButton(){
if (!this.props.spotify.authorization) return null
return (
this.props.uiActions.openModal('send_authorization', {}) }>
Share authentication
)
}
renderServiceStatus(service){
let colour = 'red'
let icon = 'close'
let name = service.charAt(0).toUpperCase() + service.slice(1).toLowerCase()
let text = 'Disconnected'
service = this.props[service]
if (service.connecting){
icon = 'plug'
colour = 'grey'
text = 'Connecting'
} else if (name == 'Spotify' && (!this.props.mopidy.uri_schemes || !this.props.mopidy.uri_schemes.includes('spotify:'))){
icon = 'exclamation-triangle'
colour = 'orange'
text = 'Mopidy-Spotify not detected'
} else if (service.connected && name == 'Spotify' && !service.authorization){
icon = 'lock'
colour = 'orange'
text = 'Limited access'
} else if (service.connected){
icon = 'check'
colour = 'green'
text = 'Connected'
}
return (
)
}
render(){
var options = (
hashHistory.push(global.baseURL+'settings/debug')}>
Debug
Help
)
return (
{this.renderServiceStatus('mopidy')}
{this.renderServiceStatus('pusher')}
{this.renderServiceStatus('spotify')}
System
Localization
Spotify
Status
{!this.props.mopidy.uri_schemes || !this.props.mopidy.uri_schemes.includes('spotify:') ?
Mopidy-Spotify not available
: null}
{this.props.spotify.authorization ?
Authorized All Spotify functionality available :
Limited access Authorize Iris for full functionality}
Authorization
{ this.renderSendAuthorizationButton() }
Current user
{ this.renderSpotifyUser() }
Advanced
Reset
this.resetAllSettings()} />
this.props.spotifyActions.refreshingToken()}>Refresh Spotify token
About
Iris is an open-source project by
James Barnsley . It is provided free and with absolutely no warranty. If you paid someone for this software, please let me know.
Google Analytics is used to help trace issues and provide valuable insight into how we can continue to make improvements.
);
}
}
const mapStateToProps = (state, ownProps) => {
return state;
}
const mapDispatchToProps = (dispatch) => {
return {
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
pusherActions: bindActionCreators(pusherActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Settings)