136 lines
3.5 KiB
JavaScript
136 lines
3.5 KiB
JavaScript
import { Response } from './response';
|
|
|
|
export class QuestionProposal {
|
|
constructor(res) {
|
|
if (res) {
|
|
this.update(res);
|
|
}
|
|
}
|
|
|
|
update({ id, id_question, label }) {
|
|
this.id = id;
|
|
this.id_question = id_question;
|
|
this.label = label;
|
|
if (this.changed !== undefined)
|
|
delete this.changed;
|
|
}
|
|
|
|
async save() {
|
|
const res = await fetch(this.id?`api/questions/${this.id_question}/proposals/${this.id}`:`api/questions/${this.id_question}/proposals`, {
|
|
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 delete() {
|
|
if (this.id) {
|
|
const res = await fetch(`api/questions/${this.id_question}/proposals/${this.id}`, {
|
|
method: 'DELETE',
|
|
headers: {'Accept': 'application/json'},
|
|
});
|
|
if (res.status == 200) {
|
|
return true;
|
|
} else {
|
|
throw new Error((await res.json()).errmsg);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export class Question {
|
|
constructor(res) {
|
|
if (res) {
|
|
this.update(res);
|
|
}
|
|
}
|
|
|
|
update({ id, id_survey, title, description, desc_raw, placeholder, kind }) {
|
|
this.id = id;
|
|
this.id_survey = id_survey;
|
|
this.title = title;
|
|
this.description = description;
|
|
this.desc_raw = desc_raw;
|
|
this.placeholder = placeholder;
|
|
this.kind = kind;
|
|
}
|
|
|
|
async getProposals() {
|
|
const res = await fetch(`api/questions/${this.id}/proposals`, {
|
|
method: 'GET',
|
|
headers: {'Accept': 'application/json'},
|
|
});
|
|
if (res.status == 200) {
|
|
return (await res.json()).map((p) => new QuestionProposal(p))
|
|
} else {
|
|
throw new Error((await res.json()).errmsg);
|
|
}
|
|
}
|
|
|
|
async getResponses() {
|
|
const res = await fetch(`api/surveys/${this.id_survey}/questions/${this.id}/responses`, {
|
|
method: 'GET',
|
|
headers: {'Accept': 'application/json'},
|
|
});
|
|
if (res.status == 200) {
|
|
return (await res.json()).map((r) => new Response(r))
|
|
} else {
|
|
throw new Error((await res.json()).errmsg);
|
|
}
|
|
}
|
|
|
|
async save() {
|
|
const res = await fetch(this.id?`api/questions/${this.id}`:`api/surveys/${this.id_survey}/questions`, {
|
|
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 delete() {
|
|
if (this.id) {
|
|
const res = await fetch(`api/questions/${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 getQuestion(qid) {
|
|
const res = await fetch(`api/questions/${qid}`, {headers: {'Accept': 'application/json'}})
|
|
if (res.status == 200) {
|
|
return new Question(await res.json());
|
|
} else {
|
|
throw new Error((await res.json()).errmsg);
|
|
}
|
|
}
|
|
|
|
export async function getQuestions(sid) {
|
|
const res = await fetch(`api/surveys/${sid}/questions`, {headers: {'Accept': 'application/json'}})
|
|
if (res.status == 200) {
|
|
return (await res.json()).map((e) => new Question(e))
|
|
} else {
|
|
throw new Error((await res.json()).errmsg);
|
|
}
|
|
}
|