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

84 lines
3 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import LANGUAGES from 'constants/languages';
import * as SETTINGS from 'constants/settings';
function getLocalStorageSetting(setting, fallback) {
2017-12-13 22:36:30 +01:00
const localStorageVal = localStorage.getItem(`setting_${setting}`);
return localStorageVal === null ? fallback : JSON.parse(localStorageVal);
}
2017-06-06 06:21:55 +02:00
const reducers = {};
2017-06-28 09:12:01 +02:00
const defaultState = {
clientSettings: {
instantPurchaseEnabled: getLocalStorageSetting(SETTINGS.INSTANT_PURCHASE_ENABLED, false),
instantPurchaseMax: getLocalStorageSetting(SETTINGS.INSTANT_PURCHASE_MAX, {
currency: 'LBC',
amount: 0.1,
}),
showNsfw: getLocalStorageSetting(SETTINGS.SHOW_NSFW, false),
showUnavailable: getLocalStorageSetting(SETTINGS.SHOW_UNAVAILABLE, true),
welcome_acknowledged: getLocalStorageSetting(SETTINGS.NEW_USER_ACKNOWLEDGED, false),
email_collection_acknowledged: getLocalStorageSetting(SETTINGS.EMAIL_COLLECTION_ACKNOWLEDGED),
2017-12-13 22:36:30 +01:00
credit_required_acknowledged: false, // this needs to be re-acknowledged every run
language: getLocalStorageSetting(SETTINGS.LANGUAGE, 'en'),
theme: getLocalStorageSetting(SETTINGS.THEME, 'light'),
themes: getLocalStorageSetting(SETTINGS.THEMES, []),
2018-01-14 10:14:15 +01:00
automaticDarkModeEnabled: getLocalStorageSetting(SETTINGS.AUTOMATIC_DARK_MODE_ENABLED, false),
2018-05-30 05:18:41 +02:00
autoplay: getLocalStorageSetting(SETTINGS.AUTOPLAY, false),
2017-06-28 09:12:01 +02:00
},
isNight: false,
2017-08-19 20:03:51 +02:00
languages: {},
2018-03-26 23:32:43 +02:00
daemonSettings: {},
2017-06-28 09:12:01 +02:00
};
reducers[ACTIONS.DAEMON_SETTINGS_RECEIVED] = (state, action) =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
daemonSettings: action.data.settings,
});
reducers[ACTIONS.CLIENT_SETTING_CHANGED] = (state, action) => {
2017-06-28 09:12:01 +02:00
const { key, value } = action.data;
const clientSettings = Object.assign({}, state.clientSettings);
clientSettings[key] = value;
2017-12-13 22:36:30 +01:00
// this technically probably shouldn't happen here, and should be fixed when we're no longer using localStorage at all for persistent app state
localStorage.setItem(`setting_${key}`, JSON.stringify(value));
2017-06-28 09:12:01 +02:00
return Object.assign({}, state, {
clientSettings,
});
};
2018-01-14 10:14:15 +01:00
reducers[ACTIONS.UPDATE_IS_NIGHT] = (state, action) =>
Object.assign({}, state, {
isNight: action.data.isNight,
});
reducers[ACTIONS.DOWNLOAD_LANGUAGE_SUCCEEDED] = (state, action) => {
2017-08-19 20:03:51 +02:00
const languages = Object.assign({}, state.languages);
const { language } = action.data;
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
const langCode = language.substring(0, 2);
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
if (LANGUAGES[langCode]) {
languages[language] = `${LANGUAGES[langCode][0]} (${LANGUAGES[langCode][1]})`;
2017-08-19 20:03:51 +02:00
} else {
languages[langCode] = langCode;
}
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
return Object.assign({}, state, { languages });
};
2017-08-08 11:36:14 +02:00
reducers[ACTIONS.DOWNLOAD_LANGUAGE_FAILED] = (state, action) => {
2017-08-19 20:03:51 +02:00
const languages = Object.assign({}, state.languages);
delete languages[action.data.language];
return Object.assign({}, state, { languages });
2017-08-08 11:36:14 +02:00
};
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}