Skip to content

Commit 37ed212

Browse files
Merge pull request #27 from sampension/develop
Update Cache and error
2 parents 2eb3ef0 + bf2ba14 commit 37ed212

File tree

8 files changed

+234
-113
lines changed

8 files changed

+234
-113
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ on:
88
- release/**
99
pull_request:
1010
branches:
11-
- "*"
1211
- main
13-
- feature/**
14-
- release/**
12+
- develop
1513

1614
permissions:
1715
contents: write

src/@episerver/forms-react/src/Form.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
color: $text-color;
4444
}
4545

46-
.Form__Warning__Message {
47-
background-color: $warning-background-color;
48-
color: $warning-foreground-color;
49-
}
46+
// .Form__Warning__Message {
47+
// background-color: $warning-background-color;
48+
// color: $warning-foreground-color;
49+
// }
5050

5151
.Form__Readonly__Message{
5252
display: block;

src/@episerver/forms-react/src/components/FormBody.tsx

Lines changed: 132 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useEffect, useRef } from 'react';
2-
import { useForms } from '../context/store';
1+
import React, { useEffect, useRef } from "react";
2+
import { useForms } from "../context/store";
33
import {
44
StepHelper,
55
FormContainer,
@@ -16,10 +16,10 @@ import {
1616
ProblemDetail,
1717
StepDependCondition,
1818
getConfirmationData,
19-
} from '@episerver/forms-sdk';
20-
import { RenderElementInStep } from './RenderElementInStep';
21-
import { DispatchFunctions } from '../context/dispatchFunctions';
22-
import { FormStepNavigation } from './FormStepNavigation';
19+
} from "@episerver/forms-sdk";
20+
import { RenderElementInStep } from "./RenderElementInStep";
21+
import { DispatchFunctions } from "../context/dispatchFunctions";
22+
import { FormStepNavigation } from "./FormStepNavigation";
2323

2424
interface FormBodyProps {
2525
identityInfo?: IdentityInfo;
@@ -39,13 +39,16 @@ export const FormBody = (props: FormBodyProps) => {
3939
const currentPageUrl = props.currentPageUrl ?? window.location.href;
4040

4141
const formTitleId = `${form.key}_label`;
42-
const statusMessage = useRef<string>('');
43-
const statusDisplay = useRef<string>('hide');
42+
const statusMessage = useRef<string>("");
43+
const statusDisplay = useRef<string>("hide");
4444

4545
const showForm = useRef<boolean>(true);
4646

47-
const formCache = new FormCache();
48-
const localFormCache = new FormCache(window.localStorage);
47+
const formCache = new FormCache(undefined, props.identityInfo?.username);
48+
const localFormCache = new FormCache(
49+
window.localStorage,
50+
props.identityInfo?.username
51+
);
4952
const currentStepIndex = formContext?.currentStepIndex ?? 0;
5053

5154
// @sampension - check for redirect to page after submit to prevent rendering of (default) success message before redirect
@@ -57,24 +60,37 @@ export const FormBody = (props: FormBodyProps) => {
5760
isProgressiveSubmit = useRef<boolean>(false),
5861
isSuccess = useRef<boolean>(false),
5962
submissionWarning = useRef<boolean>(false),
60-
message = useRef<string>(''),
63+
message = useRef<string>(""),
6164
submissionStorageKey = FormConstants.FormSubmissionId + form.key,
62-
isStepValidToDisplay = stepDependCondition.isStepValidToDisplay(currentStepIndex, currentPageUrl),
65+
isStepValidToDisplay = stepDependCondition.isStepValidToDisplay(
66+
currentStepIndex,
67+
currentPageUrl
68+
),
6369
isMalFormSteps = stepHelper.isMalFormSteps();
6470

6571
// @sampension - added check for redirect to page
66-
if ((isFormFinalized.current || isProgressiveSubmit.current) && isSuccess.current && !shouldRedirectToPage) {
67-
statusDisplay.current = 'Form__Success__Message';
68-
statusMessage.current = form.properties.submitSuccessMessage ?? message.current;
69-
} else if ((submissionWarning.current || !isSuccess.current) && !isNullOrEmpty(message.current)) {
70-
statusDisplay.current = 'Form__Warning__Message';
72+
if (
73+
(isFormFinalized.current || isProgressiveSubmit.current) &&
74+
isSuccess.current &&
75+
!shouldRedirectToPage
76+
) {
77+
statusDisplay.current = "Form__Success__Message";
78+
statusMessage.current =
79+
form.properties.submitSuccessMessage ?? message.current;
80+
} else if (
81+
(submissionWarning.current || !isSuccess.current) &&
82+
!isNullOrEmpty(message.current)
83+
) {
84+
statusDisplay.current = "Form__Warning__Message";
7185
statusMessage.current = message.current;
7286
} else {
73-
statusDisplay.current = 'hide';
74-
statusMessage.current = '';
87+
statusDisplay.current = "hide";
88+
statusMessage.current = "";
7589
}
7690

77-
const validationCssClass = validateFail.current ? 'ValidationFail' : 'ValidationSuccess';
91+
const validationCssClass = validateFail.current
92+
? "ValidationFail"
93+
: "ValidationSuccess";
7894

7995
const showError = (error: string) => {
8096
submissionWarning.current = !isNullOrEmpty(error);
@@ -83,15 +99,22 @@ export const FormBody = (props: FormBodyProps) => {
8399

84100
const handleConfirm = () => {
85101
const form = formContext?.formContainer ?? ({} as FormContainer);
86-
const confirmationMessage = form.properties.confirmationMessage ?? '';
102+
const confirmationMessage = form.properties.confirmationMessage ?? "";
87103
let confimStatus = true;
88104

89105
if (form.properties.showSummarizedData) {
90106
const data = formContext?.formSubmissions ?? [];
91-
const messageData = getConfirmationData(data, form, currentStepIndex, inactiveElements);
92-
const showConfirmationMessage = !(isNullOrEmpty(confirmationMessage) && isNullOrEmpty(messageData));
107+
const messageData = getConfirmationData(
108+
data,
109+
form,
110+
currentStepIndex,
111+
inactiveElements
112+
);
113+
const showConfirmationMessage = !(
114+
isNullOrEmpty(confirmationMessage) && isNullOrEmpty(messageData)
115+
);
93116
if (showConfirmationMessage) {
94-
confimStatus = confirm(confirmationMessage + '\n\n' + messageData);
117+
confimStatus = confirm(confirmationMessage + "\n\n" + messageData);
95118
}
96119
}
97120

@@ -101,13 +124,18 @@ export const FormBody = (props: FormBodyProps) => {
101124
const handleSubmit = (e: any) => {
102125
e.preventDefault();
103126

104-
if (!form.properties.allowAnonymousSubmission && isNullOrEmpty(formContext?.identityInfo?.accessToken)) {
127+
if (
128+
!form.properties.allowAnonymousSubmission &&
129+
isNullOrEmpty(formContext?.identityInfo?.accessToken)
130+
) {
105131
return;
106132
}
107133

108134
//Find submit button, if found then check property 'finalizeForm' of submit button. Otherwise, button Next/Previous was clicked.
109135
const buttonId = e.nativeEvent.submitter?.id;
110-
const submitButton = form.formElements.find((fe) => fe.key === buttonId) as SubmitButton;
136+
const submitButton = form.formElements.find(
137+
(fe) => fe.key === buttonId
138+
) as SubmitButton;
111139
if (!isNull(submitButton)) {
112140
//when submitting by SubmitButton, isProgressiveSubmit default is true
113141
isProgressiveSubmit.current = true;
@@ -122,15 +150,20 @@ export const FormBody = (props: FormBodyProps) => {
122150

123151
//filter submissions by active elements and current step
124152
const formSubmissions = (formContext?.formSubmissions ?? []).filter(
125-
(fs) => !isInArray(fs.elementKey, inactives) && stepHelper.isInCurrentStep(fs.elementKey, currentStepIndex)
153+
(fs) =>
154+
!isInArray(fs.elementKey, inactives) &&
155+
stepHelper.isInCurrentStep(fs.elementKey, currentStepIndex)
126156
);
127157

128158
//validate all submission data before submit
129159
const formValidationResults = formSubmitter.doValidate(formSubmissions);
130160
dispatchFunctions.updateAllValidation(formValidationResults);
131161

132162
//set focus on the 1st invalid element of current step
133-
const invalid = stepHelper.getFirstInvalidElement(formValidationResults, currentStepIndex);
163+
const invalid = stepHelper.getFirstInvalidElement(
164+
formValidationResults,
165+
currentStepIndex
166+
);
134167
if (!isNullOrEmpty(invalid)) {
135168
dispatchFunctions.updateFocusOn(invalid);
136169
return;
@@ -148,7 +181,13 @@ export const FormBody = (props: FormBodyProps) => {
148181
formKey: form.key,
149182
locale: form.locale,
150183
isFinalized: submitButton?.properties?.finalizeForm || isLastStep,
151-
partialSubmissionKey: localFormCache.get(submissionStorageKey) ?? formContext?.submissionKey ?? '',
184+
partialSubmissionKey:
185+
localFormCache.get(
186+
submissionStorageKey,
187+
props.identityInfo?.username
188+
) ??
189+
formContext?.submissionKey ??
190+
"",
152191
hostedPageUrl: currentPageUrl,
153192
submissionData: formSubmissions,
154193
accessToken: formContext?.identityInfo?.accessToken,
@@ -162,32 +201,51 @@ export const FormBody = (props: FormBodyProps) => {
162201
.then((response: FormSubmitResult) => {
163202
//go here, response.success always is true
164203
isSuccess.current = response.success;
165-
isFormFinalized.current = (submitButton?.properties?.finalizeForm || isLastStep) && response.success;
204+
isFormFinalized.current =
205+
(submitButton?.properties?.finalizeForm || isLastStep) &&
206+
response.success;
166207
dispatchFunctions.updateSubmissionKey(response.submissionKey);
167-
localFormCache.set(submissionStorageKey, response.submissionKey);
208+
localFormCache.set(
209+
submissionStorageKey,
210+
response.submissionKey,
211+
props.identityInfo?.username
212+
);
168213

169214
if (isProgressiveSubmit.current) {
170-
message.current = response.messages.map((m) => m.message).join('<br>');
215+
message.current = response.messages
216+
.map((m) => m.message)
217+
.join("<br>");
171218
showForm.current = false;
172219
}
173220

174221
// Custom redirect message
175-
const redirectMessage = response.messages.find((message) => message.section === 'redirect');
222+
const redirectMessage = response.messages.find(
223+
(message) => message.section === "redirect"
224+
);
176225
if (redirectMessage && !isNullOrEmpty(redirectMessage.message)) {
177226
window.location.href = redirectMessage.message;
178227
return;
179228
}
180229

181230
if (isFormFinalized.current) {
182-
formCache.remove(FormConstants.FormCurrentStep + form.key);
183-
localFormCache.remove(submissionStorageKey);
231+
formCache.remove(
232+
FormConstants.FormCurrentStep + form.key,
233+
props.identityInfo?.username
234+
);
235+
localFormCache.remove(
236+
submissionStorageKey,
237+
props.identityInfo?.username
238+
);
184239
}
185240

186241
//redirect after submit
187242
if (submitButton) {
188-
const redirectToPage = submitButton?.properties?.redirectToPage ?? form.properties?.redirectToPage;
243+
const redirectToPage =
244+
submitButton?.properties?.redirectToPage ??
245+
form.properties?.redirectToPage;
189246
if (!isNullOrEmpty(redirectToPage)) {
190-
const cmsUrl = process.env.REACT_APP_HEADLESS_FORM_BASE_URL ?? 'http://temp';
247+
const cmsUrl =
248+
process.env.REACT_APP_HEADLESS_FORM_BASE_URL ?? "http://temp";
191249
const url = new URL(redirectToPage, cmsUrl);
192250
window.location.href = url.href;
193251
}
@@ -198,7 +256,10 @@ export const FormBody = (props: FormBodyProps) => {
198256
case 401:
199257
//clear access token to ask login again
200258
dispatchFunctions.updateIdentity({} as IdentityInfo);
201-
formCache.remove(FormConstants.FormAccessToken);
259+
formCache.remove(
260+
FormConstants.FormAccessToken,
261+
props.identityInfo?.username
262+
);
202263
break;
203264
case 400:
204265
if (e.errors) {
@@ -210,15 +271,21 @@ export const FormBody = (props: FormBodyProps) => {
210271
? fr
211272
: {
212273
...fr,
213-
result: { valid: false, message: e.errors[fr.elementKey].join('<br/>') },
274+
result: {
275+
valid: false,
276+
message: e.errors[fr.elementKey].join("<br/>"),
277+
},
214278
}
215279
) ?? [];
216280

217281
dispatchFunctions.updateAllValidation(formValidationResults);
218282

219283
//set focus on the 1st invalid element of current step
220284
dispatchFunctions.updateFocusOn(
221-
stepHelper.getFirstInvalidElement(formValidationResults, currentStepIndex)
285+
stepHelper.getFirstInvalidElement(
286+
formValidationResults,
287+
currentStepIndex
288+
)
222289
);
223290
}
224291
break;
@@ -233,29 +300,37 @@ export const FormBody = (props: FormBodyProps) => {
233300

234301
useEffect(() => {
235302
dispatchFunctions.updateIdentity(props.identityInfo);
236-
if (isNullOrEmpty(props.identityInfo?.accessToken) && !form.properties.allowAnonymousSubmission) {
303+
if (
304+
isNullOrEmpty(props.identityInfo?.accessToken) &&
305+
!form.properties.allowAnonymousSubmission
306+
) {
237307
showError(form.localizations.allowAnonymousSubmissionErrorMessage);
238308
} else {
239-
showError('');
309+
showError("");
240310
}
241311
}, [props.identityInfo?.accessToken]);
242312

243313
//reset when change page
244314
useEffect(() => {
245315
isSuccess.current = false;
246316
if (form.properties.focusOnForm || currentStepIndex > 0) {
247-
dispatchFunctions.updateFocusOn(stepHelper.getFirstInputElement(currentStepIndex, inactiveElements));
317+
dispatchFunctions.updateFocusOn(
318+
stepHelper.getFirstInputElement(currentStepIndex, inactiveElements)
319+
);
248320
}
249321
}, [currentStepIndex]);
250322

251323
//Run in-case change page by url. The currentStepIndex that get from cache is incorrect.
252324
useEffect(() => {
253325
if (!isStepValidToDisplay) {
254-
dispatchFunctions.updateCurrentStepIndex(stepHelper.getCurrentStepIndex(currentPageUrl));
326+
dispatchFunctions.updateCurrentStepIndex(
327+
stepHelper.getCurrentStepIndex(currentPageUrl)
328+
);
255329
}
256330
}, []);
257331

258-
isMalFormSteps && showError(form.localizations.malformstepconfigruationErrorMessage);
332+
isMalFormSteps &&
333+
showError(form.localizations.malformstepconfigruationErrorMessage);
259334

260335
return (
261336
<form
@@ -276,26 +351,30 @@ export const FormBody = (props: FormBodyProps) => {
276351
</h2>
277352
)}
278353
{form.properties.description && (
279-
<aside className="body-lg color-txt-primary body--400 Form__Description">{form.properties.description}</aside>
354+
<aside className="body-lg color-txt-primary body--400 Form__Description">
355+
{form.properties.description}
356+
</aside>
280357
)}
281358
<div
282359
className="Form__MainBody"
283-
style={{ display: showForm.current ? 'flow' : 'none' }}
360+
style={{ display: showForm.current ? "flow" : "none" }}
284361
>
285362
{/* render element */}
286363
{form.steps.map((e, i) => {
287364
const stepDisplaying =
288-
currentStepIndex === i && !isFormFinalized.current && isStepValidToDisplay && !isMalFormSteps ? '' : 'hide';
365+
currentStepIndex === i &&
366+
!isFormFinalized.current &&
367+
isStepValidToDisplay &&
368+
!isMalFormSteps
369+
? ""
370+
: "hide";
289371
return (
290372
<section
291373
key={e.formStep.key}
292374
id={e.formStep.key}
293375
className={`Form__Element__Step ${stepDisplaying}`}
294376
>
295-
<RenderElementInStep
296-
elements={e.elements}
297-
stepIndex={i}
298-
/>
377+
<RenderElementInStep elements={e.elements} stepIndex={i} />
299378
</section>
300379
);
301380
})}
@@ -311,7 +390,7 @@ export const FormBody = (props: FormBodyProps) => {
311390
/>
312391
</div>
313392
{/* area for showing Form's status or validation */}
314-
<div className="Form__Status">
393+
<div className="Form__Status alert-bar alert-bar--urgency--high">
315394
<div
316395
role="status"
317396
className={`Form__Status__Message ${statusDisplay.current}`}

0 commit comments

Comments
 (0)