Skip to content

Commit 3d1831d

Browse files
feat(section): ask whether deleting a section keeps its questions
Deleting a section with questions asks whether to delete only the section, so its questions join the part before it, or the section with its questions, as in Microsoft Forms. The questions are deleted one after the other with the existing endpoint, and the editor warns once about the jumps this changes. An empty section is deleted directly. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 4cd826f commit 3d1831d

3 files changed

Lines changed: 185 additions & 44 deletions

File tree

‎playwright/e2e/sections.spec.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,62 @@ test.describe('Sections', () => {
583583
await expect(label(2)).toHaveText('End of the form')
584584
})
585585

586+
test('Deleting a section asks whether to keep its questions', async ({
587+
page,
588+
}) => {
589+
const form = await api(page.request, 'POST', '/forms')
590+
for (const [type, text] of [
591+
['section', 'Part one'],
592+
['short', 'First'],
593+
['section', 'Part two'],
594+
['short', 'Second'],
595+
['short', 'Third'],
596+
['section', 'Empty part'],
597+
]) {
598+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
599+
type,
600+
text,
601+
})
602+
}
603+
await page.goto(`apps/forms/${form.hash}/edit`)
604+
const deleteSection = async (number: number) => {
605+
await page
606+
.getByRole('listitem', { name: `Section number ${number}` })
607+
.getByRole('button', { name: 'Actions', exact: true })
608+
.click()
609+
await page.getByRole('menuitem', { name: 'Delete section' }).click()
610+
}
611+
612+
await deleteSection(3)
613+
await expect(
614+
page.getByRole('listitem', { name: 'Section number 3' }),
615+
).toHaveCount(0)
616+
617+
await deleteSection(2)
618+
const dialog = page.getByRole('dialog', { name: 'Delete section' })
619+
await expect(dialog).toContainText(
620+
'Delete only the section, so its 2 questions join the part before it',
621+
)
622+
await dialog.getByRole('button', { name: 'Keep questions' }).click()
623+
await expect(
624+
page.getByRole('listitem', { name: 'Section number 2' }),
625+
).toHaveCount(0)
626+
await expect(
627+
page.getByRole('listitem', { name: 'Question number 3' }),
628+
).toBeVisible()
629+
630+
await deleteSection(1)
631+
await expect(dialog).toContainText(
632+
'Delete only the section, so its 3 questions stay in a form without sections',
633+
)
634+
await dialog.getByRole('button', { name: 'Delete questions' }).click()
635+
await expect(
636+
page.getByRole('listitem', { name: /^(Question|Section) number/ }),
637+
).toHaveCount(0)
638+
const { questions } = await api(page.request, 'GET', `/forms/${form.id}`)
639+
expect(questions).toEqual([])
640+
})
641+
586642
test('Each page checks its required questions, an empty section is an info page', async ({
587643
page,
588644
submitView,
@@ -874,7 +930,12 @@ test.describe('Sections', () => {
874930

875931
const start = page.getByRole('listitem', { name: 'Section number 1' })
876932
await start.getByRole('button', { name: 'Actions', exact: true }).click()
877-
await page.getByRole('menuitem', { name: 'Delete question' }).click()
933+
await page.getByRole('menuitem', { name: 'Delete section' }).click()
934+
const dialog = page.getByRole('dialog', { name: 'Delete section' })
935+
await expect(dialog).toContainText(
936+
'Delete only the section, so its question joins the next section',
937+
)
938+
await dialog.getByRole('button', { name: 'Keep questions' }).click()
878939

879940
await expect(
880941
page.getByRole('textbox', { name: 'Title of section number 1' }),

‎src/components/Questions/Question.vue‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@
169169
<template #icon>
170170
<NcIconSvgWrapper :svg="IconDelete" />
171171
</template>
172-
{{ t('forms', 'Delete question') }}
172+
{{
173+
isSection
174+
? t('forms', 'Delete section')
175+
: t('forms', 'Delete question')
176+
}}
173177
</NcActionButton>
174178
</NcActions>
175179
</Teleport>

‎src/views/Create.vue‎

Lines changed: 118 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
"
199199
@update:options="updateQuestionOptions(index, $event)"
200200
@clone="cloneQuestion(question, index)"
201-
@delete="deleteQuestion(question.id)"
201+
@delete="onDelete(question.id)"
202202
@moveDown="onMoveDown(index)"
203203
@moveUp="onMoveUp(index)">
204204
<template
@@ -1318,65 +1318,141 @@ export default defineComponent({
13181318
}
13191319
13201320
/**
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
13221322
*
1323-
* @param questionId the question id to delete
1323+
* @param questionIds the ids of the questions to delete
13241324
*/
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) {
13271328
return
13281329
}
1329-
deletingQuestionIds.add(questionId)
1330+
ids.forEach((id) => deletingQuestionIds.add(id))
13301331
const formId = props.form.id
1332+
const oldQuestions = props.form.questions
13311333
isLoadingQuestions.value = true
13321334
startChangingQuestions(formId)
13331335
1336+
const deleted = new Set<number>()
13341337
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+
),
13451350
),
1346-
),
1347-
)
1348-
if (props.form.id !== formId) {
1349-
return
1351+
)
1352+
deleted.add(questionId)
13501353
}
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)
13681354
} catch (error) {
1369-
logger.error(`Error while removing question ${questionId}`, {
1370-
error,
1371-
})
1355+
logger.error('Error while removing questions', { error, ids })
13721356
showError(
13731357
t('forms', 'There was an error while removing the question'),
13741358
)
13751359
} 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+
}
13761384
isLoadingQuestions.value = false
13771385
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
13791402
}
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+
)
13801456
}
13811457
13821458
/**
@@ -1532,7 +1608,7 @@ export default defineComponent({
15321608
resizeTitle,
15331609
resizeDescription,
15341610
addQuestion,
1535-
deleteQuestion,
1611+
onDelete,
15361612
insertQuestion,
15371613
cloneQuestion,
15381614
questionNumbers,

0 commit comments

Comments
 (0)