|
198 | 198 | " |
199 | 199 | @update:options="updateQuestionOptions(index, $event)" |
200 | 200 | @clone="cloneQuestion(question, index)" |
201 | | - @delete="deleteQuestion(question.id)" |
| 201 | + @delete="onDelete(question.id)" |
202 | 202 | @moveDown="onMoveDown(index)" |
203 | 203 | @moveUp="onMoveUp(index)"> |
204 | 204 | <template |
@@ -1318,65 +1318,141 @@ export default defineComponent({ |
1318 | 1318 | } |
1319 | 1319 |
|
1320 | 1320 | /** |
1321 | | - * Delete a question |
| 1321 | + * Delete questions one after the other, one request each, and warn once about the jumps this changes; a failed request keeps the remaining questions. Deleting the first section moves the next section up to its place, as the server does |
1322 | 1322 | * |
1323 | | - * @param questionId the question id to delete |
| 1323 | + * @param questionIds the ids of the questions to delete |
1324 | 1324 | */ |
1325 | | - const deleteQuestion = async (questionId: number): Promise<void> => { |
1326 | | - if (deletingQuestionIds.has(questionId)) { |
| 1325 | + const deleteQuestions = async (questionIds: number[]): Promise<void> => { |
| 1326 | + const ids = questionIds.filter((id) => !deletingQuestionIds.has(id)) |
| 1327 | + if (ids.length === 0) { |
1327 | 1328 | return |
1328 | 1329 | } |
1329 | | - deletingQuestionIds.add(questionId) |
| 1330 | + ids.forEach((id) => deletingQuestionIds.add(id)) |
1330 | 1331 | const formId = props.form.id |
| 1332 | + const oldQuestions = props.form.questions |
1331 | 1333 | isLoadingQuestions.value = true |
1332 | 1334 | startChangingQuestions(formId) |
1333 | 1335 |
|
| 1336 | + const deleted = new Set<number>() |
1334 | 1337 | try { |
1335 | | - await queueQuestionRequest( |
1336 | | - props.form.questions.map(({ id }) => id), |
1337 | | - () => |
1338 | | - axios.delete( |
1339 | | - generateOcsUrl( |
1340 | | - 'apps/forms/api/v3/forms/{id}/questions/{questionId}', |
1341 | | - { |
1342 | | - id: props.form.id, |
1343 | | - questionId, |
1344 | | - }, |
| 1338 | + for (const questionId of ids) { |
| 1339 | + await queueQuestionRequest( |
| 1340 | + props.form.questions.map(({ id }) => id), |
| 1341 | + () => |
| 1342 | + axios.delete( |
| 1343 | + generateOcsUrl( |
| 1344 | + 'apps/forms/api/v3/forms/{id}/questions/{questionId}', |
| 1345 | + { |
| 1346 | + id: props.form.id, |
| 1347 | + questionId, |
| 1348 | + }, |
| 1349 | + ), |
1345 | 1350 | ), |
1346 | | - ), |
1347 | | - ) |
1348 | | - if (props.form.id !== formId) { |
1349 | | - return |
| 1351 | + ) |
| 1352 | + deleted.add(questionId) |
1350 | 1353 | } |
1351 | | - const position = props.form.questions.findIndex( |
1352 | | - ({ id }) => id === questionId, |
1353 | | - ) |
1354 | | - const remaining = props.form.questions.filter( |
1355 | | - (search: FormsQuestion) => search.id !== questionId, |
1356 | | - ) |
1357 | | - const reordered = |
1358 | | - props.form.questions[position]?.type === QUESTION_TYPE_SECTION |
1359 | | - ? withNextSectionMovedUp(remaining, position) |
1360 | | - : remaining |
1361 | | - const questions = |
1362 | | - reordered === remaining |
1363 | | - ? remaining |
1364 | | - : withoutDisallowedJumps(reordered) |
1365 | | - warnAboutJumpChanges(props.form.questions, questions) |
1366 | | - emit('update:form', { ...props.form, questions }) |
1367 | | - emitEvent('forms:last-updated:set', props.form.id) |
1368 | 1354 | } catch (error) { |
1369 | | - logger.error(`Error while removing question ${questionId}`, { |
1370 | | - error, |
1371 | | - }) |
| 1355 | + logger.error('Error while removing questions', { error, ids }) |
1372 | 1356 | showError( |
1373 | 1357 | t('forms', 'There was an error while removing the question'), |
1374 | 1358 | ) |
1375 | 1359 | } finally { |
| 1360 | + if (props.form.id === formId && deleted.size > 0) { |
| 1361 | + let questions = props.form.questions |
| 1362 | + let moved = false |
| 1363 | + for (const id of deleted) { |
| 1364 | + const position = questions.findIndex( |
| 1365 | + (question) => question.id === id, |
| 1366 | + ) |
| 1367 | + const remaining = questions.filter( |
| 1368 | + (question) => question.id !== id, |
| 1369 | + ) |
| 1370 | + const next = |
| 1371 | + questions[position]?.type === QUESTION_TYPE_SECTION |
| 1372 | + ? withNextSectionMovedUp(remaining, position) |
| 1373 | + : remaining |
| 1374 | + moved = moved || next !== remaining |
| 1375 | + questions = next |
| 1376 | + } |
| 1377 | + if (moved) { |
| 1378 | + questions = withoutDisallowedJumps(questions) |
| 1379 | + } |
| 1380 | + warnAboutJumpChanges(oldQuestions, questions) |
| 1381 | + emit('update:form', { ...props.form, questions }) |
| 1382 | + emitEvent('forms:last-updated:set', props.form.id) |
| 1383 | + } |
1376 | 1384 | isLoadingQuestions.value = false |
1377 | 1385 | endChangingQuestions(formId) |
1378 | | - deletingQuestionIds.delete(questionId) |
| 1386 | + ids.forEach((id) => deletingQuestionIds.delete(id)) |
| 1387 | + } |
| 1388 | + } |
| 1389 | +
|
| 1390 | + /** |
| 1391 | + * Delete a question; for a section with questions, ask whether to delete only the section, so its questions join the section before it, the next section or, for the only section, the form without sections, or the section with its questions, deleting the section last |
| 1392 | + * |
| 1393 | + * @param questionId the id of the question or section |
| 1394 | + */ |
| 1395 | + const onDelete = async (questionId: number): Promise<void> => { |
| 1396 | + const position = props.form.questions.findIndex( |
| 1397 | + ({ id }) => id === questionId, |
| 1398 | + ) |
| 1399 | + if (props.form.questions[position]?.type !== QUESTION_TYPE_SECTION) { |
| 1400 | + await deleteQuestions([questionId]) |
| 1401 | + return |
1379 | 1402 | } |
| 1403 | + const end = props.form.questions.findIndex( |
| 1404 | + (question, index) => |
| 1405 | + index > position && question.type === QUESTION_TYPE_SECTION, |
| 1406 | + ) |
| 1407 | + const sectionQuestions = props.form.questions.slice( |
| 1408 | + position + 1, |
| 1409 | + end === -1 ? undefined : end, |
| 1410 | + ) |
| 1411 | + if (sectionQuestions.length === 0) { |
| 1412 | + await deleteQuestions([questionId]) |
| 1413 | + return |
| 1414 | + } |
| 1415 | + const isFirst = !props.form.questions |
| 1416 | + .slice(0, position) |
| 1417 | + .some((question) => question.type === QUESTION_TYPE_SECTION) |
| 1418 | + let text = n( |
| 1419 | + 'forms', |
| 1420 | + 'Delete only the section, so its question joins the part before it, or delete the section with its question?', |
| 1421 | + 'Delete only the section, so its %n questions join the part before it, or delete the section with its %n questions?', |
| 1422 | + sectionQuestions.length, |
| 1423 | + ) |
| 1424 | + if (isFirst && end !== -1) { |
| 1425 | + text = n( |
| 1426 | + 'forms', |
| 1427 | + 'Delete only the section, so its question joins the next section, or delete the section with its question?', |
| 1428 | + 'Delete only the section, so its %n questions join the next section, or delete the section with its %n questions?', |
| 1429 | + sectionQuestions.length, |
| 1430 | + ) |
| 1431 | + } else if (isFirst) { |
| 1432 | + text = n( |
| 1433 | + 'forms', |
| 1434 | + 'Delete only the section, so its question stays in a form without sections, or delete the section with its question?', |
| 1435 | + 'Delete only the section, so its %n questions stay in a form without sections, or delete the section with its %n questions?', |
| 1436 | + sectionQuestions.length, |
| 1437 | + ) |
| 1438 | + } |
| 1439 | + let withQuestions: boolean |
| 1440 | + try { |
| 1441 | + withQuestions = await showConfirmation({ |
| 1442 | + name: t('forms', 'Delete section'), |
| 1443 | + text, |
| 1444 | + labelConfirm: t('forms', 'Delete questions'), |
| 1445 | + labelReject: t('forms', 'Keep questions'), |
| 1446 | + severity: 'warning', |
| 1447 | + }) |
| 1448 | + } catch { |
| 1449 | + return |
| 1450 | + } |
| 1451 | + await deleteQuestions( |
| 1452 | + withQuestions |
| 1453 | + ? [...sectionQuestions.map(({ id }) => id), questionId] |
| 1454 | + : [questionId], |
| 1455 | + ) |
1380 | 1456 | } |
1381 | 1457 |
|
1382 | 1458 | /** |
@@ -1532,7 +1608,7 @@ export default defineComponent({ |
1532 | 1608 | resizeTitle, |
1533 | 1609 | resizeDescription, |
1534 | 1610 | addQuestion, |
1535 | | - deleteQuestion, |
| 1611 | + onDelete, |
1536 | 1612 | insertQuestion, |
1537 | 1613 | cloneQuestion, |
1538 | 1614 | questionNumbers, |
|
0 commit comments