reveil/ui/src/lib/settings.js

40 lines
992 B
JavaScript
Raw Normal View History

2022-10-05 18:16:53 +00:00
export class Settings {
constructor(res) {
if (res) {
this.update(res);
}
}
2022-10-14 18:08:03 +00:00
update({ language, gong_interval, weather_delay, weather_action, max_run_time }) {
2022-10-05 18:16:53 +00:00
this.language = language;
this.gong_interval = gong_interval;
this.weather_delay = weather_delay;
this.weather_action = weather_action;
2022-10-14 18:08:03 +00:00
this.max_run_time = max_run_time;
2022-10-05 18:16:53 +00:00
}
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);
}
}