Skip to content

Commit 79538a6

Browse files
feat(section): reorder sections in a dialog
"Move section" in the menu of a section opens a dialog listing the sections in their order, as in Google Forms. Up and down buttons change the order there, and saving moves each section with its questions in one reorder request; cancelling keeps the form as it was. Questions before the first section stay first. Section cards have no drag handle or move buttons any more, so a section is never dragged alone away from its questions. Jumps that the new order would turn backward or into another section are handled as for any reorder. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent ac61bf5 commit 79538a6

6 files changed

Lines changed: 642 additions & 23 deletions

File tree

‎playwright/e2e/sections.spec.ts‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { expect, mergeTests } from '@playwright/test'
99
import {
1010
getQuestionIdsInSections,
1111
isEmptySection,
12+
reorderSections,
1213
} from '../../src/utils/Sections.ts'
1314
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
1415
import { test as submitTest } from '../support/fixtures/submit.ts'
@@ -639,6 +640,170 @@ test.describe('Sections', () => {
639640
expect(questions).toEqual([])
640641
})
641642

643+
test('Sections are reordered with their questions', () => {
644+
const items = [
645+
{ id: 1, type: 'short' },
646+
{ id: 2, type: 'section' },
647+
{ id: 3, type: 'short' },
648+
{ id: 4, type: 'section' },
649+
{ id: 5, type: 'short' },
650+
{ id: 6, type: 'short' },
651+
{ id: 7, type: 'section' },
652+
]
653+
const ids = (questions: typeof items) => questions.map(({ id }) => id)
654+
expect(ids(reorderSections(items, [4, 7, 2]))).toEqual([1, 4, 5, 6, 7, 2, 3])
655+
expect(ids(reorderSections(items, [7, 2]))).toEqual([1, 7, 2, 3, 4, 5, 6])
656+
expect(ids(reorderSections(items.slice(0, 1), [2]))).toEqual([1])
657+
})
658+
659+
test('The reorder sections dialog moves sections with their questions on save, by button or by dragging', async ({
660+
page,
661+
}) => {
662+
const form = await api(page.request, 'POST', '/forms')
663+
const ids: number[] = []
664+
for (const [type, text] of [
665+
['section', 'Part one'],
666+
['short', 'First'],
667+
['section', 'Part two'],
668+
['short', 'Second'],
669+
['short', 'Third'],
670+
]) {
671+
ids.push(
672+
(
673+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
674+
type,
675+
text,
676+
})
677+
).id,
678+
)
679+
}
680+
await page.goto(`apps/forms/${form.hash}/edit`)
681+
682+
const firstSection = page.getByRole('listitem', { name: 'Section number 1' })
683+
await expect(
684+
firstSection.getByRole('button', { name: /^Move/ }),
685+
).toHaveCount(0)
686+
await firstSection
687+
.getByRole('button', { name: 'Actions', exact: true })
688+
.click()
689+
await page.getByRole('menuitem', { name: 'Move section' }).click()
690+
691+
const dialog = page.getByRole('dialog', { name: 'Reorder sections' })
692+
const items = dialog.getByRole('listitem')
693+
await expect(items).toHaveText([
694+
/Part one\s*Section 1 of 2/,
695+
/Part two\s*Section 2 of 2/,
696+
])
697+
const save = dialog.getByRole('button', { name: 'Save' })
698+
await expect(save).toBeDisabled()
699+
await expect(
700+
dialog.getByRole('button', { name: 'Move section Part one up' }),
701+
).toBeDisabled()
702+
703+
const down = dialog.getByRole('button', {
704+
name: 'Move section Part one down',
705+
})
706+
await down.click()
707+
await expect(items).toHaveText([/Part two/, /Part one/])
708+
await expect(
709+
dialog.getByRole('button', { name: 'Move section Part one up' }),
710+
).toBeFocused()
711+
await dialog.getByRole('button', { name: 'Cancel' }).click()
712+
await expect(dialog).toHaveCount(0)
713+
await expect(
714+
firstSection.getByRole('textbox', { name: 'Title of section number 1' }),
715+
).toHaveValue('Part one')
716+
717+
await firstSection
718+
.getByRole('button', { name: 'Actions', exact: true })
719+
.click()
720+
await page.getByRole('menuitem', { name: 'Move section' }).click()
721+
await expect(items).toHaveText([/Part one/, /Part two/])
722+
await items
723+
.nth(1)
724+
.locator('.reorder-sections__handle')
725+
.dragTo(items.nth(0), { targetPosition: { x: 20, y: 5 } })
726+
await expect(items).toHaveText([/Part two/, /Part one/])
727+
const order = page.waitForResponse(
728+
(response) =>
729+
response.request().method() === 'PATCH'
730+
&& response.url().endsWith(`/forms/${form.id}/questions`),
731+
)
732+
await save.click()
733+
await order
734+
735+
const { questions } = await api(page.request, 'GET', `/forms/${form.id}`)
736+
expect(questions.map(({ id }: { id: number }) => id)).toEqual([
737+
ids[2],
738+
ids[3],
739+
ids[4],
740+
ids[0],
741+
ids[1],
742+
])
743+
await expect(
744+
page.getByRole('textbox', { name: 'Title of section number 1' }),
745+
).toHaveValue('Part two')
746+
})
747+
748+
test('Reordering sections names the moved section when a jump would go back', async ({
749+
page,
750+
}) => {
751+
const form = await api(page.request, 'POST', '/forms')
752+
const ids: Record<string, number> = {}
753+
for (const [type, text] of [
754+
['section', 'Part one'],
755+
['multiple_unique', 'Skip ahead?'],
756+
['section', 'Part two'],
757+
['short', 'Second'],
758+
]) {
759+
ids[text] = (
760+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
761+
type,
762+
text,
763+
})
764+
).id
765+
}
766+
const options = await api(
767+
page.request,
768+
'POST',
769+
`/forms/${form.id}/questions/${ids['Skip ahead?']}/options`,
770+
{ optionTexts: ['Yes', 'No'] },
771+
)
772+
await api(
773+
page.request,
774+
'PATCH',
775+
`/forms/${form.id}/questions/${ids['Skip ahead?']}`,
776+
{
777+
keyValuePairs: {
778+
extraSettings: {
779+
goTo: [{ optionId: options[0].id, target: ids['Part two'] }],
780+
},
781+
},
782+
},
783+
)
784+
await page.goto(`apps/forms/${form.hash}/edit`)
785+
786+
await page
787+
.getByRole('listitem', { name: 'Section number 1' })
788+
.getByRole('button', { name: 'Actions', exact: true })
789+
.click()
790+
await page.getByRole('menuitem', { name: 'Move section' }).click()
791+
const reorder = page.getByRole('dialog', { name: 'Reorder sections' })
792+
await reorder
793+
.getByRole('button', { name: 'Move section Part two up' })
794+
.click()
795+
await reorder.getByRole('button', { name: 'Save' }).click()
796+
797+
const dialog = page.getByRole('dialog', { name: 'Jumps cannot go back' })
798+
await expect(dialog).toContainText(
799+
'Move the section "Part two" back, or remove these jumps?',
800+
)
801+
await dialog.getByRole('button', { name: 'Move back' }).click()
802+
await expect(
803+
page.getByRole('textbox', { name: 'Title of section number 1' }),
804+
).toHaveValue('Part one')
805+
})
806+
642807
test('Each page checks its required questions, an empty section is an info page', async ({
643808
page,
644809
submitView,

‎src/components/Questions/Question.vue‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<!-- Drag handle -->
2222
<!-- TODO: implement arrow key mapping to reorder question -->
2323
<div
24-
v-if="!readOnly"
24+
v-if="!readOnly && !isSection"
2525
class="question__drag-handle"
2626
:class="{
2727
'question__drag-handle--shiftup': shiftDragHandle,
@@ -144,6 +144,15 @@
144144
}}
145145
</NcActionCheckbox>
146146
<slot name="actions" />
147+
<NcActionButton
148+
v-if="isSection && (canMoveUp || canMoveDown)"
149+
closeAfterClick
150+
@click="$emit('moveSection')">
151+
<template #icon>
152+
<NcIconSvgWrapper :svg="IconSwapVert" />
153+
</template>
154+
{{ t('forms', 'Move section') }}
155+
</NcActionButton>
147156
<NcActionInput
148157
v-if="!isSection"
149158
:label="t('forms', 'Technical name of the question')"
@@ -263,6 +272,7 @@ import IconAlertCircleOutline from '@material-symbols/svg-400/outlined/error.svg
263272
import IconArrowDown from '@material-symbols/svg-400/outlined/keyboard_arrow_down.svg?raw'
264273
import IconArrowUp from '@material-symbols/svg-400/outlined/keyboard_arrow_up.svg?raw'
265274
import IconDotsHorizontal from '@material-symbols/svg-400/outlined/more_horiz.svg?raw'
275+
import IconSwapVert from '@material-symbols/svg-400/outlined/swap_vert.svg?raw'
266276
import { t } from '@nextcloud/l10n'
267277
import {
268278
computed,
@@ -409,6 +419,7 @@ export default defineComponent({
409419
'update:isRequired',
410420
'moveDown',
411421
'moveUp',
422+
'moveSection',
412423
'delete',
413424
'clone',
414425
],
@@ -588,6 +599,7 @@ export default defineComponent({
588599
IconArrowDown,
589600
IconArrowUp,
590601
IconContentCopy,
602+
IconSwapVert,
591603
IconDelete,
592604
IconDotsHorizontal,
593605
IconDragIndicator,

0 commit comments

Comments
 (0)