Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion playwright/e2e/sections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,62 @@ test.describe('Sections', () => {
await expect(label(2)).toHaveText('End of the form')
})

test('Deleting a section asks whether to keep its questions', async ({
page,
}) => {
const form = await api(page.request, 'POST', '/forms')
for (const [type, text] of [
['section', 'Part one'],
['short', 'First'],
['section', 'Part two'],
['short', 'Second'],
['short', 'Third'],
['section', 'Empty part'],
]) {
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
type,
text,
})
}
await page.goto(`apps/forms/${form.hash}/edit`)
const deleteSection = async (number: number) => {
await page
.getByRole('listitem', { name: `Section number ${number}` })
.getByRole('button', { name: 'Actions', exact: true })
.click()
await page.getByRole('menuitem', { name: 'Delete section' }).click()
}

await deleteSection(3)
await expect(
page.getByRole('listitem', { name: 'Section number 3' }),
).toHaveCount(0)

await deleteSection(2)
const dialog = page.getByRole('dialog', { name: 'Delete section' })
await expect(dialog).toContainText(
'Delete only the section, so its 2 questions join the part before it',
)
await dialog.getByRole('button', { name: 'Keep questions' }).click()
await expect(
page.getByRole('listitem', { name: 'Section number 2' }),
).toHaveCount(0)
await expect(
page.getByRole('listitem', { name: 'Question number 3' }),
).toBeVisible()

await deleteSection(1)
await expect(dialog).toContainText(
'Delete only the section, so its 3 questions stay in a form without sections',
)
await dialog.getByRole('button', { name: 'Delete questions' }).click()
await expect(
page.getByRole('listitem', { name: /^(Question|Section) number/ }),
).toHaveCount(0)
const { questions } = await api(page.request, 'GET', `/forms/${form.id}`)
expect(questions).toEqual([])
})

test('Each page checks its required questions, an empty section is an info page', async ({
page,
submitView,
Expand Down Expand Up @@ -874,7 +930,12 @@ test.describe('Sections', () => {

const start = page.getByRole('listitem', { name: 'Section number 1' })
await start.getByRole('button', { name: 'Actions', exact: true }).click()
await page.getByRole('menuitem', { name: 'Delete question' }).click()
await page.getByRole('menuitem', { name: 'Delete section' }).click()
const dialog = page.getByRole('dialog', { name: 'Delete section' })
await expect(dialog).toContainText(
'Delete only the section, so its question joins the next section',
)
await dialog.getByRole('button', { name: 'Keep questions' }).click()

await expect(
page.getByRole('textbox', { name: 'Title of section number 1' }),
Expand Down
6 changes: 5 additions & 1 deletion src/components/Questions/Question.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@
<template #icon>
<NcIconSvgWrapper :svg="IconDelete" />
</template>
{{ t('forms', 'Delete question') }}
{{
isSection
? t('forms', 'Delete section')
: t('forms', 'Delete question')
}}
</NcActionButton>
</NcActions>
</Teleport>
Expand Down
160 changes: 118 additions & 42 deletions src/views/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"
@update:options="updateQuestionOptions(index, $event)"
@clone="cloneQuestion(question, index)"
@delete="deleteQuestion(question.id)"
@delete="onDelete(question.id)"
@moveDown="onMoveDown(index)"
@moveUp="onMoveUp(index)">
<template
Expand Down Expand Up @@ -1320,65 +1320,141 @@ export default defineComponent({
}

/**
* Delete a question
* 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
*
* @param questionId the question id to delete
* @param questionIds the ids of the questions to delete
*/
const deleteQuestion = async (questionId: number): Promise<void> => {
if (deletingQuestionIds.has(questionId)) {
const deleteQuestions = async (questionIds: number[]): Promise<void> => {
const ids = questionIds.filter((id) => !deletingQuestionIds.has(id))
if (ids.length === 0) {
return
}
deletingQuestionIds.add(questionId)
ids.forEach((id) => deletingQuestionIds.add(id))
const formId = props.form.id
const oldQuestions = props.form.questions
isLoadingQuestions.value = true
startChangingQuestions(formId)

const deleted = new Set<number>()
try {
await queueQuestionRequest(
props.form.questions.map(({ id }) => id),
() =>
axios.delete(
generateOcsUrl(
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
{
id: props.form.id,
questionId,
},
for (const questionId of ids) {
await queueQuestionRequest(
props.form.questions.map(({ id }) => id),
() =>
axios.delete(
generateOcsUrl(
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
{
id: props.form.id,
questionId,
},
),
),
),
)
if (props.form.id !== formId) {
return
)
deleted.add(questionId)
}
const position = props.form.questions.findIndex(
({ id }) => id === questionId,
)
const remaining = props.form.questions.filter(
(search: FormsQuestion) => search.id !== questionId,
)
const reordered =
props.form.questions[position]?.type === QUESTION_TYPE_SECTION
? withNextSectionMovedUp(remaining, position)
: remaining
const questions =
reordered === remaining
? remaining
: withoutDisallowedJumps(reordered)
warnAboutJumpChanges(props.form.questions, questions)
emit('update:form', { ...props.form, questions })
emitEvent('forms:last-updated:set', props.form.id)
} catch (error) {
logger.error(`Error while removing question ${questionId}`, {
error,
})
logger.error('Error while removing questions', { error, ids })
showError(
t('forms', 'There was an error while removing the question'),
)
} finally {
if (props.form.id === formId && deleted.size > 0) {
let questions = props.form.questions
let moved = false
for (const id of deleted) {
const position = questions.findIndex(
(question) => question.id === id,
)
const remaining = questions.filter(
(question) => question.id !== id,
)
const next =
questions[position]?.type === QUESTION_TYPE_SECTION
? withNextSectionMovedUp(remaining, position)
: remaining
moved = moved || next !== remaining
questions = next
}
if (moved) {
questions = withoutDisallowedJumps(questions)
}
warnAboutJumpChanges(oldQuestions, questions)
emit('update:form', { ...props.form, questions })
emitEvent('forms:last-updated:set', props.form.id)
}
isLoadingQuestions.value = false
endChangingQuestions(formId)
deletingQuestionIds.delete(questionId)
ids.forEach((id) => deletingQuestionIds.delete(id))
}
}

/**
* 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
*
* @param questionId the id of the question or section
*/
const onDelete = async (questionId: number): Promise<void> => {
const position = props.form.questions.findIndex(
({ id }) => id === questionId,
)
if (props.form.questions[position]?.type !== QUESTION_TYPE_SECTION) {
await deleteQuestions([questionId])
return
}
const end = props.form.questions.findIndex(
(question, index) =>
index > position && question.type === QUESTION_TYPE_SECTION,
)
const sectionQuestions = props.form.questions.slice(
position + 1,
end === -1 ? undefined : end,
)
if (sectionQuestions.length === 0) {
await deleteQuestions([questionId])
return
}
const isFirst = !props.form.questions
.slice(0, position)
.some((question) => question.type === QUESTION_TYPE_SECTION)
let text = n(
'forms',
'Delete only the section, so its question joins the part before it, or delete the section with its question?',
'Delete only the section, so its %n questions join the part before it, or delete the section with its %n questions?',
sectionQuestions.length,
)
if (isFirst && end !== -1) {
text = n(
'forms',
'Delete only the section, so its question joins the next section, or delete the section with its question?',
'Delete only the section, so its %n questions join the next section, or delete the section with its %n questions?',
sectionQuestions.length,
)
} else if (isFirst) {
text = n(
'forms',
'Delete only the section, so its question stays in a form without sections, or delete the section with its question?',
'Delete only the section, so its %n questions stay in a form without sections, or delete the section with its %n questions?',
sectionQuestions.length,
)
}
let withQuestions: boolean
try {
withQuestions = await showConfirmation({
name: t('forms', 'Delete section'),
text,
labelConfirm: t('forms', 'Delete questions'),
labelReject: t('forms', 'Keep questions'),
severity: 'warning',
})
} catch {
return
}
await deleteQuestions(
withQuestions
? [...sectionQuestions.map(({ id }) => id), questionId]
: [questionId],
)
}

/**
Expand Down Expand Up @@ -1534,7 +1610,7 @@ export default defineComponent({
resizeTitle,
resizeDescription,
addQuestion,
deleteQuestion,
onDelete,
insertQuestion,
cloneQuestion,
questionNumbers,
Expand Down
Loading