Skip to content

Commit

Permalink
Add class for step dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hungoptimizely committed Dec 13, 2023
1 parent c1d9956 commit 75005a2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isNull, isNullOrEmpty } from "../helpers";
import { getConcatString } from "../helpers/dependencyHelper";
import { ConditionFunctionType } from "../models";

export type ConditionFunctionRecord = Record<string, (actualValue: Object, patternOfExpected: string) => boolean>;
export type ConditionFunctionRecord = Record<string, (actualValue: any, patternOfExpected: string) => boolean>;

export const ConditionFunctions: ConditionFunctionRecord = {
[ConditionFunctionType.Contains]: Contains,
Expand All @@ -17,7 +17,7 @@ export const ConditionFunctions: ConditionFunctionRecord = {
* @param dependencyFieldValue
* @returns
*/
function Equals(actualValue: Object, dependencyFieldValue: string): boolean {
function Equals(actualValue: any, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue === dependencyFieldValue;
Expand All @@ -28,7 +28,7 @@ function Equals(actualValue: Object, dependencyFieldValue: string): boolean {
* @param dependencyFieldValue
* @returns
*/
function NotEquals(actualValue: Object, dependencyFieldValue: string): boolean {
function NotEquals(actualValue: any, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue !== dependencyFieldValue;
Expand All @@ -39,7 +39,7 @@ function NotEquals(actualValue: Object, dependencyFieldValue: string): boolean {
* @param dependencyFieldValue
* @returns
*/
function Contains(actualValue: Object, dependencyFieldValue: string): boolean {
function Contains(actualValue: any, dependencyFieldValue: string): boolean {
const _actualValue = isNull(actualValue) ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue.indexOf(dependencyFieldValue) >= 0;
Expand All @@ -50,7 +50,7 @@ function Contains(actualValue: Object, dependencyFieldValue: string): boolean {
* @param dependencyFieldValue
* @returns
*/
function NotContains(actualValue: Object, dependencyFieldValue: string): boolean {
function NotContains(actualValue: any, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
const actualValueNull = isNullOrEmpty(_actualValue)
const dependencyFieldValueNull = isNullOrEmpty(dependencyFieldValue)
Expand All @@ -64,7 +64,7 @@ function NotContains(actualValue: Object, dependencyFieldValue: string): boolean
* @param patternOfExpected
* @returns
*/
function MatchRegularExpression(actualValue: Object, patternOfExpected: string): boolean {
function MatchRegularExpression(actualValue: any, patternOfExpected: string): boolean {
var regex = new RegExp(patternOfExpected, "igm");
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
return isNullOrEmpty(patternOfExpected) || (!isNullOrEmpty(patternOfExpected) && regex.test(_actualValue));
Expand Down
3 changes: 2 additions & 1 deletion src/@episerver/forms-sdk/src/form-step/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./stepBuilder"
export * from "./stepBuilder";
export * from "./stepDependCondition";
84 changes: 84 additions & 0 deletions src/@episerver/forms-sdk/src/form-step/stepDependCondition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ConditionFunctions } from "../form-depend-conditions/ConditionFunctions";
import { FormStorage } from "../form-storage";
import { isInArray } from "../helpers";
import { FormContainer, FormStep } from "../models";

/**
* Class to help check step is satisfy depend condition
*/
export class StepDependCondition {
readonly _form: FormContainer
readonly _formStorage: FormStorage
readonly _inactiveElements: string[]
/**
* The constructor of class StepDependCondition
* @param form Current form container
* @param inactiveElements List of inactive elements. This param can passed from {@link FormState}.
*/
constructor(form: FormContainer, inactiveElements: string[]){
this._form = form;
this._formStorage = new FormStorage(form);
this._inactiveElements = inactiveElements;
}

/**
* Check if step is satisfy the depend condition
* @param stepIndex
* @returns
*/
isSatisfied (stepIndex: number): boolean {
let step = this._form.steps[stepIndex]?.formStep as FormStep;

if (!step) {
return false;
}

let dependField = step?.properties?.dependField,
storedData = this._formStorage.loadFormDataFromStorage().filter(fs => fs.elementKey === dependField?.key)[0],
funcOfDependCondition = ConditionFunctions[step?.properties?.dependCondition];

if (!dependField || !funcOfDependCondition || !storedData) { // no input to check, consider it's OK
return true;
}

if(!dependField && isInArray(dependField, this._inactiveElements)){
return funcOfDependCondition(null, step?.properties?.dependValue);
}

return funcOfDependCondition(storedData?.value, step?.properties?.dependValue);
}

/**
* Rescusive finding next step to display in form.
* @param currentStepIndex
* @returns
*/
findNextStep (currentStepIndex: number): number | undefined {
let nextStepIndex = currentStepIndex + 1;
let nextStep = this._form.steps[nextStepIndex];

if(nextStep){
return this.isSatisfied(nextStepIndex)
? nextStepIndex
: this.findNextStep(nextStepIndex);
}
return undefined;
}

/**
* Find previous step to display in form.
* @param currentStepIndex
* @returns
*/
findPreviousStep (currentStepIndex: number): number | undefined {
let prevStepIndex = currentStepIndex - 1;
let prevStep = this._form.steps[prevStepIndex];

if(prevStep){
return this.isSatisfied(prevStepIndex)
? prevStepIndex
: this.findPreviousStep(prevStepIndex);
}
return undefined;
}
}
2 changes: 1 addition & 1 deletion src/@episerver/forms-sdk/src/models/elements/FormStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface FormStep extends FormElementBase {
export interface FormStepProperties extends FormElementPropertiesBase {
attachedContentLink: string
dependField: DependField
dependCondition: number
dependCondition: string
dependValue: string
}

Expand Down

0 comments on commit 75005a2

Please sign in to comment.