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);
}, (error) => {
ToastsStore.addErrorToast({
msg: error.errmsg,
msg: error,
});
})
}
let deleteInProgress = false;
function deleteSurvey() {
deleteInProgress = true;
survey.delete().then((response) => {
deleteInProgress = false;
goto(`surveys`);
}, (error) => {
deleteInProgress = false;
ToastsStore.addErrorToast({
msg: error.errmsg,
msg: error,
});
})
}
@ -33,7 +37,7 @@
goto(`surveys/${response.id}`);
}).catch((error) => {
ToastsStore.addErrorToast({
msg: error.errmsg,
msg: error,
});
})
}
@ -137,7 +141,12 @@
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Enregistrer</button>
{#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>
{/if}
</div>

View File

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

View File

@ -120,6 +120,12 @@ export class Question {
async delete() {
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}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'},

View File

@ -1,3 +1,4 @@
import { getCorrectionTemplates } from './correctionTemplates';
import { getQuestions } from './questions';
import { Response } from './response';
import { Work } from './works';
@ -101,9 +102,33 @@ export class Survey {
// Now recopy questions
const questions = await getQuestions(oldSurveyId);
for (const q of questions) {
const oldQuestionId = q.id;
delete q.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;
@ -115,6 +140,12 @@ export class Survey {
async delete() {
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}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'},