Skip to content

Commit

Permalink
Refactor ConfirmMessage function
Browse files Browse the repository at this point in the history
  • Loading branch information
epi-qang2 committed Jan 26, 2024
1 parent b668d08 commit 9f292fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
21 changes: 12 additions & 9 deletions src/@episerver/forms-react/src/components/FormBody.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useEffect, useRef } from "react";
import { useForms } from "../context/store";
import { StepHelper, FormContainer, FormSubmitter, IdentityInfo, isInArray, isNull, isNullOrEmpty, FormSubmitModel, FormSubmitResult, SubmitButton, FormCache, FormConstants, ProblemDetail, StepDependCondition, buildConfirmMessage } from "@episerver/forms-sdk";
import { StepHelper, FormContainer, FormSubmitter, IdentityInfo, isInArray, isNull,
isNullOrEmpty, FormSubmitModel, FormSubmitResult, SubmitButton, FormCache,
FormConstants, ProblemDetail, StepDependCondition, getConfirmationData } from "@episerver/forms-sdk";
import { RenderElementInStep } from "./RenderElementInStep";
import { DispatchFunctions } from "../context/dispatchFunctions";
import { FormStepNavigation } from "./FormStepNavigation";
Expand All @@ -27,7 +29,7 @@ export const FormBody = (props: FormBodyProps) => {
const statusDisplay = useRef<string>("hide");

const formCache = new FormCache();
const localFormCache = new FormCache(window.localStorage);
const localFormCache = new FormCache(window.sessionStorage);
const currentStepIndex = formContext?.currentStepIndex ?? 0;

//TODO: these variables should be get from api or sdk
Expand Down Expand Up @@ -61,18 +63,20 @@ export const FormBody = (props: FormBodyProps) => {
submissionWarning.current = !isNullOrEmpty(error);
message.current = error;
}

const handleConfirm = () => {
const form = formContext?.formContainer ?? {} as FormContainer;
const confirmationMessage = form.properties.confirmationMessage;
const confirmationMessage = form.properties.confirmationMessage ?? "";
let confimStatus = true;

if (!isNullOrEmpty(confirmationMessage) && form.properties.showSummarizedData) {
if (form.properties.showSummarizedData) {
const data = formContext?.formSubmissions ?? []
const confirmationMessageWithData = buildConfirmMessage(confirmationMessage, data, form)
if (!isNullOrEmpty(confirmationMessage)) {
confimStatus = confirm(confirmationMessageWithData);
const messageData = getConfirmationData(data, form)
const showConfirmationMessage = !(isNullOrEmpty(confirmationMessage) && isNullOrEmpty(messageData))
if (showConfirmationMessage) {
confimStatus = confirm(confirmationMessage + "\n\n" + messageData);
}
}
}

return confimStatus
}
Expand All @@ -94,7 +98,6 @@ export const FormBody = (props: FormBodyProps) => {

let isLastStep = currentStepIndex === form.steps.length - 1;


//filter submissions by active elements and current step
let formSubmissions = (formContext?.formSubmissions ?? [])
.filter(fs => !isInArray(fs.elementKey, formContext?.dependencyInactiveElements ?? []) && stepHelper.isInCurrentStep(fs.elementKey, currentStepIndex));
Expand Down
11 changes: 4 additions & 7 deletions src/@episerver/forms-sdk/src/helpers/buildConfirmationMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ export function getValueAsString(element: FormSubmission) {
return `${value}`
}

export function buildConfirmMessage(confirmationMessage: string, data: FormSubmission[], form: FormContainer, fieldsToIgnore?: string[]): string {
export function getConfirmationData(data: FormSubmission[], form: FormContainer, fieldsToIgnore?: string[]): string {
let message = "";
let _fieldsToIgnore = fieldsToIgnore ?? FieldsToIgnore;

data.forEach(element => {
let formElement = form.formElements.find(fe => fe.key === element.elementKey)
let value = getValueAsString(element)
if (formElement && !isNullOrEmpty(value) && _fieldsToIgnore.indexOf(formElement.contentType) !== -1) {
if (formElement && !isNullOrEmpty(value) && _fieldsToIgnore.indexOf(formElement.contentType) === -1) {
message += `${formElement.displayName}: ${value}${"\n"}`;
}
});

if (isNullOrEmpty(message) && isNullOrEmpty(confirmationMessage)) {
return message
}

return confirmationMessage + "\n" + message
return message
}

0 comments on commit 9f292fc

Please sign in to comment.