Skip to content

Commit 1376228

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 dbb4334 commit 1376228

3 files changed

Lines changed: 141 additions & 32 deletions

File tree

‎playwright/e2e/sections.spec.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,59 @@ test.describe('Sections', () => {
494494
await expect(label(2)).toHaveText('End of the form')
495495
})
496496

497+
test('Deleting a section asks whether to keep its questions', async ({
498+
page,
499+
}) => {
500+
const form = await api(page.request, 'POST', '/forms')
501+
for (const [type, text] of [
502+
['section', 'Part one'],
503+
['short', 'First'],
504+
['section', 'Part two'],
505+
['short', 'Second'],
506+
['short', 'Third'],
507+
['section', 'Empty part'],
508+
]) {
509+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
510+
type,
511+
text,
512+
})
513+
}
514+
await page.goto(`apps/forms/${form.hash}/edit`)
515+
const deleteSection = async (number: number) => {
516+
await page
517+
.getByRole('listitem', { name: `Section number ${number}` })
518+
.getByRole('button', { name: 'Actions', exact: true })
519+
.click()
520+
await page.getByRole('menuitem', { name: 'Delete section' }).click()
521+
}
522+
523+
await deleteSection(3)
524+
await expect(
525+
page.getByRole('listitem', { name: 'Section number 3' }),
526+
).toHaveCount(0)
527+
528+
await deleteSection(2)
529+
const dialog = page.getByRole('dialog', { name: 'Delete section' })
530+
await expect(dialog).toContainText(
531+
'Delete only the section, so its 2 questions join the part before it',
532+
)
533+
await dialog.getByRole('button', { name: 'Keep questions' }).click()
534+
await expect(
535+
page.getByRole('listitem', { name: 'Section number 2' }),
536+
).toHaveCount(0)
537+
await expect(
538+
page.getByRole('listitem', { name: 'Question number 3' }),
539+
).toBeVisible()
540+
541+
await deleteSection(1)
542+
await dialog.getByRole('button', { name: 'Delete questions' }).click()
543+
await expect(
544+
page.getByRole('listitem', { name: /^(Question|Section) number/ }),
545+
).toHaveCount(0)
546+
const { questions } = await api(page.request, 'GET', `/forms/${form.id}`)
547+
expect(questions).toEqual([])
548+
})
549+
497550
test('Each page checks its required questions, an empty section is an info page', async ({
498551
page,
499552
submitView,

‎src/components/Questions/Question.vue‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@
149149
<template #icon>
150150
<NcIconSvgWrapper :svg="IconDelete" />
151151
</template>
152-
{{ t('forms', 'Delete question') }}
152+
{{
153+
isSection
154+
? t('forms', 'Delete section')
155+
: t('forms', 'Delete question')
156+
}}
153157
</NcActionButton>
154158
</NcActions>
155159
</div>

‎src/views/Create.vue‎

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
"
168168
@update:options="updateQuestionOptions(index, $event)"
169169
@clone="cloneQuestion(question, index)"
170-
@delete="deleteQuestion(question.id)"
170+
@delete="onDelete(question.id)"
171171
@moveDown="onMoveDown(index)"
172172
@moveUp="onMoveUp(index)">
173173
<template
@@ -1155,56 +1155,108 @@ export default defineComponent({
11551155
}
11561156
11571157
/**
1158-
* Delete a question
1158+
* Delete questions one after the other, one request each, and warn once about the jumps this changes; a failed request keeps the remaining questions
11591159
*
1160-
* @param questionId the question id to delete
1160+
* @param questionIds the ids of the questions to delete
11611161
*/
1162-
const deleteQuestion = async (questionId: number): Promise<void> => {
1163-
if (deletingQuestionIds.has(questionId)) {
1162+
const deleteQuestions = async (questionIds: number[]): Promise<void> => {
1163+
const ids = questionIds.filter((id) => !deletingQuestionIds.has(id))
1164+
if (ids.length === 0) {
11641165
return
11651166
}
1166-
deletingQuestionIds.add(questionId)
1167+
ids.forEach((id) => deletingQuestionIds.add(id))
11671168
const formId = props.form.id
1169+
const oldQuestions = props.form.questions
11681170
isLoadingQuestions.value = true
11691171
startChangingQuestions(formId)
11701172
1173+
const deleted = new Set<number>()
11711174
try {
1172-
await queueQuestionRequest(
1173-
props.form.questions.map(({ id }) => id),
1174-
() =>
1175-
axios.delete(
1176-
generateOcsUrl(
1177-
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
1178-
{
1179-
id: props.form.id,
1180-
questionId,
1181-
},
1175+
for (const questionId of ids) {
1176+
await queueQuestionRequest(
1177+
props.form.questions.map(({ id }) => id),
1178+
() =>
1179+
axios.delete(
1180+
generateOcsUrl(
1181+
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
1182+
{
1183+
id: props.form.id,
1184+
questionId,
1185+
},
1186+
),
11821187
),
1183-
),
1184-
)
1185-
if (props.form.id !== formId) {
1186-
return
1188+
)
1189+
deleted.add(questionId)
11871190
}
1188-
const questions = props.form.questions.filter(
1189-
(search: FormsQuestion) => search.id !== questionId,
1190-
)
1191-
warnAboutJumpChanges(props.form.questions, questions)
1192-
emit('update:form', { ...props.form, questions })
1193-
emitEvent('forms:last-updated:set', props.form.id)
11941191
} catch (error) {
1195-
logger.error(`Error while removing question ${questionId}`, {
1196-
error,
1197-
})
1192+
logger.error('Error while removing questions', { error, ids })
11981193
showError(
11991194
t('forms', 'There was an error while removing the question'),
12001195
)
12011196
} finally {
1197+
if (props.form.id === formId && deleted.size > 0) {
1198+
const questions = props.form.questions.filter(
1199+
(search: FormsQuestion) => !deleted.has(search.id),
1200+
)
1201+
warnAboutJumpChanges(oldQuestions, questions)
1202+
emit('update:form', { ...props.form, questions })
1203+
emitEvent('forms:last-updated:set', props.form.id)
1204+
}
12021205
isLoadingQuestions.value = false
12031206
endChangingQuestions(formId)
1204-
deletingQuestionIds.delete(questionId)
1207+
ids.forEach((id) => deletingQuestionIds.delete(id))
12051208
}
12061209
}
12071210
1211+
/**
1212+
* Delete a question; for a section with questions, ask whether to delete only the section, so its questions join the part before it, or the section with its questions, deleting the section last
1213+
*
1214+
* @param questionId the id of the question or section
1215+
*/
1216+
const onDelete = async (questionId: number): Promise<void> => {
1217+
const position = props.form.questions.findIndex(
1218+
({ id }) => id === questionId,
1219+
)
1220+
if (props.form.questions[position]?.type !== QUESTION_TYPE_SECTION) {
1221+
await deleteQuestions([questionId])
1222+
return
1223+
}
1224+
const end = props.form.questions.findIndex(
1225+
(question, index) =>
1226+
index > position && question.type === QUESTION_TYPE_SECTION,
1227+
)
1228+
const sectionQuestions = props.form.questions.slice(
1229+
position + 1,
1230+
end === -1 ? undefined : end,
1231+
)
1232+
if (sectionQuestions.length === 0) {
1233+
await deleteQuestions([questionId])
1234+
return
1235+
}
1236+
let withQuestions: boolean
1237+
try {
1238+
withQuestions = await showConfirmation({
1239+
name: t('forms', 'Delete section'),
1240+
text: n(
1241+
'forms',
1242+
'Delete only the section, so its question joins the part before it, or delete the section with its question?',
1243+
'Delete only the section, so its %n questions join the part before it, or delete the section with its %n questions?',
1244+
sectionQuestions.length,
1245+
),
1246+
labelConfirm: t('forms', 'Delete questions'),
1247+
labelReject: t('forms', 'Keep questions'),
1248+
severity: 'warning',
1249+
})
1250+
} catch {
1251+
return
1252+
}
1253+
await deleteQuestions(
1254+
withQuestions
1255+
? [...sectionQuestions.map(({ id }) => id), questionId]
1256+
: [questionId],
1257+
)
1258+
}
1259+
12081260
/**
12091261
* Clone a question
12101262
*
@@ -1339,7 +1391,7 @@ export default defineComponent({
13391391
resizeTitle,
13401392
resizeDescription,
13411393
addQuestion,
1342-
deleteQuestion,
1394+
onDelete,
13431395
insertQuestion,
13441396
cloneQuestion,
13451397
questionNumbers,

0 commit comments

Comments
 (0)