Skip to content

Commit

Permalink
Merge pull request #1420 from dpc-sdp/feature/SD-604-unsupported-inpu…
Browse files Browse the repository at this point in the history
…t-message

[SD-604] - Changed the 'unsupported input message' in webforms to only show up in draft mode
  • Loading branch information
dylankelly authored Jan 13, 2025
2 parents cbc7f23 + a8232e0 commit d546ab2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: Forms, extended
And a field labelled "Extended" should exist with the CSS class ".rpl-form__input--type-item"

@mockserver
Scenario: Unsupported input
Scenario: Unsupported input (draft page)
Given I visit the page "/webform"
Then the landing page component "TideLandingPageWebForm" should exist
And an input of type "signature" is not yet supported
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { TideDynamicPageComponent } from '@dpc-sdp/ripple-tide-api/types'
import { TidePageApi } from '@dpc-sdp/ripple-tide-api'
import type { TideWebform, ApiField } from '@dpc-sdp/ripple-tide-webform/types'
import type {
TideWebform,
ApiField,
ApiPage
} from '@dpc-sdp/ripple-tide-webform/types'
import {
getFormSchemaFromMapping,
getCaptchaSettings
} from '@dpc-sdp/ripple-tide-webform/mapping'

const componentMapping = async (field: ApiField, tidePageApi: TidePageApi) => {
const componentMapping = async (
field: ApiField,
page: ApiPage,
tidePageApi: TidePageApi
) => {
return {
title: field.field_paragraph_title,
formId: field.field_paragraph_webform.meta.drupal_internal__target_id,
Expand All @@ -24,22 +32,22 @@ const componentMapping = async (field: ApiField, tidePageApi: TidePageApi) => {
'We are experiencing a server error. Please try again, otherwise contact us.',
schema: await getFormSchemaFromMapping(
field.field_paragraph_webform,
page,
tidePageApi
),
captchaConfig: getCaptchaSettings(field.field_paragraph_webform)
}
}
export const webformMapping = async (
field: ApiField,
// @ts-expect-error unused
page,
page: ApiPage,
tidePageApi: TidePageApi
): Promise<TideDynamicPageComponent<TideWebform>> => {
return {
component: 'TideLandingPageWebForm',
id: field.drupal_internal__id,
title: field.field_paragraph_title,
props: await componentMapping(field, tidePageApi)
props: await componentMapping(field, page, tidePageApi)
}
}

Expand Down
28 changes: 19 additions & 9 deletions packages/ripple-tide-webform/mapping/webforms-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getValidationAndConditionals
} from './../server/webform-utils'
import { getAdvancedAddressMapping } from './webforms-address'
import type { TideWebformElement, ApiWebForm } from './../types'
import type { TideWebformElement, ApiWebForm, ApiPage } from './../types'

interface CustomInputsConfig {
[key: string]: {
Expand All @@ -19,12 +19,15 @@ const customInputs: CustomInputsConfig = appConfig.customInputs || {}

export const getFormSchemaFromMapping = async (
webform: ApiWebForm,
page: ApiPage,
tidePageApi: TidePageApi
): Promise<FormKitSchemaNode[]> => {
const elements: TideWebformElement[] = webform.elements || []
const fields = []
const formId = webform.drupal_internal__id

const isDraftMode = page.moderation_state === 'draft'

for (const [fieldMachineName, fieldData] of Object.entries(elements)) {
let mappedField
const field: TideWebformElement = { ...fieldData, formId }
Expand Down Expand Up @@ -347,17 +350,24 @@ export const getFormSchemaFromMapping = async (
fieldKey
)
} else {
mappedField = {
$el: 'div',
attrs: {
class: 'rpl-form__outer rpl-form__input--unsupported'
},
children: [`"${field['#type']}" is not yet supported`]
}
// For unsupported fields, render a message to users when previewing a draft page
mappedField = isDraftMode
? {
$el: 'div',
attrs: {
class: 'rpl-form__outer rpl-form__input--unsupported'
},
children: [
`The "${field['#type']}" field is not supported (this message will only show in draft mode).`
]
}
: null
}
}

fields.push(mappedField)
if (mappedField) {
fields.push(mappedField)
}
}

return fields
Expand Down
4 changes: 2 additions & 2 deletions packages/ripple-tide-webform/server/api/tide/webform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class TideWebformApi extends TideApiBase {
mapping: {
_src: (field: any) =>
process.env.NODE_ENV === 'development' ? field : undefined,
schema: async (field: any, page, tidePageApi: TidePageApi) =>
await getFormSchemaFromMapping(field, tidePageApi),
schema: async (field: any, page: any, tidePageApi: TidePageApi) =>
await getFormSchemaFromMapping(field, page, tidePageApi),
captchaConfig: (field: any) => getCaptchaSettings(field)
},
includes: []
Expand Down
4 changes: 4 additions & 0 deletions packages/ripple-tide-webform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export interface ApiField {
field_paragraph_webform: ApiWebForm
}

export interface ApiPage {
moderation_state?: string
}

export enum CaptchaType {
RECAPTCHA_V2 = 'google_recaptcha_v2',
RECAPTCHA_V3 = 'google_recaptcha_v3',
Expand Down

0 comments on commit d546ab2

Please sign in to comment.