dispatch/client/js/state/settings.js

128 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-10-15 06:56:17 +00:00
import assign from 'lodash/assign';
import createReducer from 'utils/createReducer';
import * as actions from './actions';
export const getSettings = state => state.settings;
2018-04-25 03:36:27 +00:00
export default createReducer(
{},
{
[actions.UPLOAD_CERT](state) {
state.uploadingCert = true;
},
2018-04-25 03:36:27 +00:00
[actions.socket.CERT_SUCCESS](state) {
state.uploadingCert = false;
delete state.certFile;
delete state.cert;
delete state.keyFile;
delete state.key;
},
2018-04-25 03:36:27 +00:00
[actions.socket.CERT_FAIL](state, action) {
state.uploadingCert = false;
state.certError = action.message;
},
2018-04-25 03:36:27 +00:00
[actions.SET_CERT_ERROR](state, action) {
state.uploadingCert = false;
state.certError = action.message;
},
2018-04-25 03:36:27 +00:00
[actions.SET_CERT](state, action) {
state.certFile = action.fileName;
state.cert = action.cert;
},
2018-04-25 03:36:27 +00:00
[actions.SET_KEY](state, action) {
state.keyFile = action.fileName;
state.key = action.key;
2018-10-15 06:56:17 +00:00
},
[actions.SETTINGS_SET](state, { key, value, settings }) {
if (settings) {
assign(state, settings);
} else {
state[key] = value;
}
2018-04-25 03:36:27 +00:00
}
}
2018-04-25 03:36:27 +00:00
);
export function setCertError(message) {
return {
type: actions.SET_CERT_ERROR,
message
};
}
export function uploadCert() {
return (dispatch, getState) => {
const { settings } = getState();
2018-04-25 03:36:27 +00:00
if (settings.cert && settings.key) {
dispatch({
type: actions.UPLOAD_CERT,
socket: {
type: 'cert',
data: {
2018-04-25 03:36:27 +00:00
cert: settings.cert,
key: settings.key
}
}
});
} else {
dispatch(setCertError('Missing certificate or key'));
}
};
}
export function setCert(fileName, cert) {
return {
type: actions.SET_CERT,
fileName,
2018-10-10 19:23:42 +00:00
cert: cert
};
}
export function setKey(fileName, key) {
return {
type: actions.SET_KEY,
fileName,
2018-10-10 19:23:42 +00:00
key: key
};
}
2018-10-15 06:56:17 +00:00
export function setSetting(key, value) {
return {
type: actions.SETTINGS_SET,
key,
value,
socket: {
type: 'settings_set',
data: {
[key]: value
},
debounce: {
delay: 250,
key: `settings:${key}`
}
}
};
}
export function setSettings(settings, local = false) {
const action = {
type: actions.SETTINGS_SET,
settings
};
if (!local) {
action.socket = {
type: 'settings_set',
data: settings
};
}
return action;
}