Skip to content

Commit be7e6db

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 c67d327 commit be7e6db

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
@@ -1320,65 +1320,141 @@ export default defineComponent({
13201320
}
13211321
13221322
/**
1323-
* Delete a question
1323+
* 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
13241324
*
1325-
* @param questionId the question id to delete
1325+
* @param questionIds the ids of the questions to delete
13261326
*/
1327-
const deleteQuestion = async (questionId: number): Promise<void> => {
1328-
if (deletingQuestionIds.has(questionId)) {
1327+
const deleteQuestions = async (questionIds: number[]): Promise<void> => {
1328+
const ids = questionIds.filter((id) => !deletingQuestionIds.has(id))
1329+
if (ids.length === 0) {
13291330
return
13301331
}
1331-
deletingQuestionIds.add(questionId)
1332+
ids.forEach((id) => deletingQuestionIds.add(id))
13321333
const formId = props.form.id
1334+
const oldQuestions = props.form.questions
13331335
isLoadingQuestions.value = true
13341336
startChangingQuestions(formId)
13351337
1338+
const deleted = new Set<number>()
13361339
try {
1337-
await queueQuestionRequest(
1338-
props.form.questions.map(({ id }) => id),
1339-
() =>
1340-
axios.delete(
1341-
generateOcsUrl(
1342-
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
1343-
{
1344-
id: props.form.id,
1345-
questionId,
1346-
},
1340+
for (const questionId of ids) {
1341+
await queueQuestionRequest(
1342+
props.form.questions.map(({ id }) => id),
1343+
() =>
1344+
axios.delete(
1345+
generateOcsUrl(
1346+
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
1347+
{
1348+
id: props.form.id,
1349+
questionId,
1350+
},
1351+
),
13471352
),
1348-
),
1349-
)
1350-
if (props.form.id !== formId) {
1351-
return
1353+
)
1354+
deleted.add(questionId)
13521355
}
1353-
const position = props.form.questions.findIndex(
1354-
({ id }) => id === questionId,
1355-
)
1356-
const remaining = props.form.questions.filter(
1357-
(search: FormsQuestion) => search.id !== questionId,
1358-
)
1359-
const reordered =
1360-
props.form.questions[position]?.type === QUESTION_TYPE_SECTION
1361-
? withNextSectionMovedUp(remaining, position)
1362-
: remaining
1363-
const questions =
1364-
reordered === remaining
1365-
? remaining
1366-
: withoutDisallowedJumps(reordered)
1367-
warnAboutJumpChanges(props.form.questions, questions)
1368-
emit('update:form', { ...props.form, questions })
1369-
emitEvent('forms:last-updated:set', props.form.id)
13701356
} catch (error) {
1371-
logger.error(`Error while removing question ${questionId}`, {
1372-
error,
1373-
})
1357+
logger.error('Error while removing questions', { error, ids })
13741358
showError(
13751359
t('forms', 'There was an error while removing the question'),
13761360
)
13771361
} finally {
1362+
if (props.form.id === formId && deleted.size > 0) {
1363+
let questions = props.form.questions
1364+
let moved = false
1365+
for (const id of deleted) {
1366+
const position = questions.findIndex(
1367+
(question) => question.id === id,
1368+
)
1369+
const remaining = questions.filter(
1370+
(question) => question.id !== id,
1371+
)
1372+
const next =
1373+
questions[position]?.type === QUESTION_TYPE_SECTION
1374+
? withNextSectionMovedUp(remaining, position)
1375+
: remaining
1376+
moved = moved || next !== remaining
1377+
questions = next
1378+
}
1379+
if (moved) {
1380+
questions = withoutDisallowedJumps(questions)
1381+
}
1382+
warnAboutJumpChanges(oldQuestions, questions)
1383+
emit('update:form', { ...props.form, questions })
1384+
emitEvent('forms:last-updated:set', props.form.id)
1385+
}
13781386
isLoadingQuestions.value = false
13791387
endChangingQuestions(formId)
1380-
deletingQuestionIds.delete(questionId)
1388+
ids.forEach((id) => deletingQuestionIds.delete(id))
1389+
}
1390+
}
1391+
1392+
/**
1393+
* 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
1394+
*
1395+
* @param questionId the id of the question or section
1396+
*/
1397+
const onDelete = async (questionId: number): Promise<void> => {
1398+
const position = props.form.questions.findIndex(
1399+
({ id }) => id === questionId,
1400+
)
1401+
if (props.form.questions[position]?.type !== QUESTION_TYPE_SECTION) {
1402+
await deleteQuestions([questionId])
1403+
return
13811404
}
1405+
const end = props.form.questions.findIndex(
1406+
(question, index) =>
1407+
index > position && question.type === QUESTION_TYPE_SECTION,
1408+
)
1409+
const sectionQuestions = props.form.questions.slice(
1410+
position + 1,
1411+
end === -1 ? undefined : end,
1412+
)
1413+
if (sectionQuestions.length === 0) {
1414+
await deleteQuestions([questionId])
1415+
return
1416+
}
1417+
const isFirst = !props.form.questions
1418+
.slice(0, position)
1419+
.some((question) => question.type === QUESTION_TYPE_SECTION)
1420+
let text = n(
1421+
'forms',
1422+
'Delete only the section, so its question joins the part before it, or delete the section with its question?',
1423+
'Delete only the section, so its %n questions join the part before it, or delete the section with its %n questions?',
1424+
sectionQuestions.length,
1425+
)
1426+
if (isFirst && end !== -1) {
1427+
text = n(
1428+
'forms',
1429+
'Delete only the section, so its question joins the next section, or delete the section with its question?',
1430+
'Delete only the section, so its %n questions join the next section, or delete the section with its %n questions?',
1431+
sectionQuestions.length,
1432+
)
1433+
} else if (isFirst) {
1434+
text = n(
1435+
'forms',
1436+
'Delete only the section, so its question stays in a form without sections, or delete the section with its question?',
1437+
'Delete only the section, so its %n questions stay in a form without sections, or delete the section with its %n questions?',
1438+
sectionQuestions.length,
1439+
)
1440+
}
1441+
let withQuestions: boolean
1442+
try {
1443+
withQuestions = await showConfirmation({
1444+
name: t('forms', 'Delete section'),
1445+
text,
1446+
labelConfirm: t('forms', 'Delete questions'),
1447+
labelReject: t('forms', 'Keep questions'),
1448+
severity: 'warning',
1449+
})
1450+
} catch {
1451+
return
1452+
}
1453+
await deleteQuestions(
1454+
withQuestions
1455+
? [...sectionQuestions.map(({ id }) => id), questionId]
1456+
: [questionId],
1457+
)
13821458
}
13831459
13841460
/**
@@ -1534,7 +1610,7 @@ export default defineComponent({
15341610
resizeTitle,
15351611
resizeDescription,
15361612
addQuestion,
1537-
deleteQuestion,
1613+
onDelete,
15381614
insertQuestion,
15391615
cloneQuestion,
15401616
questionNumbers,

0 commit comments

Comments
 (0)