Skip to content

Commit

Permalink
Simplify types for collection view models
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Oct 28, 2024
1 parent 5b9d8b1 commit 733db2d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
8 changes: 5 additions & 3 deletions src/server/plugins/engine/components/ComponentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type ComponentFieldClass,
type FormComponentFieldClass
} from '~/src/server/plugins/engine/components/helpers.js'
import { type ComponentCollectionViewModel } from '~/src/server/plugins/engine/components/types.js'
import { type ComponentViewModel } 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 @@ -114,8 +114,10 @@ export class ComponentCollection {
payload: FormPayload,
errors?: FormSubmissionErrors,
conditions?: FormModel['conditions']
): ComponentCollectionViewModel {
const result = this.items.map((item) => {
) {
const { items } = this

const result: ComponentViewModel[] = items.map((item) => {
return {
type: item.type,
isFormComponent: item.isFormComponent,
Expand Down
25 changes: 5 additions & 20 deletions src/server/plugins/engine/components/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
type ContentComponentsDef,
type FormComponentsDef,
type Item
} from '@defra/forms-model'
import { type ComponentType, type Item } from '@defra/forms-model'

import { type FormValue } from '~/src/server/plugins/engine/types.js'

Expand Down Expand Up @@ -88,7 +84,7 @@ export interface ViewModel extends Record<string, unknown> {
classes?: string
attributes?: string | Record<string, string>
}
children?: ComponentCollectionViewModel
children?: ComponentViewModel[]
upload?: {
count: number
pendingCount: number
Expand All @@ -105,23 +101,12 @@ export interface FileUploadSummaryRow {
uploadId: string
}

export interface FormComponentViewModel {
type: FormComponentsDef['type']
isFormComponent: true
export interface ComponentViewModel {
type: ComponentType
isFormComponent: boolean
model: ViewModel
}

export interface ContentComponentViewModel {
type: ContentComponentsDef['type']
isFormComponent: false
model: ViewModel
}

export type ComponentCollectionViewModel = (
| FormComponentViewModel
| ContentComponentViewModel
)[]

export enum DataType {
List = 'list',
Text = 'text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
tempItemSchema,
tempStatusSchema
} from '~/src/server/plugins/engine/components/FileUploadField.js'
import { type FormComponentViewModel } from '~/src/server/plugins/engine/components/types.js'
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
import { PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js'
import {
Expand Down Expand Up @@ -204,8 +203,7 @@ export class FileUploadPageController extends PageController {
const components = viewModel.components

const [fileUploadComponent] = components.filter(
(component): component is FormComponentViewModel =>
component.model.id === name
(component) => component.model.id === name
)

const id = components.indexOf(fileUploadComponent)
Expand Down
11 changes: 4 additions & 7 deletions src/server/plugins/engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { type Item } from '@defra/forms-model'
import { type ResponseObject } from '@hapi/hapi'

import {
type ComponentCollectionViewModel,
type FormComponentViewModel
} from '~/src/server/plugins/engine/components/types.js'
import { type ComponentViewModel } from '~/src/server/plugins/engine/components/types.js'
import {
type FileUploadPageController,
type PageController
Expand Down Expand Up @@ -177,7 +174,7 @@ export interface PageViewModelBase {
pageTitle: string
sectionTitle?: string
showTitle: boolean
components: ComponentCollectionViewModel
components: ComponentViewModel[]
errors?: FormSubmissionErrors
isStartPage: boolean
startPage?: ResponseObject
Expand All @@ -195,8 +192,8 @@ export interface FileUploadPageViewModel extends PageViewModelBase {
page: FileUploadPageController
path: string
formAction?: string
fileUploadComponent: FormComponentViewModel
preUploadComponents: ComponentCollectionViewModel
fileUploadComponent: ComponentViewModel
preUploadComponents: ComponentViewModel[]
}

export type PageViewModel = PageViewModelBase | FileUploadPageViewModel

0 comments on commit 733db2d

Please sign in to comment.