diff --git a/client/source/sass/components/_torrents.scss b/client/source/sass/components/_torrents.scss
index 6cd76db5..3cc21bd4 100644
--- a/client/source/sass/components/_torrents.scss
+++ b/client/source/sass/components/_torrents.scss
@@ -167,6 +167,7 @@ $torrent--background--error: #e95779;
color: $torrent--secondary--foreground;
flex: 0 0 auto;
font-size: 0.75em;
+ font-weight: 500;
margin-right: $spacing-unit * 2/5;
text-align: right;
white-space: nowrap;
@@ -276,6 +277,20 @@ $torrent--background--error: #e95779;
&--speed {
+ &--download {
+
+ .is-actively-downloading & {
+ color: $green;
+ }
+ }
+
+ &--upload {
+
+ .is-actively-downloading & {
+ color: $blue;
+ }
+ }
+
&--download,
&--upload {
transition: color 0.25s;
@@ -292,14 +307,6 @@ $torrent--background--error: #e95779;
color: $torrent--tertiary--foreground--selected--stopped !important;
}
}
-
- &--download {
- color: $green;
- }
-
- &--upload {
- color: $blue;
- }
}
}
}
diff --git a/client/source/scripts/actions/SettingsActions.js b/client/source/scripts/actions/SettingsActions.js
index 08641f82..c478e559 100644
--- a/client/source/scripts/actions/SettingsActions.js
+++ b/client/source/scripts/actions/SettingsActions.js
@@ -4,8 +4,8 @@ import AppDispatcher from '../dispatcher/AppDispatcher';
import ActionTypes from '../constants/ActionTypes';
const SettingsActions = {
- fetchSettings: () => {
- return axios.get('/client/settings')
+ fetchSettings: (property) => {
+ return axios.get('/client/settings', {params: {property}})
.then((json = {}) => {
return json.data;
})
@@ -16,6 +16,7 @@ const SettingsActions = {
});
})
.catch((error) => {
+ console.trace(error);
AppDispatcher.dispatchServerAction({
type: ActionTypes.SETTINGS_FETCH_REQUEST_ERROR,
error
@@ -35,6 +36,7 @@ const SettingsActions = {
});
})
.catch((error) => {
+ console.error(error);
AppDispatcher.dispatchServerAction({
type: ActionTypes.SETTINGS_SAVE_REQUEST_ERROR,
error
diff --git a/client/source/scripts/actions/TorrentActions.js b/client/source/scripts/actions/TorrentActions.js
index c0e87868..1154033f 100644
--- a/client/source/scripts/actions/TorrentActions.js
+++ b/client/source/scripts/actions/TorrentActions.js
@@ -5,12 +5,6 @@ import ActionTypes from '../constants/ActionTypes';
const TorrentActions = {
addTorrentsByUrls: (options) => {
- axios.post('/ui/torrent-location', {
- destination: options.destination
- })
- .catch((error) => {
- console.log(error);
- });
return axios.post('/client/add', options)
.then((json = {}) => {
return json.data;
@@ -34,12 +28,6 @@ const TorrentActions = {
},
addTorrentsByFiles: (filesData, destination) => {
- axios.post('/ui/torrent-location', {
- destination
- })
- .catch((error) => {
- console.log(error);
- });
return axios.post('/client/add-files', filesData)
.then((json = {}) => {
return json.data;
@@ -81,25 +69,6 @@ const TorrentActions = {
});
},
- fetchLatestTorrentLocation: () => {
- return axios.get('/ui/torrent-location')
- .then((json = {}) => {
- return json.data;
- })
- .then((data) => {
- AppDispatcher.dispatchServerAction({
- type: ActionTypes.UI_LATEST_TORRENT_LOCATION_REQUEST_SUCCESS,
- data
- });
- })
- .catch((error) => {
- AppDispatcher.dispatchServerAction({
- type: ActionTypes.UI_LATEST_TORRENT_LOCATION_REQUEST_ERROR,
- error
- });
- });
- },
-
fetchTorrents: () => {
return axios.get('/client/torrents')
.then((json = {}) => {
diff --git a/client/source/scripts/app.js b/client/source/scripts/app.js
index f1cd3285..b8b0b2b9 100644
--- a/client/source/scripts/app.js
+++ b/client/source/scripts/app.js
@@ -6,17 +6,14 @@ import ApplicationContent from './components/layout/ApplicationContent';
import ApplicationLoadingIndicator from './components/layout/ApplicationLoadingIndicator';
import Modals from './components/modals/Modals';
import Sidebar from './components/panels/Sidebar';
+import SettingsStore from './stores/SettingsStore';
import TorrentActions from './actions/TorrentActions';
import TorrentDetailsView from './components/panels/TorrentDetailsView';
import TorrentListView from './components/panels/TorrentListView';
-import UIActions from './actions/UIActions';
class FloodApp extends React.Component {
componentDidMount() {
- TorrentActions.fetchLatestTorrentLocation();
- window.addEventListener('click', () => {
- UIActions.dismissContextMenu();
- });
+ SettingsStore.fetchSettings();
}
render() {
diff --git a/client/source/scripts/components/modals/AddTorrentsActions.js b/client/source/scripts/components/modals/AddTorrentsActions.js
index ca2e76c2..c6b769ab 100644
--- a/client/source/scripts/components/modals/AddTorrentsActions.js
+++ b/client/source/scripts/components/modals/AddTorrentsActions.js
@@ -2,8 +2,30 @@ import React from 'react';
import LoadingIndicatorDots from '../icons/LoadingIndicatorDots';
import ModalActions from './ModalActions';
+import SettingsStore from '../../stores/SettingsStore';
+
+const METHODS_TO_BIND = ['handleStartTorrentsToggle'];
export default class AddTorrents extends React.Component {
+ constructor() {
+ super();
+
+ this.state = {
+ startTorrentsOnLoad: true
+ };
+
+ METHODS_TO_BIND.forEach((method) => {
+ this[method] = this[method].bind(this);
+ });
+ }
+
+ componentWillMount() {
+ let startTorrentsOnLoad = SettingsStore.getSettings('startTorrentsOnLoad');
+ if (startTorrentsOnLoad !== true) {
+ this.setState({startTorrentsOnLoad: false});
+ }
+ }
+
getActions() {
let icon = null;
let primaryButtonText = 'Add Torrent';
@@ -15,8 +37,8 @@ export default class AddTorrents extends React.Component {
return [
{
- checked: true,
- clickHandler: this.props.onStartTorrentsToggle,
+ checked: this.state.startTorrentsOnLoad,
+ clickHandler: this.handleStartTorrentsToggle,
content: 'Start Torrent',
triggerDismiss: false,
type: 'checkbox'
@@ -42,6 +64,13 @@ export default class AddTorrents extends React.Component {
];
}
+ handleStartTorrentsToggle(value) {
+ SettingsStore.saveSettings({id: 'startTorrentsOnLoad', data: value});
+ if (!!this.props.onStartTorrentsToggle) {
+ this.props.onStartTorrentsToggle(value);
+ }
+ }
+
render() {
return (
diff --git a/client/source/scripts/components/modals/AddTorrentsByFile.js b/client/source/scripts/components/modals/AddTorrentsByFile.js
index 6d653837..eb38c259 100644
--- a/client/source/scripts/components/modals/AddTorrentsByFile.js
+++ b/client/source/scripts/components/modals/AddTorrentsByFile.js
@@ -8,13 +8,15 @@ import Close from '../icons/Close';
import File from '../icons/File';
import Files from '../icons/Files';
import ModalActions from './ModalActions';
+import SettingsStore from '../../stores/SettingsStore';
import TorrentActions from '../../actions/TorrentActions';
const METHODS_TO_BIND = [
'handleAddTorrents',
'handleDestinationChange',
'handleFileDrop',
- 'handleFileRemove'
+ 'handleFileRemove',
+ 'handleStartTorrentsToggle'
];
export default class AddTorrents extends React.Component {
@@ -104,6 +106,8 @@ export default class AddTorrents extends React.Component {
return;
}
+ SettingsStore.saveSettings({id: 'torrentDestination', data: this.state.destination});
+
this.setState({isAddingTorrents: true});
let fileData = new FormData();
@@ -117,6 +121,10 @@ export default class AddTorrents extends React.Component {
TorrentActions.addTorrentsByFiles(fileData, this.state.destination);
}
+ handleStartTorrentsToggle(value) {
+ this.setState({startTorrents: value});
+ }
+
handleDestinationChange(destination) {
this.setState({destination});
}
@@ -133,6 +141,7 @@ export default class AddTorrents extends React.Component {
);
diff --git a/client/source/scripts/components/modals/AddTorrentsByURL.js b/client/source/scripts/components/modals/AddTorrentsByURL.js
index c94ef29a..4d0f689c 100644
--- a/client/source/scripts/components/modals/AddTorrentsByURL.js
+++ b/client/source/scripts/components/modals/AddTorrentsByURL.js
@@ -2,6 +2,7 @@ import React from 'react';
import AddTorrentsActions from './AddTorrentsActions';
import AddTorrentsDestination from './AddTorrentsDestination';
+import SettingsStore from '../../stores/SettingsStore';
import TextboxRepeater from '../forms/TextboxRepeater';
import TorrentActions from '../../actions/TorrentActions';
@@ -39,6 +40,7 @@ export default class AddTorrents extends React.Component {
destination: this.state.destination,
start: this.state.startTorrents
});
+ SettingsStore.saveSettings({id: 'torrentDestination', data: this.state.destination});
}
handleDestinationChange(destination) {
diff --git a/client/source/scripts/components/modals/AddTorrentsDestination.js b/client/source/scripts/components/modals/AddTorrentsDestination.js
index 1a186a5f..4974ac95 100644
--- a/client/source/scripts/components/modals/AddTorrentsDestination.js
+++ b/client/source/scripts/components/modals/AddTorrentsDestination.js
@@ -2,12 +2,9 @@ import classnames from 'classnames';
import React from 'react';
import EventTypes from '../../constants/EventTypes';
-import UIStore from '../../stores/UIStore';
+import SettingsStore from '../../stores/SettingsStore';
-const METHODS_TO_BIND = [
- 'handleDestinationChange',
- 'onLatestTorrentLocationChange'
-];
+const METHODS_TO_BIND = ['handleDestinationChange'];
export default class AddTorrents extends React.Component {
constructor() {
@@ -23,22 +20,13 @@ export default class AddTorrents extends React.Component {
}
componentWillMount() {
- let destination = UIStore.getLatestTorrentLocation();
+ let destination = SettingsStore.getSettings('torrentDestination');
if (this.props.suggested) {
destination = this.props.suggested;
}
this.setState({destination});
}
- componentDidMount() {
- UIStore.listen(EventTypes.UI_LATEST_TORRENT_LOCATION_CHANGE, this.onLatestTorrentLocationChange);
- UIStore.fetchLatestTorrentLocation();
- }
-
- componentWillUnmount() {
- UIStore.unlisten(EventTypes.UI_LATEST_TORRENT_LOCATION_CHANGE, this.onLatestTorrentLocationChange);
- }
-
handleDestinationChange(event) {
let destination = event.target.value;
@@ -49,20 +37,6 @@ export default class AddTorrents extends React.Component {
this.setState({destination});
}
- onLatestTorrentLocationChange() {
- if (this.props.suggested) {
- return;
- }
-
- let destination = UIStore.getLatestTorrentLocation();
-
- if (this.props.onChange) {
- this.props.onChange(destination);
- }
-
- this.setState({destination});
- }
-
render() {
let textboxClasses = classnames('textbox', {
'is-fulfilled': this.state.destination && this.state.destination !== ''
diff --git a/client/source/scripts/components/modals/SettingsModal.js b/client/source/scripts/components/modals/SettingsModal.js
index 62fa4be6..d5865f5a 100644
--- a/client/source/scripts/components/modals/SettingsModal.js
+++ b/client/source/scripts/components/modals/SettingsModal.js
@@ -32,7 +32,7 @@ export default class AddTorrents extends React.Component {
componentDidMount() {
SettingsStore.listen(EventTypes.SETTINGS_FETCH_REQUEST_SUCCESS, this.handleSettingsFetchRequestSuccess);
- SettingsStore.fetchSettings(EventTypes.SETTINGS_FETCH_REQUEST_ERROR, this.handleSettingsFetchRequestError);
+ SettingsStore.fetchSettings('speedLimits');
}
componentWillUnmount() {
@@ -65,10 +65,17 @@ export default class AddTorrents extends React.Component {
}
handleSaveSettingsClick() {
- SettingsStore.saveSettings(this.state.settings);
+ let settingsToSave = Object.keys(this.state.settings).map((settingsKey) => {
+ return {
+ id: settingsKey,
+ data: this.state.settings[settingsKey]
+ };
+ });
+
+ SettingsStore.saveSettings(settingsToSave);
}
- handleSettingsFetchRequestError() {
+ handleSettingsFetchRequestError(error) {
console.log(error);
}
diff --git a/client/source/scripts/components/sidebar/SpeedLimitDropdown.js b/client/source/scripts/components/sidebar/SpeedLimitDropdown.js
index 25b8e769..117f5c2c 100644
--- a/client/source/scripts/components/sidebar/SpeedLimitDropdown.js
+++ b/client/source/scripts/components/sidebar/SpeedLimitDropdown.js
@@ -10,19 +10,14 @@ import SettingsStore from '../../stores/SettingsStore';
import TransferDataStore from '../../stores/TransferDataStore';
const METHODS_TO_BIND = ['handleSettingsFetchRequestSuccess', 'onTransferDataRequestSuccess'];
-const SPEEDS = [1024, 10240, 102400, 512000, 1048576, 2097152, 5242880, 10485760, 0];
+const DEFAULT_SPEEDS = [1024, 10240, 102400, 512000, 1048576, 2097152, 5242880, 10485760, 0];
class SpeedLimitDropdown extends React.Component {
constructor() {
super();
this.state = {
- settings: {
- speedLimits: {
- download: [],
- upload: []
- }
- },
+ speedLimits: {},
throttle: null
};
@@ -33,7 +28,7 @@ class SpeedLimitDropdown extends React.Component {
componentDidMount() {
SettingsStore.listen(EventTypes.SETTINGS_FETCH_REQUEST_SUCCESS, this.handleSettingsFetchRequestSuccess);
- SettingsStore.fetchSettings(EventTypes.SETTINGS_FETCH_REQUEST_ERROR, this.handleSettingsFetchRequestError);
+ SettingsStore.fetchSettings('speedLimits');
TransferDataStore.listen(
EventTypes.CLIENT_TRANSFER_DATA_REQUEST_SUCCESS,
this.onTransferDataRequestSuccess
@@ -87,8 +82,9 @@ class SpeedLimitDropdown extends React.Component {
let insertCurrentThrottle = true;
let currentThrottle = this.state.throttle;
+ let speeds = this.state.speedLimits[property] || DEFAULT_SPEEDS;
- let items = this.state.settings.speedLimits[property].map((bytes) => {
+ let items = speeds.map((bytes) => {
let selected = false;
bytes = Number(bytes);
@@ -113,7 +109,7 @@ class SpeedLimitDropdown extends React.Component {
if (insertCurrentThrottle && currentThrottle) {
// Find the position to insert the current throttle setting so that it
// remains sorted from lowest to highest.
- let insertionPoint = _.sortedIndex(SPEEDS, currentThrottle[property]);
+ let insertionPoint = _.sortedIndex(speeds, currentThrottle[property]);
items.splice(insertionPoint, 0, {
displayName: this.getHumanReadableSpeed(currentThrottle[property]),
@@ -138,9 +134,11 @@ class SpeedLimitDropdown extends React.Component {
}
handleSettingsFetchRequestSuccess() {
- this.setState({
- settings: SettingsStore.getSettings()
- });
+ let speedLimits = SettingsStore.getSettings('speedLimits');
+
+ if (!!speedLimits) {
+ this.setState({speedLimits});
+ }
}
render() {
diff --git a/client/source/scripts/components/sidebar/TransferData.js b/client/source/scripts/components/sidebar/TransferData.js
index 194085c7..44efc291 100644
--- a/client/source/scripts/components/sidebar/TransferData.js
+++ b/client/source/scripts/components/sidebar/TransferData.js
@@ -18,7 +18,7 @@ const METHODS_TO_BIND = [
'onTransferHistoryRequestSuccess'
];
-class ClientStats extends React.Component {
+class TransferData extends React.Component {
constructor() {
super();
@@ -165,8 +165,8 @@ class ClientStats extends React.Component {
}
-ClientStats.defaultProps = {
+TransferData.defaultProps = {
historyLength: 1
};
-export default ClientStats;
+export default TransferData;
diff --git a/client/source/scripts/components/torrent-list/Torrent.js b/client/source/scripts/components/torrent-list/Torrent.js
index fbc82776..5582e10d 100644
--- a/client/source/scripts/components/torrent-list/Torrent.js
+++ b/client/source/scripts/components/torrent-list/Torrent.js
@@ -52,7 +52,6 @@ export default class Torrent extends React.Component {
let downloadTotal = format.data(torrent.downloadTotal);
let eta = format.eta(torrent.eta);
let ratio = format.ratio(torrent.ratio);
- let secondaryDetails = [];
let totalSize = format.data(torrent.sizeBytes);
let uploadRate = format.data(torrent.uploadRate, '/s');
let uploadTotal = format.data(torrent.uploadTotal);
@@ -63,8 +62,23 @@ export default class Torrent extends React.Component {
let isActivelyDownloading = downloadRate.value > 0 || uploadRate.value > 0;
let wasAddedRecently = (Date.now() - added.getTime()) < 1000 * 60 * 10; // Was added in the last 10 minutes.
+ let secondaryDetails = [
+
+
+ {downloadRate.value}
+ {downloadRate.unit}
+ ,
+
+
+ {uploadRate.value}
+ {uploadRate.unit}
+
+ ];
+
if (isActivelyDownloading) {
- secondaryDetails.push(
+ secondaryDetails.unshift(
@@ -75,18 +89,7 @@ export default class Torrent extends React.Component {
if (isActivelyDownloading || wasAddedRecently) {
secondaryDetails.push(
-
-
- {downloadRate.value}
- {downloadRate.unit}
- ,
-
-
- {uploadRate.value}
- {uploadRate.unit}
-
+
);
}
diff --git a/client/source/scripts/components/ui/ContextMenu.js b/client/source/scripts/components/ui/ContextMenu.js
index a36d68b1..45b7d924 100644
--- a/client/source/scripts/components/ui/ContextMenu.js
+++ b/client/source/scripts/components/ui/ContextMenu.js
@@ -2,6 +2,8 @@ import classnames from 'classnames';
import React from 'react';
import ReactDOM from 'react-dom';
+import UIActions from '../../actions/UIActions';
+
export default class ContextMenu extends React.Component {
constructor() {
super();
@@ -13,6 +15,8 @@ export default class ContextMenu extends React.Component {
}
componentDidMount() {
+ document.addEventListener('click', UIActions.dismissContextMenu);
+
if (this.props.onMenuOpen) {
this.props.onMenuOpen();
}
@@ -25,6 +29,8 @@ export default class ContextMenu extends React.Component {
}
componentWillUnmount() {
+ document.removeEventListener('click', UIActions.dismissContextMenu);
+
if (this.props.onMenuClose) {
this.props.onMenuClose();
}
diff --git a/client/source/scripts/stores/SettingsStore.js b/client/source/scripts/stores/SettingsStore.js
index 47a1fdff..19424752 100644
--- a/client/source/scripts/stores/SettingsStore.js
+++ b/client/source/scripts/stores/SettingsStore.js
@@ -9,11 +9,15 @@ class SettingsStoreClass extends BaseStore {
super();
}
- fetchSettings() {
- SettingsActions.fetchSettings();
+ fetchSettings(property) {
+ SettingsActions.fetchSettings(property);
}
- getSettings() {
+ getSettings(property) {
+ if (property) {
+ return this.settings[property];
+ }
+
return this.settings;
}
@@ -27,6 +31,7 @@ class SettingsStoreClass extends BaseStore {
}
saveSettings(settings) {
+ this.settings[settings.id] = settings.data;
SettingsActions.saveSettings(settings);
}
}
diff --git a/client/source/scripts/stores/UIStore.js b/client/source/scripts/stores/UIStore.js
index fdabd517..054225e6 100644
--- a/client/source/scripts/stores/UIStore.js
+++ b/client/source/scripts/stores/UIStore.js
@@ -25,10 +25,6 @@ class UIStoreClass extends BaseStore {
}
}
- fetchLatestTorrentLocation() {
- TorrentActions.fetchLatestTorrentLocation();
- }
-
getActiveContextMenu() {
return this.activeContextMenu;
}
@@ -45,15 +41,6 @@ class UIStoreClass extends BaseStore {
return this.torrentDetailsHash;
}
- handleLatestTorrentLocationRequestSuccess(location) {
- this.latestTorrentLocation = location;
- this.emit(EventTypes.UI_LATEST_TORRENT_LOCATION_CHANGE);
- }
-
- handleLatestTorrentLocationRequestError(error) {
- console.log(error);
- }
-
handleTorrentClick(hash) {
this.torrentDetailsHash = hash;
this.emit(EventTypes.UI_TORRENT_DETAILS_HASH_CHANGE);
@@ -133,12 +120,6 @@ UIStore.dispatcherID = AppDispatcher.register((payload) => {
case ActionTypes.UI_DISPLAY_CONTEXT_MENU:
UIStore.setActiveContextMenu(action.data);
break;
- case ActionTypes.UI_LATEST_TORRENT_LOCATION_REQUEST_SUCCESS:
- UIStore.handleLatestTorrentLocationRequestSuccess(action.data.path);
- break;
- case ActionTypes.UI_LATEST_TORRENT_LOCATION_REQUEST_ERROR:
- UIStore.handleLatestTorrentLocationRequestError(action.error);
- break;
}
});
diff --git a/server/db/settings/settings.db b/server/db/settings/settings.db
index 37fd57e5..cc4bb526 100644
--- a/server/db/settings/settings.db
+++ b/server/db/settings/settings.db
@@ -1 +1,4 @@
-{"1":"","id":"settings","speedLimits":{"download":[1024,10240,102400,512000,1048576,5242880,10485760,0],"upload":[1024,10240,102400,512000,1048576,2097152,5242880,10485760,0]},"_id":"ZzqDjOcloseZX8mt"}
+{"id":"startTorrentsOnLoad","data":true,"_id":"FsReCALg9EWp5AZw"}
+{"id":"torrentDestination","data":"/Users/john/Music/Ween","_id":"jc4oGMCYe8jpyBYE"}
+{"id":"startTorrents","data":false,"_id":"kaW53bQDKgERpgXm"}
+{"id":"speedLimits","data":{"download":[1024,10240,102400,512000,1048576,2097152,5242880,10485760,0],"upload":[1024,10240,102400,512000,1048576,2097152,5242880,10485760,0]},"_id":"nyOO9DKRnfjktreF"}
diff --git a/server/models/settings.js b/server/models/settings.js
index 79f08aab..9ab39ef5 100644
--- a/server/models/settings.js
+++ b/server/models/settings.js
@@ -2,36 +2,56 @@
let Datastore = require('nedb');
-let client = require('./client');
let config = require('../../config');
-let HistoryEra = require('./HistoryEra');
let settingsDB = new Datastore({
autoload: true,
filename: `${config.dbPath}settings/settings.db`
});
-let history = {
+let settings = {
get: (opts, callback) => {
- settingsDB.find({id: 'settings'}).exec((err, docs) => {
- if (err) {
- callback(null, err);
- return;
- }
- callback(docs[0]);
- }
- );
- },
+ let query = {};
+ let foundSettings = {};
- set: (settings, callback) => {
- settingsDB.update({id: 'settings'}, {$set: settings}, {upsert: true}, (err, docs) => {
+ if (opts.property) {
+ query.id = opts.property;
+ }
+
+ settingsDB.find(query).exec((err, docs) => {
if (err) {
callback(null, err);
return;
}
- callback(docs);
+
+ docs.forEach((doc) => {
+ foundSettings[doc.id] = doc.data;
+ });
+ callback(foundSettings);
+ });
+ },
+
+ set: (payloads, callback) => {
+ let docsResponse = [];
+
+ if (!Array.isArray(payloads)) {
+ payloads = [payloads];
+ }
+
+ payloads.forEach((payload, index) => {
+ settingsDB.update({id: payload.id}, {$set: {data: payload.data}}, {upsert: true}, (err, docs) => {
+ docsResponse.push(docs);
+ if (index + 1 === payloads.length) {
+ if (err) {
+ callback(null, err);
+ return;
+ }
+ callback(docsResponse);
+ return;
+ }
+ });
});
}
}
-module.exports = history;
+module.exports = settings;
diff --git a/server/models/uiSettings.js b/server/models/uiSettings.js
index eecb73e2..cc870f81 100644
--- a/server/models/uiSettings.js
+++ b/server/models/uiSettings.js
@@ -11,57 +11,51 @@ let uiDB = new Datastore({
uiDB.persistence.setAutocompactionInterval(config.dbCleanInterval);
+let getDbResponseHandler = (callback) => {
+ return (error, docs) => {
+ if (error) {
+ callback(null, error);
+ return;
+ }
+
+ if (Array.isArray(docs)) {
+ callback(docs[0]);
+ return;
+ }
+
+ callback(docs);
+ return;
+ };
+};
+
let uiSettings = {
- getLatestTorrentLocation: function(callback) {
- uiDB.find({type: 'location'}, function(error, docs) {
- if (error) {
- callback(null, error);
- return;
- }
-
- if (docs.length) {
- callback(docs[0]);
- }
- });
+ get: (type, callback) => {
+ uiDB.find({type}, getDbResponseHandler(callback));
},
- getSortProps: function(callback) {
- uiDB.find({type: 'sort'}, function(error, docs) {
- if (error) {
- callback(null, error);
- return;
- }
-
- if (docs.length) {
- callback(docs[0]);
- }
- });
+ set: (payload, callback) => {
+ let newLocationData = Object.assign({}, {type: payload.type}, {data: payload.data});
+ uiDB.update({type: payload.type, data: payload.data}, getDbResponseHandler(callback));
},
- setLatestTorrentLocation: function(data, callback) {
+ getLatestTorrentLocation: (callback) => {
+ try {
+ uiDB.find({type: 'location'}, getDbResponseHandler(callback));
+ } catch (e) { console.log(e); }
+ },
+
+ getSortProps: (callback) => {
+ uiDB.find({type: 'sort'}, getDbResponseHandler(callback));
+ },
+
+ setLatestTorrentLocation: (data, callback) => {
let newLocationData = Object.assign({}, {type: 'location'}, {path: data.destination});
- uiDB.update({type: 'location'}, newLocationData, {upsert: true}, function (error, docs) {
- if (error) {
- callback(null, error);
- return;
- }
-
- callback(docs);
- });
+ uiDB.update({type: 'location'}, newLocationData, {upsert: true}, getDbResponseHandler(callback));
},
- setSortProps: function(sortProps, callback) {
+ setSortProps: (sortProps, callback) => {
let newSortPropData = Object.assign({}, {type: 'sort'}, sortProps);
- uiDB.update({type: 'sort'}, newSortPropData, {upsert: true}, function (error, docs) {
- if (error) {
- callback(null, error);
- return;
- }
-
- if (docs.length) {
- callback(docs);
- }
- });
+ uiDB.update({type: 'sort'}, newSortPropData, {upsert: true}, getDbResponseHandler(callback));
}
}
diff --git a/server/routes/ui.js b/server/routes/ui.js
index 0c13cbf1..632eade0 100644
--- a/server/routes/ui.js
+++ b/server/routes/ui.js
@@ -9,6 +9,14 @@ let client = require('../models/client');
let history = require('../models/history');
let uiSettings = require('../models/uiSettings');
+router.get('/settings', (req, res, next) => {
+ uiSettings.get(req.body.type, ajaxUtil.getResponseFn(res));
+});
+
+router.post('/settings', (req, res, next) => {
+ uiSettings.set(req.body, ajaxUtil.getResponseFn(res));
+});
+
router.post('/sort-props', (req, res, next) => {
uiSettings.setSortProps(req.body, ajaxUtil.getResponseFn(res));
});
diff --git a/server/util/ajaxUtil.js b/server/util/ajaxUtil.js
index cc22b8e0..b8ae7546 100644
--- a/server/util/ajaxUtil.js
+++ b/server/util/ajaxUtil.js
@@ -4,7 +4,7 @@ let ajaxUtil = {
getResponseFn: (res) => {
return (data, error) => {
if (error) {
- console.trace(error);
+ console.log(error);
res.status(500).json(error);
return;
} else {