Skip to content

Commit

Permalink
Remove field value DataType formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Oct 28, 2024
1 parent 6a35e32 commit 2c6a103
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 302 deletions.
11 changes: 1 addition & 10 deletions src/server/plugins/engine/components/ComponentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import joi, {
type StringSchema
} from 'joi'

import {
DataType,
type ViewModel
} from '~/src/server/plugins/engine/components/types.js'
import { type ViewModel } from '~/src/server/plugins/engine/components/types.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import { answerFromDetailItem } from '~/src/server/plugins/engine/pageControllers/SummaryPageController.js'
import {
type FormPayload,
type FormSubmissionErrors
Expand All @@ -27,11 +23,6 @@ export class ComponentBase {
options?: Extract<ComponentDef, { options: object }>['options']

isFormComponent = false

/**
* This is passed onto webhooks, see {@link answerFromDetailItem}
*/
dataType: DataType = DataType.Text
model: FormModel

/** joi schemas based on a component defined in the form JSON. This validates a user's answer and is generated from {@link ComponentDef} */
Expand Down
6 changes: 1 addition & 5 deletions src/server/plugins/engine/components/DatePartsField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { ComponentCollection } from '~/src/server/plugins/engine/components/Comp
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import { NumberField } from '~/src/server/plugins/engine/components/NumberField.js'
import { optionalText } from '~/src/server/plugins/engine/components/constants.js'
import {
DataType,
type DateInputItem
} from '~/src/server/plugins/engine/components/types.js'
import { type DateInputItem } from '~/src/server/plugins/engine/components/types.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import {
type FormPayload,
Expand All @@ -22,7 +19,6 @@ import {
export class DatePartsField extends FormComponent {
declare options: DatePartsFieldComponent['options']
children: ComponentCollection
dataType: DataType = DataType.Date

constructor(def: DatePartsFieldComponent, model: FormModel) {
super(def, model)
Expand Down
19 changes: 17 additions & 2 deletions src/server/plugins/engine/components/FileUploadField.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type FileUploadFieldComponent } from '@defra/forms-model'
import joi, { type ArraySchema } from 'joi'

import { config } from '~/src/config/index.js'
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import { DataType } from '~/src/server/plugins/engine/components/types.js'
import { filesize } from '~/src/server/plugins/engine/helpers.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import {
Expand All @@ -16,6 +16,8 @@ import {
type FormSubmissionState
} from '~/src/server/plugins/engine/types.js'

const designerUrl = config.get('designerUrl')

export const uploadIdSchema = joi.string().uuid().required()

export const fileSchema = joi
Expand Down Expand Up @@ -85,7 +87,6 @@ export const formItemSchema = itemSchema.append({
export class FileUploadField extends FormComponent {
declare options: FileUploadFieldComponent['options']
declare schema: FileUploadFieldComponent['schema']
dataType: DataType = DataType.File

declare formSchema: ArraySchema<object>
declare stateSchema: ArraySchema<object>
Expand Down Expand Up @@ -135,6 +136,20 @@ export class FileUploadField extends FormComponent {
return `You uploaded ${count} file${count !== 1 ? 's' : ''}`
}

getMarkdownStringFromState(state: FormSubmissionState) {
const values = this.getFormValueFromState(state)
const count = values?.length

const bullets = (values ?? [])
.map(({ status }) => {
const { filename, fileId } = status.form.file
return `* [${filename}](${designerUrl}/file-download/${fileId})`
})
.join('\n')

return `${count} file${count !== 1 ? 's' : ''} uploaded:\n\n${bullets}`
}

getViewModel(payload: FormPayload, errors?: FormSubmissionErrors) {
const { options } = this

Expand Down
5 changes: 5 additions & 0 deletions src/server/plugins/engine/components/FormComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ export class FormComponent extends ComponentBase {
typeof value === 'boolean'
)
}

getMarkdownStringFromState(state: FormSubmissionState) {
const formatted = this.getDisplayStringFromState(state)
return `\`\`\`\n${formatted}\n\`\`\``
}
}
6 changes: 1 addition & 5 deletions src/server/plugins/engine/components/ListFormComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import joi, {
} from 'joi'

import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import {
DataType,
type ListItem
} from '~/src/server/plugins/engine/components/types.js'
import { type ListItem } from '~/src/server/plugins/engine/components/types.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import {
type FormPayload,
Expand Down Expand Up @@ -48,7 +45,6 @@ export class ListFormComponent extends FormComponent {

list?: List
listType: List['type'] = 'string'
dataType: DataType = DataType.List

get items(): Item[] {
return this.list?.items ?? []
Expand Down
6 changes: 1 addition & 5 deletions src/server/plugins/engine/components/MonthYearField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { ComponentCollection } from '~/src/server/plugins/engine/components/Comp
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import { NumberField } from '~/src/server/plugins/engine/components/NumberField.js'
import { optionalText } from '~/src/server/plugins/engine/components/constants.js'
import {
DataType,
type DateInputItem
} from '~/src/server/plugins/engine/components/types.js'
import { type DateInputItem } from '~/src/server/plugins/engine/components/types.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import {
type FormPayload,
Expand All @@ -20,7 +17,6 @@ import {
export class MonthYearField extends FormComponent {
declare options: MonthYearFieldComponent['options']
children: ComponentCollection
dataType: DataType = DataType.MonthYear

constructor(def: MonthYearFieldComponent, model: FormModel) {
super(def, model)
Expand Down
9 changes: 0 additions & 9 deletions src/server/plugins/engine/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,3 @@ export interface ComponentViewModel {
isFormComponent: boolean
model: ViewModel
}

export enum DataType {
List = 'list',
Text = 'text',
Date = 'date',
MonthYear = 'monthYear',
Number = 'number',
File = 'file'
}
70 changes: 44 additions & 26 deletions src/server/plugins/engine/models/SummaryViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { RepeatPageController } from '~/src/server/plugins/engine/pageController
import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js'
import {
type FormState,
type FormSubmissionState
type FormSubmissionState,
type RepeatState
} from '~/src/server/plugins/engine/types.js'
import {
type FormQuery,
type FormRequest,
type FormRequestPayload
} from '~/src/server/routes/types.js'
Expand Down Expand Up @@ -131,11 +133,13 @@ export class SummaryViewModel {
)

sectionPages.forEach((page) => {
const { formItems } = page.components

if (page instanceof RepeatPageController) {
addRepeaterItem(page, request, model, state, items)
} else {
for (const component of page.components.formItems) {
const item = Item(component, state, page, model)
for (const component of formItems) {
const item = Item(page, model, state, component)
if (items.find((cbItem) => cbItem.name === item.name)) return
items.push(item)
}
Expand Down Expand Up @@ -178,37 +182,45 @@ function addRepeaterItem(
state: FormSubmissionState,
items: DetailItem[]
) {
const { basePath } = model
const { formItems } = page.components
const { options } = page.repeat
const { name, title } = options
const repeatSummaryPath = page.getSummaryPath(request)
const path = `/${model.basePath}${page.path}`

const rawValue = page.getListFromState(state)
const hasItems = rawValue.length > 0
const value = hasItems ? rawValue.length.toString() : '0'
const url = redirectUrl(hasItems ? repeatSummaryPath : path, {
returnUrl: redirectUrl(`/${model.basePath}/summary`)

const pagePath = hasItems
? page.getSummaryPath(request)
: `/${basePath}${page.path}`

// Path to change link
const url = redirectUrl(pagePath, {
returnUrl: redirectUrl(page.defaultNextPath)
})

const subItems: DetailItem[][] = []

rawValue.forEach((itemState) => {
const sub: DetailItem[] = []
for (const component of page.components.formItems) {
const item = Item(component, itemState, page, model)

for (const component of formItems) {
const item = Item(page, model, itemState, component)
if (sub.find((cbItem) => cbItem.name === item.name)) return
sub.push(item)
}

subItems.push(sub)
})

items.push({
name,
path: page.path,
label: hasItems ? `${title}s added` : title,
value: `You added ${value} ${title}${value === '1' ? '' : 's'}`,
name: options.name,
title: options.title,
label: hasItems ? `${options.title}s added` : options.title,
value: `You added ${value} ${options.title}${value === '1' ? '' : 's'}`,
rawValue,
page,
url,
title,
subItems
})
}
Expand All @@ -217,23 +229,29 @@ function addRepeaterItem(
* Creates an Item object for Details
*/
function Item(
component: FormComponentFieldClass,
state: FormState,
page: PageControllerClass,
model: FormModel,
params: { returnUrl: string } = {
returnUrl: redirectUrl(`/${model.basePath}/summary`)
}
): DetailItem {
state: FormState | RepeatState,
component: FormComponentFieldClass,
params?: FormQuery
) {
const { basePath } = model

const pagePath = `/${basePath}${page.path}`
const returnUrl = redirectUrl(page.getSummaryPath())

// Path to change link
const url = redirectUrl(pagePath, { returnUrl, ...params })

return {
name: component.name,
path: page.path,
title: component.title,
label: component.title,
value: component.getDisplayStringFromState(state),
markdownValue: component.getMarkdownStringFromState(state),
rawValue: component.getFormValueFromState(state),
url: redirectUrl(`/${model.basePath}${page.path}`, params),
type: component.type,
title: component.title,
dataType: component.dataType
}
page,
url
} satisfies DetailItem
}
Loading

0 comments on commit 2c6a103

Please sign in to comment.