ui: Working on works
This commit is contained in:
parent
b9acaa798b
commit
197c23736d
12 changed files with 475 additions and 10 deletions
108
ui/src/lib/works.js
Normal file
108
ui/src/lib/works.js
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
export class Work {
|
||||
constructor(res) {
|
||||
this.kind = "w";
|
||||
if (res) {
|
||||
this.update(res);
|
||||
}
|
||||
}
|
||||
|
||||
update({ id, title, promo, group, shown, submission_url, corrected, start_availability, end_availability }) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.promo = promo;
|
||||
this.group = group;
|
||||
this.shown = shown;
|
||||
this.submission_url = submission_url;
|
||||
this.corrected = corrected;
|
||||
if (this.start_availability != start_availability) {
|
||||
this.start_availability = start_availability;
|
||||
delete this.__start_availability;
|
||||
}
|
||||
if (this.end_availability != end_availability) {
|
||||
this.end_availability = end_availability;
|
||||
delete this.__end_availability;
|
||||
}
|
||||
}
|
||||
|
||||
startAvailability() {
|
||||
if (!this.__start_availability) {
|
||||
this.__start_availability = new Date(this.start_availability)
|
||||
}
|
||||
return this.__start_availability
|
||||
}
|
||||
|
||||
endAvailability() {
|
||||
if (!this.__end_availability) {
|
||||
this.__end_availability = new Date(this.end_availability)
|
||||
}
|
||||
return this.__end_availability
|
||||
}
|
||||
|
||||
isFinished() {
|
||||
return this.endAvailability() < new Date();
|
||||
}
|
||||
|
||||
async save() {
|
||||
const res = await fetch(this.id?`api/works/${this.id}`:'api/works', {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
async duplicate() {
|
||||
if (this.id) {
|
||||
const oldSurveyId = this.id;
|
||||
delete this.id;
|
||||
const res = await fetch(`api/works`, {
|
||||
method: 'POST',
|
||||
headers: {'Accept': 'application/json'},
|
||||
body: JSON.stringify(this),
|
||||
});
|
||||
if (res.status == 200) {
|
||||
return await res.json();
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async delete() {
|
||||
if (this.id) {
|
||||
const res = await fetch(`api/works/${this.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {'Accept': 'application/json'},
|
||||
});
|
||||
if (res.status == 200) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getWorks() {
|
||||
const res = await fetch(`api/works`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return (await res.json()).map((s) => new Work(s));
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getWork(wid) {
|
||||
const res = await fetch(`api/works/${wid}`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new Work(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
Reference in a new issue