Smarter survey and question deletion/duplucation

This commit is contained in:
nemunaire 2022-09-16 10:55:51 +02:00
parent aeebddd191
commit 15bff5af96
4 changed files with 54 additions and 8 deletions

View File

@ -13,17 +13,21 @@
dispatch('saved', response); dispatch('saved', response);
}, (error) => { }, (error) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }
let deleteInProgress = false;
function deleteSurvey() { function deleteSurvey() {
deleteInProgress = true;
survey.delete().then((response) => { survey.delete().then((response) => {
deleteInProgress = false;
goto(`surveys`); goto(`surveys`);
}, (error) => { }, (error) => {
deleteInProgress = false;
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }
@ -33,7 +37,7 @@
goto(`surveys/${response.id}`); goto(`surveys/${response.id}`);
}).catch((error) => { }).catch((error) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }
@ -137,7 +141,12 @@
<div class="col-sm-10"> <div class="col-sm-10">
<button type="submit" class="btn btn-primary">Enregistrer</button> <button type="submit" class="btn btn-primary">Enregistrer</button>
{#if survey.id} {#if survey.id}
<button type="button" class="btn btn-danger" on:click={deleteSurvey}>Supprimer</button> <button type="button" class="btn btn-danger" on:click={deleteSurvey} disabled={deleteInProgress}>
{#if deleteInProgress}
<div class="spinner-border spinner-border-sm text-light me-1" role="status"></div>
{/if}
Supprimer
</button>
<button type="button" class="btn btn-secondary" on:click={duplicateSurvey}>Dupliquer avec ces nouveaux paramètres</button> <button type="button" class="btn btn-secondary" on:click={duplicateSurvey}>Dupliquer avec ces nouveaux paramètres</button>
{/if} {/if}
</div> </div>

View File

@ -12,7 +12,7 @@
dispatch('saved', response); dispatch('saved', response);
}, (error) => { }, (error) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }
@ -22,7 +22,7 @@
goto(`works`); goto(`works`);
}, (error) => { }, (error) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }
@ -32,7 +32,7 @@
goto(`works/${response.id}`); goto(`works/${response.id}`);
}).catch((error) => { }).catch((error) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error.errmsg, msg: error,
}); });
}) })
} }

View File

@ -120,6 +120,12 @@ export class Question {
async delete() { async delete() {
if (this.id) { if (this.id) {
// Start by deleting proposals
const proposals = await this.getProposals();
for (const p of proposals) {
await p.delete();
}
const res = await fetch(`api/questions/${this.id}`, { const res = await fetch(`api/questions/${this.id}`, {
method: 'DELETE', method: 'DELETE',
headers: {'Accept': 'application/json'}, headers: {'Accept': 'application/json'},

View File

@ -1,3 +1,4 @@
import { getCorrectionTemplates } from './correctionTemplates';
import { getQuestions } from './questions'; import { getQuestions } from './questions';
import { Response } from './response'; import { Response } from './response';
import { Work } from './works'; import { Work } from './works';
@ -101,9 +102,33 @@ export class Survey {
// Now recopy questions // Now recopy questions
const questions = await getQuestions(oldSurveyId); const questions = await getQuestions(oldSurveyId);
for (const q of questions) { for (const q of questions) {
const oldQuestionId = q.id;
delete q.id; delete q.id;
q.id_survey = response.id; q.id_survey = response.id;
q.save(); q.save().then((question) => {
q.id = oldQuestionId;
// Now recopy proposals
if (q.kind == "mcq" || q.kind == "ucq") {
q.getProposals().then((proposals) => {
for (const p of proposals) {
delete p.id;
p.id_question = question.id;
p.save();
}
});
}
// Now recopy correction templates
getCorrectionTemplates(oldQuestionId).then((cts) => {
for (const ct of cts) {
delete ct.id;
ct.id_question = question.id;
ct.save();
}
});
});
} }
return response; return response;
@ -115,6 +140,12 @@ export class Survey {
async delete() { async delete() {
if (this.id) { if (this.id) {
// Start by deleting questions
const questions = await getQuestions(this.id);
for (const q of questions) {
await q.delete();
}
const res = await fetch(`api/surveys/${this.id}`, { const res = await fetch(`api/surveys/${this.id}`, {
method: 'DELETE', method: 'DELETE',
headers: {'Accept': 'application/json'}, headers: {'Accept': 'application/json'},