Handle settings
This commit is contained in:
parent
8f64a349ec
commit
25a4d7be2c
7 changed files with 208 additions and 56 deletions
38
ui/src/lib/settings.js
Normal file
38
ui/src/lib/settings.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
export class Settings {
|
||||
constructor(res) {
|
||||
if (res) {
|
||||
this.update(res);
|
||||
}
|
||||
}
|
||||
|
||||
update({ language, gong_interval, weather_delay, weather_action }) {
|
||||
this.language = language;
|
||||
this.gong_interval = gong_interval;
|
||||
this.weather_delay = weather_delay;
|
||||
this.weather_action = weather_action;
|
||||
}
|
||||
|
||||
async save() {
|
||||
const res = await fetch(`api/settings`, {
|
||||
method: 'PUT',
|
||||
headers: {'Accept': 'application/json'},
|
||||
body: JSON.stringify(this),
|
||||
});
|
||||
if (res.status == 200) {
|
||||
const data = await res.json();
|
||||
this.update(data);
|
||||
return data;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSettings() {
|
||||
const res = await fetch(`api/settings`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new Settings(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue