2019-11-12 03:34:37 +00:00
|
|
|
import Api from "common/api";
|
|
|
|
|
|
|
|
class Settings {
|
|
|
|
constructor(values) {
|
|
|
|
this.__originalValues = {};
|
|
|
|
|
2019-11-17 02:08:13 +00:00
|
|
|
if (!values) {
|
2019-12-03 12:08:48 +00:00
|
|
|
throw "can't create settings with empty values";
|
2019-11-12 03:34:37 +00:00
|
|
|
}
|
2019-11-17 02:08:13 +00:00
|
|
|
|
|
|
|
this.setValues(values);
|
2019-11-12 03:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setValues(values) {
|
|
|
|
if(!values) return;
|
|
|
|
|
|
|
|
for(let key in values) {
|
|
|
|
if(values.hasOwnProperty(key) && key !== "__originalValues") {
|
|
|
|
this[key] = values[key];
|
|
|
|
this.__originalValues[key] = values[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getValues() {
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
for(let key in this.__originalValues) {
|
|
|
|
if(this.__originalValues.hasOwnProperty(key) && key !== "__originalValues") {
|
|
|
|
result[key] = this[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
|
|
|
return Api.get("settings").then((response) => {
|
|
|
|
return Promise.resolve(this.setValues(response.data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
return Api.post("settings", this.getValues()).then((response) => Promise.resolve(this.setValues(response.data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Settings;
|