import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { Route, Link } from 'react-router-dom'; import localForage from 'localforage'; 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 SelectField from '../components/Fields/SelectField'; import Header from '../components/Header'; import Icon from '../components/Icon'; import Services from '../components/Services'; import Servers from '../components/Servers'; 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'; import { isHosted } from '../util/helpers'; import { i18n, I18n, languagesAvailable } from '../locale'; import Button from '../components/Button'; 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() { const { uiActions: { setWindowTitle } } = this.props; setWindowTitle(i18n('settings.title')); } componentDidUpdate = () => { const { pusher: { username } } = this.props; const { pusher_username, input_in_focus } = this.state; let new_username = null; if (username && username !== pusher_username && input_in_focus !== 'pusher_username') { new_username = username; } if (new_username) this.setState({ pusher_username: new_username }); } onLanguageChange = (language) => { const { uiActions: { setLanguage } } = this.props; setLanguage(language); } onMopidySettingChanged = (name, value) => { const { mopidyActions: { set, }, } = this.props; const shortName = name.replace('mopidy_',''); set({ [shortName]: value }); this.setState({ [name]: value }); } resetAllSettings = () => { localForage.clear(() => { console.debug('Cleared settings, reloading...'); //window.location = '#'; //window.location.reload(true); }); return false; } resetStorage = () => { localForage.keys().then((keys) => { const keysToKeep = ['persist:root', 'persist:ui', 'persist:spotify']; const keysToRemove = keys.filter((key) => keysToKeep.indexOf(key) < 0); keysToRemove.forEach((key, index) => { localForage.removeItem(key).then(() => { console.debug(`Removed ${key}`); if (index === keysToRemove.length) { console.debug('Reloading...'); window.location = '#'; window.location.reload(true); } }); }); }); } resetServiceWorkerAndCache = () => { const { coreActions: { handleException } } = this.props; 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 { handleException(i18n('errors.no_service_worker')); } } doRestart = () => { const { pusherActions: { restart } } = this.props; restart(); } doUpgrade = () => { const { pusherActions: { upgrade } } = this.props; upgrade(); } doLocalScan = () => { const { pusherActions: { localScan } } = this.props; localScan(); } renderLocalScanButton = () => { const { ui: { processes }, } = this.props; const loading = processes.local_scan && processes.local_scan.status === 'running'; return ( ); } render = () => { const { mopidyActions: { set: setMopidy, }, mopidy, pusherActions: { setUsername, runCommand, setCommands, }, pusher, history, uiActions, ui, } = this.props; const { pusher_username, } = this.state; const options = ( ); return (

({ value: language.key, label: `${language.name} (${language.key})`, }))} value={ui.language} />
{isHosted() ? null : (
.
)}
runCommand(id, notify)} onChange={(commands) => setCommands(commands)} />

this.onMopidySettingChanged('mopidy_library_artists_uri', value)} autosave />
{pusher.version.current} {' '} {build} {pusher.version.upgrade_available ? ( {' '} ) : ( {' '} )}
{this.renderLocalScanButton()}
{pusher.version.upgrade_available && ( )}

Iris James Barnsley


); } } const mapStateToProps = (state, ownProps) => state; const mapDispatchToProps = (dispatch) => ({ 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);