Handle tracks
This commit is contained in:
parent
5799eb32ef
commit
5c7841fdc6
7 changed files with 343 additions and 52 deletions
65
ui/src/lib/track.js
Normal file
65
ui/src/lib/track.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
export class Track {
|
||||
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/tracks/${this.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {'Accept': 'application/json'}
|
||||
});
|
||||
if (res.status == 200) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
toggleEnable() {
|
||||
this.enabled = !this.enabled;
|
||||
this.save();
|
||||
return this;
|
||||
}
|
||||
|
||||
async save() {
|
||||
const res = await fetch(this.id?`api/tracks/${this.id}`:'api/tracks', {
|
||||
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 getTracks() {
|
||||
const res = await fetch(`api/tracks`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return (await res.json()).map((t) => new Track(t));
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTrack(tid) {
|
||||
const res = await fetch(`api/tracks/${tid}`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new Track(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue