lbry-desktop/src/renderer/redux/actions/settings.js

143 lines
3.4 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import * as SETTINGS from 'constants/settings';
2017-08-24 20:28:05 +02:00
import Lbry from 'lbry';
import Fs from 'fs';
import Http from 'http';
export function doFetchDaemonSettings() {
return function(dispatch) {
Lbry.settings_get().then(settings => {
dispatch({
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
}
export function doSetDaemonSetting(key, value) {
return function(dispatch) {
2017-12-13 22:36:30 +01:00
const settings = {};
settings[key] = value;
Lbry.settings_set(settings).then(settings);
Lbry.settings_get().then(remoteSettings => {
dispatch({
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
data: {
remoteSettings,
2017-06-06 23:19:12 +02:00
},
});
});
};
2017-06-06 06:21:55 +02:00
}
2017-06-28 09:12:01 +02:00
export function doSetClientSetting(key, value) {
return {
type: ACTIONS.CLIENT_SETTING_CHANGED,
2017-06-28 09:12:01 +02:00
data: {
key,
value,
},
};
}
2017-08-08 11:36:14 +02:00
2017-08-24 18:11:39 +02:00
export function doGetThemes() {
return function(dispatch) {
const themes = ['light', 'dark'];
dispatch(doSetClientSetting(SETTINGS.THEMES, themes));
2017-08-26 21:04:44 +02:00
};
2017-08-24 20:28:05 +02:00
}
2017-08-26 21:04:44 +02:00
2017-08-19 20:03:51 +02:00
export function doDownloadLanguage(langFile) {
return function(dispatch) {
2017-12-13 22:36:30 +01:00
const destinationPath = `${app.i18n.directory}/${langFile}`;
const language = langFile.replace('.json', '');
const errorHandler = () => {
Fs.unlink(destinationPath, () => {}); // Delete the file async. (But we don't check the result)
dispatch({
type: ACTIONS.DOWNLOAD_LANGUAGE_FAILED,
data: { language },
});
};
const req = Http.get(
2017-08-19 20:03:51 +02:00
{
headers: {
'Content-Type': 'text/html',
2017-08-19 20:03:51 +02:00
},
host: 'i18n.lbry.io',
2017-08-19 20:03:51 +02:00
path: `/langs/${langFile}`,
},
response => {
if (response.statusCode === 200) {
const file = Fs.createWriteStream(destinationPath);
2017-08-19 20:03:51 +02:00
file.on('error', errorHandler);
file.on('finish', () => {
2017-08-08 11:36:14 +02:00
file.close();
// push to our local list
dispatch({
type: ACTIONS.DOWNLOAD_LANGUAGE_SUCCEEDED,
2017-12-13 22:36:30 +01:00
data: { language },
2017-08-08 11:36:14 +02:00
});
});
response.pipe(file);
2017-08-19 20:03:51 +02:00
} else {
errorHandler(new Error('Language request failed.'));
2017-08-08 11:36:14 +02:00
}
2017-08-19 20:03:51 +02:00
}
);
2017-08-08 11:36:14 +02:00
2017-12-13 22:36:30 +01:00
req.setTimeout(30000, () => {
2017-08-19 20:03:51 +02:00
req.abort();
2017-08-08 11:36:14 +02:00
});
2017-08-19 20:03:51 +02:00
req.on('error', errorHandler);
2017-08-19 20:03:51 +02:00
req.end();
2017-08-08 11:36:14 +02:00
};
}
export function doDownloadLanguages() {
return function() {
2017-12-13 22:36:30 +01:00
// temporarily disable i18n so I can get a working build out -- Jeremy
// if (!Fs.existsSync(app.i18n.directory)) {
// Fs.mkdirSync(app.i18n.directory);
// }
//
// function checkStatus(response) {
// if (response.status >= 200 && response.status < 300) {
// return response;
// }
// throw new Error(
// __("The list of available languages could not be retrieved.")
// );
// }
//
// function parseJSON(response) {
// return response.json();
// }
//
// return fetch("http://i18n.lbry.io")
// .then(checkStatus)
// .then(parseJSON)
// .then(files => {
// const actions = files.map(doDownloadLanguage);
// dispatch(batchActions(...actions));
// });
2017-08-19 20:03:51 +02:00
};
}
export function doChangeLanguage(language) {
return function(dispatch) {
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, language));
2017-08-19 20:03:51 +02:00
app.i18n.setLocale(language);
2017-08-08 11:36:14 +02:00
};
}