Skip to content

Commit

Permalink
Merge pull request #463 from DEFRA/feature/398642-post-submission-gui…
Browse files Browse the repository at this point in the history
…dance

Edit submission guidance
  • Loading branch information
davidjamesstone authored Sep 19, 2024
2 parents c1bad7b + 7b25501 commit 2b8b629
Show file tree
Hide file tree
Showing 34 changed files with 556 additions and 93 deletions.
5 changes: 4 additions & 1 deletion designer/server/src/common/constants/session-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const sessionNames = {
contactEmail: /** @type {const} */ ('contactEmailValidationFailure'),
contactPhone: /** @type {const} */ ('contactPhoneValidationFailure'),
contactOnline: /** @type {const} */ ('contactOnlineValidationFailure'),
fileDownload: /** @type {const} */ ('fileDownloadValidationFailure')
fileDownload: /** @type {const} */ ('fileDownloadValidationFailure'),
submissionGuidance: /** @type {const} */ (
'submissionGuidanceValidationFailure'
)
},
successNotification: /** @type {const} */ ('successNotification'),
errorList: /** @type {const} */ ('errorList')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formsLibraryPath } from '~/src/models/links.js'

/**
* @param {string} text
* @param {string} url
Expand All @@ -16,8 +18,8 @@ export function buildEntry(text, url, options) {
*/
export function buildNavigation(request) {
return [
buildEntry('Forms library', '/library', {
isActive: !!request?.path?.startsWith('/library')
buildEntry('Forms library', formsLibraryPath, {
isActive: !!request?.path?.startsWith(formsLibraryPath)
})
]
}
Expand Down
2 changes: 1 addition & 1 deletion designer/server/src/common/nunjucks/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { markdownToHtml } from '@defra/forms-model'
export { markdownToHtml as markdown } from '@defra/forms-model'
export { formatCurrency } from '~/src/common/nunjucks/filters/format-currency.js'
export { formatDate } from '~/src/common/nunjucks/filters/format-date.js'
export { formatJSON } from '~/src/common/nunjucks/filters/format-json.js'
6 changes: 2 additions & 4 deletions designer/server/src/models/file/file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formsLibraryBackLink } from '~/src/models/links.js'

/**
* @param { ValidationFailure<{ email: string }> } [validation]
*/
export function fileViewModel(validation) {
const pageTitle = 'You have a file to download'
return {
backLink: {
text: 'Back to forms library',
href: '/library'
},
backLink: formsLibraryBackLink,
pageTitle,
pageHeading: {
text: pageTitle,
Expand Down
6 changes: 2 additions & 4 deletions designer/server/src/models/forms/contact/email.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
Expand All @@ -10,10 +11,7 @@ export function emailViewModel(metadata, validation) {

return {
form: metadata,
backLink: {
text: 'Back',
href: `/library/${metadata.slug}`
},
backLink: formOverviewBackLink(metadata.slug),
pageTitle,
errorList: buildErrorList(formErrors),
formErrors: validation?.formErrors,
Expand Down
6 changes: 2 additions & 4 deletions designer/server/src/models/forms/contact/online.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
Expand All @@ -10,10 +11,7 @@ export function onlineViewModel(metadata, validation) {

return {
form: metadata,
backLink: {
text: 'Back',
href: `/library/${metadata.slug}`
},
backLink: formOverviewBackLink(metadata.slug),
pageTitle,
errorList: buildErrorList(formErrors),
formErrors: validation?.formErrors,
Expand Down
6 changes: 2 additions & 4 deletions designer/server/src/models/forms/contact/phone.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
Expand All @@ -10,10 +11,7 @@ export function phoneViewModel(metadata, validation) {

return {
form: metadata,
backLink: {
text: 'Back',
href: `/library/${metadata.slug}`
},
backLink: formOverviewBackLink(metadata.slug),
pageTitle,
errorList: buildErrorList(formErrors),
formErrors: validation?.formErrors,
Expand Down
6 changes: 2 additions & 4 deletions designer/server/src/models/forms/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { organisations } from '@defra/forms-model'

import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formsLibraryBackLink } from '~/src/models/links.js'
import {
ROUTE_PATH_CREATE_ORGANISATION,
ROUTE_PATH_CREATE_TITLE
Expand All @@ -15,10 +16,7 @@ export function titleViewModel(metadata, validation) {
const { formValues, formErrors } = validation ?? {}

return {
backLink: {
text: 'Back to forms library',
href: '/library'
},
backLink: formsLibraryBackLink,
pageTitle,
errorList: buildErrorList(formErrors, ['title']),
formErrors: validation?.formErrors,
Expand Down
18 changes: 4 additions & 14 deletions designer/server/src/models/forms/edit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as create from '~/src/models/forms/create.js'

const backLinkText = 'Back to form overview'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
Expand All @@ -9,10 +8,7 @@ const backLinkText = 'Back to form overview'
export function organisationViewModel(metadata, validation) {
return {
...create.organisationViewModel(metadata, validation),
backLink: {
text: backLinkText,
href: `/library/${metadata.slug}`
}
backLink: formOverviewBackLink(metadata.slug)
}
}

Expand All @@ -30,10 +26,7 @@ export function teamDetailsViewModel(metadata, validation) {
const teamDetailsModel = { ...teamModel, fields: nameAndEmailfields }
return {
...teamDetailsModel,
backLink: {
text: backLinkText,
href: `/library/${metadata.slug}`
}
backLink: formOverviewBackLink(metadata.slug)
}
}

Expand All @@ -46,10 +39,7 @@ export function titleViewModel(metadata, validation) {

return {
...titleModel,
backLink: {
text: backLinkText,
href: `/library/${metadata.slug}`
}
backLink: formOverviewBackLink(metadata.slug)
}
}

Expand Down
3 changes: 2 additions & 1 deletion designer/server/src/models/forms/form-lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from '~/src/common/nunjucks/index.js'
import { getFormSpecificNavigation } from '~/src/models/forms/library.js'
import { formOverviewPath } from '~/src/models/links.js'

/**
* Model to represent confirmation page dialog for a given form.
Expand All @@ -9,7 +10,7 @@ import { getFormSpecificNavigation } from '~/src/models/forms/library.js'
export function confirmationPageViewModel(form, errorList) {
const pageTitle = 'Are you sure you want to make the draft live?'

const formPath = `/library/${form.slug}`
const formPath = formOverviewPath(form.slug)
const navigation = getFormSpecificNavigation(formPath)

return {
Expand Down
22 changes: 11 additions & 11 deletions designer/server/src/models/forms/library.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { buildEntry } from '~/src/common/nunjucks/context/build-navigation.js'
import config from '~/src/config.js'
import * as forms from '~/src/lib/forms.js'
import {
formOverviewBackLink,
formsLibraryBackLink,
formOverviewPath,
formsLibraryPath
} from '~/src/models/links.js'

/**
* @param {string} token
Expand All @@ -24,15 +30,12 @@ export async function listViewModel(token) {
*/
export function overviewViewModel(metadata, notification) {
const pageTitle = metadata.title
const formPath = `/library/${metadata.slug}`
const formPath = formOverviewPath(metadata.slug)

const navigation = getFormSpecificNavigation(formPath, 'Overview')

return {
backLink: {
text: 'Back to forms library',
href: '/library'
},
backLink: formsLibraryBackLink,
navigation,
pageTitle,
pageHeading: {
Expand Down Expand Up @@ -85,15 +88,12 @@ export function overviewViewModel(metadata, notification) {
*/
export function editorViewModel(metadata, definition) {
const pageTitle = metadata.title
const formPath = `/library/${metadata.slug}`
const formPath = formOverviewPath(metadata.slug)

const navigation = getFormSpecificNavigation(formPath, 'Editor')

return {
backLink: {
text: 'Back to form overview',
href: `/library/${metadata.slug}`
},
backLink: formOverviewBackLink(metadata.slug),
navigation,
pageTitle,
pageHeading: {
Expand All @@ -114,7 +114,7 @@ export function editorViewModel(metadata, definition) {
*/
export function getFormSpecificNavigation(formPath, activePage = '') {
return [
['Forms library', '/library'],
['Forms library', formsLibraryPath],
['Overview', formPath],
['Editor', `${formPath}/editor`]
].map((item) =>
Expand Down
6 changes: 2 additions & 4 deletions designer/server/src/models/forms/privacy-notice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
Expand All @@ -10,10 +11,7 @@ export function privacyNoticyViewModel(metadata, validation) {

return {
form: metadata,
backLink: {
text: 'Back',
href: `/library/${metadata.slug}`
},
backLink: formOverviewBackLink(metadata.slug),
pageTitle,
errorList: buildErrorList(formErrors),
formErrors: validation?.formErrors,
Expand Down
34 changes: 34 additions & 0 deletions designer/server/src/models/forms/submission-guidance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { buildErrorList } from '~/src/common/helpers/build-error-details.js'
import { formOverviewBackLink } from '~/src/models/links.js'

/**
* @param {FormMetadata} metadata
* @param {ValidationFailure<Pick<FormMetadataInput, 'submissionGuidance'>>} [validation]
*/
export function submissionGuidanceViewModel(metadata, validation) {
const pageTitle = 'Tell users what happens after they submit their form'
const { formValues, formErrors } = validation ?? {}

return {
form: metadata,
backLink: formOverviewBackLink(metadata.slug),
pageTitle,
errorList: buildErrorList(formErrors),
formErrors: validation?.formErrors,
formValues: validation?.formValues,
field: {
id: 'submissionGuidance',
name: 'submissionGuidance',
label: {
text: 'What will happen after a user submits a form?'
},
value: formValues?.submissionGuidance ?? metadata.submissionGuidance
},
buttonText: 'Save and continue'
}
}

/**
* @import { FormMetadata, FormMetadataInput } from '@defra/forms-model'
* @import { ValidationFailure } from '~/src/common/helpers/types.js'
*/
48 changes: 48 additions & 0 deletions designer/server/src/models/forms/submission-guidance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { submissionGuidanceViewModel } from '~/src/models/forms/submission-guidance.js'

describe('edit - model - submission - guidance', () => {
const now = new Date()
const authorId = 'f50ceeed-b7a4-47cf-a498-094efc99f8bc'
const authorDisplayName = 'Enrique Chase'

/**
* @satisfies {FormMetadataAuthor}
*/
const author = {
id: authorId,
displayName: authorDisplayName
}

/**
* @satisfies {FormMetadata}
*/
const formMetadata = {
id: '661e4ca5039739ef2902b214',
slug: 'my-form-slug',
title: 'Test form',
organisation: 'Defra',
teamName: 'Defra Forms',
teamEmail: '[email protected]',
submissionGuidance: 'We’ll send you an email to let you know the outcome.',
createdAt: now,
createdBy: author,
updatedAt: now,
updatedBy: author
}

it('should test submission guidance view model', () => {
const result = submissionGuidanceViewModel(formMetadata)
expect(result.pageTitle).toBe(
'Tell users what happens after they submit their form'
)
expect(result.field.id).toBe('submissionGuidance')
expect(result.field.name).toBe('submissionGuidance')
expect(result.field.value).toBe(
'We’ll send you an email to let you know the outcome.'
)
})
})

/**
* @import { FormMetadata, FormMetadataAuthor } from '@defra/forms-model'
*/
30 changes: 30 additions & 0 deletions designer/server/src/models/links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const backToFormOverviewLinkText = 'Back to form overview'
export const backToFormsLibraryLinkText = 'Back to forms library'
export const formsLibraryPath = '/library'

/**
* Path to the form overview page
* @param {string} slug - the form slug
*/
export function formOverviewPath(slug) {
return `${formsLibraryPath}/${slug}`
}

/**
* Back link to the form overview page
* @param {string} slug - the form slug
*/
export function formOverviewBackLink(slug) {
return {
text: backToFormOverviewLinkText,
href: formOverviewPath(slug)
}
}

/**
* Back link to the forms library page
*/
export const formsLibraryBackLink = {
text: backToFormsLibraryLinkText,
href: formsLibraryPath
}
5 changes: 4 additions & 1 deletion designer/server/src/routes/account/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Boom from '@hapi/boom'
import { sessionNames } from '~/src/common/constants/session-names.js'
import { hasUser } from '~/src/common/helpers/auth/get-user-session.js'
import { createUserSession } from '~/src/common/helpers/auth/user-session.js'
import { formsLibraryPath } from '~/src/models/links.js'

export default [
/**
Expand All @@ -24,7 +25,9 @@ export default [
// Add to authentication cookie for session validation
cookieAuth.set({ sessionId: credentials.user.id })

const redirect = yar.flash(sessionNames.redirectTo).at(0) ?? '/library'
const redirect =
yar.flash(sessionNames.redirectTo).at(0) ?? formsLibraryPath

return h.redirect(redirect)
},
options: {
Expand Down
Loading

0 comments on commit 2b8b629

Please sign in to comment.