From 552a7d0346b09253f69d202939e6091201dabba3 Mon Sep 17 00:00:00 2001 From: Enda McCormack Date: Tue, 18 May 2021 13:01:15 +0100 Subject: [PATCH] delete conditional form component --- .../components/ConditionalFormComponent.ts | 187 ------------------ 1 file changed, 187 deletions(-) delete mode 100644 runner/src/server/plugins/engine/components/ConditionalFormComponent.ts diff --git a/runner/src/server/plugins/engine/components/ConditionalFormComponent.ts b/runner/src/server/plugins/engine/components/ConditionalFormComponent.ts deleted file mode 100644 index f9a442bf03..0000000000 --- a/runner/src/server/plugins/engine/components/ConditionalFormComponent.ts +++ /dev/null @@ -1,187 +0,0 @@ -/*import joi from "joi"; -import nunjucks from "nunjucks"; - -import { ListFormComponent } from "./ListFormComponent"; -import { ComponentCollection } from "./ComponentCollection"; -import { - FormData, - FormPayload, - FormSubmissionErrors, - FormSubmissionState, -} from "../types"; -import { FormModel } from "../models";*/ - -//const getSchemaKeys = Symbol("getSchemaKeys"); -// FIXME:- FML -// @ts-ignore -/*export class ConditionalFormComponent extends ListFormComponent { - itemValues: any; - - constructor(def, model: FormModel) { - super(def, model); - - this.itemValues = this.values?.items.map((item) => item.value); - this.createConditionalComponents(def, model); - } - - getFormDataFromState(state: FormSubmissionState) { - const formData = super.getFormDataFromState(state); - if (formData) { - const itemsWithConditionalComponents = this.values?.items.filter( - (item: any) => item.childrenCollection - ); - itemsWithConditionalComponents?.forEach((item: any) => { - const itemFormDataFromState = item.childrenCollection.getFormDataFromState( - state - ); - if ( - itemFormDataFromState && - Object.keys(itemFormDataFromState).length > 0 - ) { - Object.assign(formData, itemFormDataFromState); - } - }); - } - return formData; - } - - getFormSchemaKeys() { - return this[getSchemaKeys]("form"); - } - - getStateFromValidForm(payload: FormPayload) { - const state = super.getStateFromValidForm(payload); - const itemsWithConditionalComponents = this.values?.items.filter( - (item: any) => item.childrenCollection - ); - - const selectedItemsWithConditionalComponents = itemsWithConditionalComponents?.filter( - (item) => { - if (payload[this.name] && Array.isArray(payload[this.name])) { - return payload[this.name].find( - (nestedItem) => item.value === nestedItem - ); - } else { - return item.value === payload[this.name]; - } - } - ); - - // Add selected form data associated with conditionally revealed content to the state. - selectedItemsWithConditionalComponents?.forEach((item: any) => - Object.assign( - state, - item.childrenCollection.getStateFromValidForm(payload) - ) - ); - - // Add null values to the state for unselected form data associated with conditionally revealed content. - // This will allow changes in the visibility of conditionally revealed content to be reflected in state correctly. - const unselectedItemsWithConditionalComponents = itemsWithConditionalComponents?.filter( - (item) => !selectedItemsWithConditionalComponents?.includes(item) - ); - unselectedItemsWithConditionalComponents?.forEach((item: any) => { - const stateFromValidForm = item.childrenCollection.getStateFromValidForm( - payload - ); - Object.values(item.childrenCollection.items) - .filter( - // TODO: type - (conditionalItem: any) => stateFromValidForm[conditionalItem.name] - ) - // TODO: type - .forEach((key: any) => { - const conditionalItemToNull = key.name; - Object.assign(stateFromValidForm, { [conditionalItemToNull]: null }); - }); - Object.assign(state, stateFromValidForm); - }); - - return state; - } - - getStateSchemaKeys() { - return this[getSchemaKeys]("state"); - } - - createConditionalComponents(_def, model) { - const { values } = this; - const items = values?.items; - - const filteredItems = items?.filter( - (item) => item.children && item.children.length > 0 - ); - // Create a collection of conditional components that can be converted to a view model and rendered by Nunjucks - // before primary view model rendering takes place. - filteredItems?.map((item: any) => { - item.childrenCollection = new ComponentCollection(item.children, model); - }); - } - - addConditionalComponents( - item, - itemModel, - formData: FormData, - errors?: FormSubmissionErrors - ) { - // The gov.uk design system Nunjucks examples for conditional reveal reference variables from macros. There does not appear to - // to be a way to do this in JavaScript. As such, render the conditional components with Nunjucks before the main view is rendered. - // The conditional html tag used by the gov.uk design system macro will reference HTML rarther than one or more additional - // gov.uk design system macros. - if (item.childrenCollection) { - itemModel.conditional = { - html: nunjucks.render("conditional-components.html", { - components: item.childrenCollection.getViewModel(formData, errors), - }), - }; - } - return itemModel; - } - - [getSchemaKeys](schemaType) { - const schemaName = `${schemaType}Schema`; - const schemaKeysFunctionName = `get${schemaType - .substring(0, 1) - .toUpperCase()}${schemaType.substring(1)}SchemaKeys`; - const filteredItems = this.values?.items.filter( - (item: any) => item.childrenCollection - ); - const conditionalName = this.name; - const schemaKeys = { [conditionalName]: this[schemaName] }; - const schema = this[schemaName]; - // All conditional component values are submitted regardless of their visibilty. - // As such create Joi validation rules such that: - // a) When a conditional component is visible it is required. - // b) When a conditional component is not visible it is optional. - filteredItems?.forEach((item: any) => { - const conditionalSchemaKeys = item.childrenCollection[ - schemaKeysFunctionName - ](); - // Iterate through the set of components handled by conditional reveal adding Joi validation rules - // based on whether or not the component controlling the conditional reveal is selected. - Object.keys(conditionalSchemaKeys).forEach((key) => { - Object.assign(schemaKeys, { - [key]: joi.alternatives().when(conditionalName, { - is: item.value, - then: conditionalSchemaKeys[key].required(), - // If multiple checkboxes are selected their values will be held in an array. In this - // case conditionally revealed content is required to be entered if the controlliing - // checkbox value is a member of the array of selected checkbox values. - otherwise: joi.alternatives().when(conditionalName, { - is: joi - .array() - .items(schema.only(item.value), joi.any()) - .required(), - then: conditionalSchemaKeys[key].required(), - otherwise: conditionalSchemaKeys[key] - .optional() - .allow("") - .allow(null), - }), - }), - }); - }); - }); - return schemaKeys; - } -}*/