Make survey duplication synchronous (to avoid proposals randomization)
Some checks are pending
continuous-integration/drone/push Build is running

This commit is contained in:
nemunaire 2022-11-28 00:25:26 +01:00
parent 0cab193d6c
commit 8f5e2f380f
2 changed files with 36 additions and 23 deletions

View File

@ -34,10 +34,14 @@
}) })
} }
let duplicateInProgress = false;
function duplicateSurvey() { function duplicateSurvey() {
duplicateInProgress = true;
survey.duplicate().then((response) => { survey.duplicate().then((response) => {
duplicateInProgress = false;
goto(`surveys/${response.id}`); goto(`surveys/${response.id}`);
}).catch((error) => { }).catch((error) => {
duplicateInProgress = false;
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: error, msg: error,
}); });
@ -157,14 +161,19 @@
<div class="form-group row"> <div class="form-group row">
<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 || duplicateInProgress}
<button type="button" class="btn btn-danger" on:click={deleteSurvey} disabled={deleteInProgress}> <button type="button" class="btn btn-danger" on:click={deleteSurvey} disabled={deleteInProgress || duplicateInProgress}>
{#if deleteInProgress} {#if deleteInProgress}
<div class="spinner-border spinner-border-sm text-light me-1" role="status"></div> <div class="spinner-border spinner-border-sm text-light me-1" role="status"></div>
{/if} {/if}
Supprimer Supprimer
</button> </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} disabled={duplicateInProgress}>
{#if duplicateInProgress}
<div class="spinner-border spinner-border-sm text-dark me-1" role="status"></div>
{/if}
Dupliquer avec ces nouveaux paramètres
</button>
{/if} {/if}
</div> </div>
</div> </div>

View File

@ -105,31 +105,35 @@ export class Survey {
for (const q of questions) { for (const q of questions) {
const oldQuestionId = q.id; const oldQuestionId = q.id;
// This will create a new question with the same parameters
delete q.id; delete q.id;
// Also alter id_survey
q.id_survey = response.id; q.id_survey = response.id;
q.save().then((question) => {
q.id = oldQuestionId;
// Now recopy proposals // This save will create
if (q.kind == "mcq" || q.kind == "ucq") { const question = await q.save();
q.getProposals().then((proposals) => {
for (const p of proposals) { // Revert to the old question ID to perform the next retrievals
delete p.id; q.id = oldQuestionId;
p.id_question = question.id;
p.save(); // Now recopy proposals
} if (q.kind == "mcq" || q.kind == "ucq") {
}); const proposals = await q.getProposals();
for (const p of proposals) {
delete p.id;
p.id_question = question.id;
await p.save();
} }
}
// Now recopy correction templates // Now recopy correction templates
getCorrectionTemplates(oldQuestionId).then((cts) => { const cts = await getCorrectionTemplates(oldQuestionId);
for (const ct of cts) { for (const ct of cts) {
delete ct.id; delete ct.id;
ct.id_question = question.id; ct.id_question = question.id;
ct.save(); ct.save();
} }
});
});
} }
return response; return response;