import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Route, Link } from 'react-router-dom';
import ConfirmationButton from '../components/Fields/ConfirmationButton';
import PusherConnectionList from '../components/PusherConnectionList';
import SourcesPriority from '../components/Fields/SourcesPriority';
import Commands from '../components/Fields/Commands';
import TextField from '../components/Fields/TextField';
import Header from '../components/Header';
import Icon from '../components/Icon';
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) {
let changed = false;
const { state } = this;
if (nextProps.pusher.username && 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;
}
resetServiceWorkerAndCache() {
if ('serviceWorker' in navigator) {
// Hose out all our caches
caches.keys().then(function(cacheNames) {
cacheNames.forEach(function(cacheName) {
caches.delete(cacheName);
});
});
// Unregister all service workers
// This forces our SW to bugger off and a new one is registered on refresh
navigator.serviceWorker.getRegistrations().then(
(registrations) => {
for (let registration of registrations) {
registration.unregister();
}
}
);
window.location = '#';
window.location.reload(true);
} else {
this.props.coreActions.handleException('Service Worker not supported');
}
}
handleBlur(service, name, value) {
this.setState({ input_in_focus: null });
const 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;
}
}
renderServerStatus() {
const {
mopidy,
pusher,
} = this.props;
let colour = 'grey';
let icon = 'help';
let status = 'Unknown';
let className = null;
if (mopidy.connecting || pusher.connecting) {
icon = 'autorenew';
status = 'Connecting...';
className = 'icon--spin';
} else if (!mopidy.connected || !pusher.connected) {
colour = 'red';
icon = 'close';
status = 'Disconnected';
} else if (mopidy.connected && pusher.connected) {
colour = 'green';
icon = 'check';
status = 'Connected';
}
return (