Handle gongs
This commit is contained in:
parent
5c7841fdc6
commit
b2d50972ed
7 changed files with 338 additions and 50 deletions
64
ui/src/lib/gong.js
Normal file
64
ui/src/lib/gong.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
export class Gong {
|
||||
constructor(res) {
|
||||
if (res) {
|
||||
this.update(res);
|
||||
}
|
||||
}
|
||||
|
||||
update({ id, name, path, enabled }) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
async delete() {
|
||||
const res = await fetch(`api/gongs/${this.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {'Accept': 'application/json'}
|
||||
});
|
||||
if (res.status == 200) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
async setDefault() {
|
||||
this.enabled = !this.enabled;
|
||||
return await this.save();
|
||||
}
|
||||
|
||||
async save() {
|
||||
const res = await fetch(this.id?`api/gongs/${this.id}`:'api/gongs', {
|
||||
method: this.id?'PUT':'POST',
|
||||
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 getGongs() {
|
||||
const res = await fetch(`api/gongs`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return (await res.json()).map((g) => new Gong(g));
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGong(gid) {
|
||||
const res = await fetch(`api/gongs/${gid}`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new Gong(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue