Skip to content

Commit 49d2b31

Browse files
feat(conditional): skip questions along the path when answering
The submit view shows the questions on the path of the current answers and announces when questions are added or skipped. Answers to skipped questions are kept while the form is open, but not submitted. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent ab7c5fe commit 49d2b31

2 files changed

Lines changed: 247 additions & 7 deletions

File tree

‎playwright/e2e/jumps.spec.ts‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import type { APIRequestContext } from '@playwright/test'
7+
8+
import { expect, mergeTests } from '@playwright/test'
9+
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
10+
import { test as submitTest } from '../support/fixtures/submit.ts'
11+
12+
const test = mergeTests(randomUserTest, submitTest)
13+
14+
/**
15+
* Call the Forms OCS API as the logged in user
16+
*
17+
* @param request the request context of the page
18+
* @param method the HTTP method
19+
* @param path the path below `api/v3`
20+
* @param data the JSON body
21+
*/
22+
async function api(
23+
request: APIRequestContext,
24+
method: string,
25+
path: string,
26+
data?: unknown,
27+
) {
28+
const response = await request.fetch(`../ocs/v2.php/apps/forms/api/v3${path}`, {
29+
method,
30+
data,
31+
headers: { 'OCS-APIRequest': 'true', Accept: 'application/json' },
32+
})
33+
expect(response.ok()).toBeTruthy()
34+
return (await response.json()).ocs.data
35+
}
36+
37+
/**
38+
* Create a form with a radio question "Do you have a pet?" (Yes, No), a required short question "Which pet?" and a short question "Comments"
39+
*
40+
* @param request the request context of the page
41+
*/
42+
async function createPetForm(request: APIRequestContext) {
43+
const form = await api(request, 'POST', '/forms')
44+
await api(request, 'PATCH', `/forms/${form.id}`, {
45+
keyValuePairs: { title: 'Pet form' },
46+
})
47+
const pet = await api(request, 'POST', `/forms/${form.id}/questions`, {
48+
type: 'multiple_unique',
49+
text: 'Do you have a pet?',
50+
})
51+
const options = await api(
52+
request,
53+
'POST',
54+
`/forms/${form.id}/questions/${pet.id}/options`,
55+
{ optionTexts: ['Yes', 'No'] },
56+
)
57+
const which = await api(request, 'POST', `/forms/${form.id}/questions`, {
58+
type: 'short',
59+
text: 'Which pet?',
60+
})
61+
await api(request, 'PATCH', `/forms/${form.id}/questions/${which.id}`, {
62+
keyValuePairs: { isRequired: true },
63+
})
64+
const comments = await api(request, 'POST', `/forms/${form.id}/questions`, {
65+
type: 'short',
66+
text: 'Comments',
67+
})
68+
return { form, pet, options, which, comments }
69+
}
70+
71+
test.describe('Jumps', () => {
72+
test('An answer jumps to a later question, set in the editor', async ({
73+
page,
74+
}) => {
75+
const { form, pet, options, comments } = await createPetForm(page.request)
76+
await page.goto(`apps/forms/${form.hash}/edit`)
77+
78+
const question = page.getByRole('listitem', { name: 'Question number 1' })
79+
await question.getByRole('button', { name: 'Actions', exact: true }).click()
80+
await page
81+
.getByRole('checkbox', { name: 'Go to based on answer' })
82+
.click({ force: true })
83+
await page.keyboard.press('Escape')
84+
85+
await question.getByRole('combobox', { name: 'Question 1: after "No" go to' }).click()
86+
await page
87+
.getByRole('option')
88+
.filter({ hasText: 'Question 3: Comments' })
89+
.click()
90+
91+
await expect(async () => {
92+
const stored = await api(
93+
page.request,
94+
'GET',
95+
`/forms/${form.id}/questions/${pet.id}`,
96+
)
97+
expect(stored.extraSettings.goTo).toEqual([
98+
{ optionId: options[1].id, target: comments.id },
99+
])
100+
}).toPass()
101+
})
102+
103+
test('Skipped questions are hidden, not required and not submitted', async ({
104+
page,
105+
submitView,
106+
}) => {
107+
const { form, pet, options, comments } = await createPetForm(page.request)
108+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${pet.id}`, {
109+
keyValuePairs: {
110+
extraSettings: {
111+
goTo: [{ optionId: options[1].id, target: comments.id }],
112+
},
113+
},
114+
})
115+
await page.goto(`apps/forms/${form.hash}/submit`)
116+
117+
await expect(submitView.getQuestion(/Which pet\?/)).toBeVisible()
118+
await submitView.checkRadio('Do you have a pet?', 'Yes')
119+
await submitView.fillText(/Which pet\?/, 'Rex')
120+
await submitView.checkRadio('Do you have a pet?', 'No')
121+
await expect(submitView.getQuestion(/Which pet\?/)).toBeHidden()
122+
await submitView.fillText('Comments', 'None')
123+
124+
await submitView.submit()
125+
await expect(submitView.successMessage).toBeVisible()
126+
127+
const { submissions } = await api(
128+
page.request,
129+
'GET',
130+
`/forms/${form.id}/submissions`,
131+
)
132+
expect(submissions[0].answers.map((answer) => answer.text).sort()).toEqual([
133+
'No',
134+
'None',
135+
])
136+
})
137+
138+
test('"End of the form" after a question hides the questions after it', async ({
139+
page,
140+
submitView,
141+
}) => {
142+
const { form, pet, options } = await createPetForm(page.request)
143+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${pet.id}`, {
144+
keyValuePairs: {
145+
extraSettings: {
146+
goTo: [{ optionId: options[1].id, target: 'submit' }],
147+
},
148+
},
149+
})
150+
await page.goto(`apps/forms/${form.hash}/submit`)
151+
152+
await expect(submitView.getQuestion(/Which pet\?/)).toBeVisible()
153+
await expect(submitView.getQuestion('Comments')).toBeVisible()
154+
await submitView.checkRadio('Do you have a pet?', 'No')
155+
await expect(submitView.getQuestion(/Which pet\?/)).toBeHidden()
156+
await expect(submitView.getQuestion('Comments')).toBeHidden()
157+
await submitView.checkRadio('Do you have a pet?', 'Yes')
158+
await expect(submitView.getQuestion('Comments')).toBeVisible()
159+
})
160+
})

‎src/views/Submit.vue‎

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<div class="hidden-visually" aria-live="polite">
6161
{{ successAnnouncement }}
6262
</div>
63+
<div class="hidden-visually" aria-live="polite">
64+
{{ jumpAnnouncement }}
65+
</div>
6366

6467
<NcEmptyContent
6568
v-if="loading"
@@ -146,7 +149,7 @@
146149
<ul>
147150
<component
148151
:is="answerTypes[question.type].component"
149-
v-for="(question, index) in validQuestions"
152+
v-for="(question, index) in shownQuestions"
150153
ref="questionRefs"
151154
:key="question.id"
152155
v-bind="question"
@@ -250,7 +253,7 @@ import axios from '@nextcloud/axios'
250253
import { showError } from '@nextcloud/dialogs'
251254
import { emit as emitEvent } from '@nextcloud/event-bus'
252255
import { loadState } from '@nextcloud/initial-state'
253-
import { t } from '@nextcloud/l10n'
256+
import { getLanguage, n, t } from '@nextcloud/l10n'
254257
import moment from '@nextcloud/moment'
255258
import { generateOcsUrl } from '@nextcloud/router'
256259
import {
@@ -288,6 +291,7 @@ import {
288291
QUESTION_EXTRASETTINGS_OTHER_PREFIX,
289292
} from '../models/Constants.ts'
290293
import { PERMISSION_TYPES } from '../models/Permissions.ts'
294+
import { getVisibleQuestions } from '../utils/Jumps.ts'
291295
import logger from '../utils/Logger.ts'
292296
import OcsResponse2Data from '../utils/OcsResponse2Data.ts'
293297
import SetWindowTitle from '../utils/SetWindowTitle.ts'
@@ -423,7 +427,7 @@ export default defineComponent({
423427
const confirmButtonCallback = ref<(val: boolean) => void>(() => {})
424428
425429
const validQuestions = computed<SubmitQuestion[]>(() => {
426-
return props.form.questions.filter((question: FormsQuestion) => {
430+
return (props.form.questions ?? []).filter((question: FormsQuestion) => {
427431
// All questions must have a valid title
428432
if (question.text?.trim() === '') {
429433
return false
@@ -442,6 +446,78 @@ export default defineComponent({
442446
return new Set(validQuestions.value.map((question) => question.id))
443447
})
444448
449+
const visibleQuestionIds = computed<Set<number>>(() => {
450+
return new Set(
451+
getVisibleQuestions(props.form.questions ?? [], answers.value).map(
452+
(question) => question.id,
453+
),
454+
)
455+
})
456+
457+
const shownQuestions = computed<SubmitQuestion[]>(() => {
458+
return validQuestions.value.filter((question) =>
459+
visibleQuestionIds.value.has(question.id),
460+
)
461+
})
462+
463+
const submittedAnswers = computed<AnswersMap>(() => {
464+
return Object.fromEntries(
465+
Object.entries(answers.value).filter(([questionId]) =>
466+
visibleQuestionIds.value.has(Number(questionId)),
467+
),
468+
)
469+
})
470+
471+
const jumpAnnouncement = ref('')
472+
473+
/**
474+
* Announce the questions an answer added to or skipped from the path
475+
*
476+
* @param oldQuestions the questions shown before the answer
477+
* @param newQuestions the questions shown after the answer
478+
*/
479+
function announcePathChange(
480+
oldQuestions: SubmitQuestion[],
481+
newQuestions: SubmitQuestion[],
482+
): void {
483+
const oldIds = new Set(oldQuestions.map((question) => question.id))
484+
const newIds = new Set(newQuestions.map((question) => question.id))
485+
const added = newQuestions.filter((question) => !oldIds.has(question.id))
486+
const removed = oldQuestions.filter(
487+
(question) => !newIds.has(question.id),
488+
)
489+
if (added.length === 0 && removed.length === 0) {
490+
return
491+
}
492+
493+
const status = new Intl.ListFormat(getLanguage(), {
494+
type: 'conjunction',
495+
}).format(
496+
[
497+
added.length > 0
498+
? n(
499+
'forms',
500+
'%n question added',
501+
'%n questions added',
502+
added.length,
503+
)
504+
: '',
505+
removed.length > 0
506+
? n(
507+
'forms',
508+
'%n question skipped',
509+
'%n questions skipped',
510+
removed.length,
511+
)
512+
: '',
513+
].filter(Boolean),
514+
)
515+
jumpAnnouncement.value = ''
516+
setTimeout(() => {
517+
jumpAnnouncement.value = status
518+
}, 100)
519+
}
520+
445521
const isRequiredUsed = computed<boolean>(() => {
446522
return props.form.questions.some((question: FormsQuestion) =>
447523
Boolean(question.isRequired),
@@ -897,10 +973,12 @@ export default defineComponent({
897973
* @param values The new values
898974
*/
899975
function onUpdate(question: SubmitQuestion, values: AnswerValue): void {
976+
const shownBefore = shownQuestions.value
900977
answers.value = {
901978
...answers.value,
902979
[question.id]: values,
903980
}
981+
announcePathChange(shownBefore, shownQuestions.value)
904982
addFormFieldToLocalStorage(question)
905983
}
906984
@@ -1018,8 +1096,8 @@ export default defineComponent({
10181096
10191097
// in case no answer is set or all are empty show the confirmation dialog
10201098
if (
1021-
Object.keys(answers.value).length === 0
1022-
|| Object.values(answers.value).every((localAnswers) =>
1099+
Object.keys(submittedAnswers.value).length === 0
1100+
|| Object.values(submittedAnswers.value).every((localAnswers) =>
10231101
Array.isArray(localAnswers)
10241102
? localAnswers.length === 0
10251103
: Object.keys(localAnswers).length === 0,
@@ -1054,7 +1132,7 @@ export default defineComponent({
10541132
},
10551133
),
10561134
{
1057-
answers: answers.value,
1135+
answers: submittedAnswers.value,
10581136
},
10591137
)
10601138
} else {
@@ -1063,7 +1141,7 @@ export default defineComponent({
10631141
id: props.form.id,
10641142
}),
10651143
{
1066-
answers: answers.value,
1144+
answers: submittedAnswers.value,
10671145
shareHash: props.shareHash,
10681146
},
10691147
)
@@ -1230,6 +1308,8 @@ export default defineComponent({
12301308
...viewForm,
12311309
answerTypes,
12321310
answers,
1311+
jumpAnnouncement,
1312+
shownQuestions,
12331313
confirmClearFormButtons,
12341314
confirmEmptyModalButtons,
12351315
confirmLeaveFormButtons,

0 commit comments

Comments
 (0)