import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { hashHistory, Link } from 'react-router'
import { bindActionCreators } from 'redux'
import ConfirmationButton from '../components/Fields/ConfirmationButton'
import PusherConnectionList from '../components/PusherConnectionList'
import SourcesPriority from '../components/Fields/SourcesPriority'
import Header from '../components/Header'
import Icon from '../components/Icon'
import Thumbnail from '../components/Thumbnail'
import URILink from '../components/URILink'
import Services from '../components/Services'
import * as helpers from '../helpers'
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 lastfmActions from '../services/lastfm/actions'
import * as spotifyActions from '../services/spotify/actions'
class Settings extends React.Component {
constructor(props){
super(props);
this.state = {
mopidy_host: this.props.mopidy.host,
mopidy_port: this.props.mopidy.port,
mopidy_library_artists_uri: this.props.mopidy.library_artists_uri,
mopidy_library_albums_uri: this.props.mopidy.library_albums_uri,
pusher_username: this.props.pusher.username,
input_in_focus: null
}
}
componentDidMount(){
this.props.uiActions.setWindowTitle("Settings");
}
componentWillReceiveProps(nextProps){
var changed = false
var state = this.state
if (nextProps.pusher.username != this.state.pusher_username && this.state.input_in_focus != 'pusher_username'){
state.pusher_username = nextProps.pusher.username
changed = true
}
if (changed){
this.setState(state)
}
}
resetAllSettings(){
localStorage.clear();
window.location = '#'
window.location.reload(true)
return false;
}
setConfig(e){
this.setState({input_in_focus: null});
e.preventDefault();
this.props.mopidyActions.set({
host: this.state.mopidy_host,
port: this.state.mopidy_port
});
window.location.reload(true);
return false;
}
handleBlur(service, name, value){
this.setState({input_in_focus: null});
var data = {};
data[name] = value;
this.props[service+'Actions'].set(data);
// Any per-field actions
switch (name){
case 'library_albums_uri':
this.props.mopidyActions.clearLibraryAlbums();
break;
case 'library_artists_uri':
this.props.mopidyActions.clearLibraryArtists();
break;
}
}
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 (
)
}
renderServerStatus(){
var colour = 'grey';
var icon = 'help';
var status = 'Unknown';
var className = null;
if (this.props.mopidy.connecting || this.props.pusher.connecting){
icon = 'autorenew';
status = 'Connecting...';
className = 'icon--spin';
} else if (!this.props.mopidy.connected || !this.props.pusher.connected){
colour = 'red';
icon = 'close';
status = 'Disconnected';
} else if (this.props.mopidy.connected && this.props.pusher.connected){
colour = 'green';
icon = 'check';
status = 'Connected';
}
return (
{status}
);
}
renderCommands(){
var commands = [];
if (this.props.pusher.commands){
for (var id in this.props.pusher.commands){
if (this.props.pusher.commands.hasOwnProperty(id)){
var command = Object.assign({}, this.props.pusher.commands[id]);
try {
command.command = JSON.parse(command.command);
} catch(error){
command.command = null;
}
commands.push(command);
}
}
}
if (commands.length <= 0){
return null;
}
return (
{
commands.map(command => {
return (
this.props.pusherActions.sendCommand(command.id, true)}>
{command.command && command.command.url ? command.command.url : "-"}
);
})
}
);
}
render(){
var options = (
hashHistory.push(global.baseURL+'settings/debug')}>
Debug
Help
)
if (this.props.mopidy.upgrading){
var upgrade_button = (
Upgrading...
);
} else if (this.props.pusher.version.upgrade_available){
var upgrade_button = this.props.pusherActions.upgrade()}>Upgrade to { this.props.pusher.version.latest } ;
} else {
var upgrade_button = null;
}
if (this.props.mopidy.local_scan_running){
var local_scan_button = (
Running scan...
);
} else {
var local_scan_button = this.props.pusherActions.runLocalScan()}>Run local scan ;
}
return (
Server
Status
{this.renderServerStatus()}
Services
Interface
Sources priority
Drag-and-drop to prioritize search providers and results
{helpers.isHosted() ? null : }
Commands
{this.renderCommands()}
Add new
Advanced
Version
{this.props.pusher.version.current} installed {this.props.pusher.version.upgrade_available ? Upgrade available : Up-to-date }
{local_scan_button}
Share configuration
{upgrade_button}
this.props.pusherActions.restart()}>{this.props.mopidy.restarting ? 'Restarting...' : 'Restart server'}
this.resetAllSettings()} />
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.
);
}
}
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),
lastfmActions: bindActionCreators(lastfmActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Settings)