Skip to content

Commit 89af0d7

Browse files
authored
Merge pull request #406 from 4site-interactive-studios/fix-watch-for-error
Core: Trigger watchForError callbacks from setError
2 parents e03f792 + a314136 commit 89af0d7

3 files changed

Lines changed: 49 additions & 25 deletions

File tree

packages/scripts/dist/engrid.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export declare abstract class ENGrid {
5454
static removeHtml(target: string): void;
5555
static slugify(text: string): string;
5656
static watchForError(callback: Function): void;
57+
private static getErrorCallbackKey;
5758
static getPaymentType(): string;
5859
static setPaymentType(paymentType: string): void;
5960
static isInViewport(element: HTMLElement): boolean;

packages/scripts/dist/engrid.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const errorCallbacks = new Map();
12
export class ENGrid {
23
constructor() {
34
if (!ENGrid.enForm) {
@@ -426,6 +427,7 @@ export class ENGrid {
426427
else {
427428
errorMessageElement.innerHTML = errorMessage;
428429
}
430+
errorCallbacks.forEach((callback) => callback());
429431
}
430432
}
431433
static removeError(element) {
@@ -510,6 +512,24 @@ export class ENGrid {
510512
// This function is used to run a callback function when an error is displayed on the page
511513
static watchForError(callback) {
512514
const errorElement = document.querySelector(".en__errorList");
515+
const callbackType = ENGrid.getErrorCallbackKey(callback);
516+
// Register callback so setError can trigger it too
517+
if (!errorCallbacks.has(callbackType)) {
518+
errorCallbacks.set(callbackType, callback);
519+
}
520+
if (errorElement && !errorElement.dataset[callbackType]) {
521+
errorElement.dataset[callbackType] = "true";
522+
const observer = new MutationObserver(function (mutations) {
523+
mutations.forEach(function (mutation) {
524+
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
525+
callback();
526+
}
527+
});
528+
});
529+
observer.observe(errorElement, { childList: true });
530+
}
531+
}
532+
static getErrorCallbackKey(callback) {
513533
const capitalize = (word) => word.charAt(0).toUpperCase() + word.slice(1);
514534
// Avoid duplicate callbacks
515535
let callbackType = callback.toString();
@@ -523,18 +543,7 @@ export class ENGrid {
523543
callbackType = callbackType.replace(/[^a-zA-Z0-9]/g, "");
524544
// Limit to 20 characters and add prefix
525545
callbackType = callbackType.substring(0, 20);
526-
callbackType = "engrid" + capitalize(callbackType);
527-
if (errorElement && !errorElement.dataset[callbackType]) {
528-
errorElement.dataset[callbackType] = "true";
529-
const observer = new MutationObserver(function (mutations) {
530-
mutations.forEach(function (mutation) {
531-
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
532-
callback();
533-
}
534-
});
535-
});
536-
observer.observe(errorElement, { childList: true });
537-
}
546+
return "engrid" + capitalize(callbackType);
538547
}
539548
// Get the Payment Type
540549
static getPaymentType() {

packages/scripts/src/engrid.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Options } from ".";
22

3+
const errorCallbacks = new Map<string, Function>();
4+
35
export abstract class ENGrid {
46
constructor() {
57
if (!ENGrid.enForm) {
@@ -492,6 +494,7 @@ export abstract class ENGrid {
492494
} else {
493495
errorMessageElement.innerHTML = errorMessage;
494496
}
497+
errorCallbacks.forEach((callback) => callback());
495498
}
496499
}
497500
static removeError(element: string | HTMLElement) {
@@ -592,8 +595,31 @@ export abstract class ENGrid {
592595
const errorElement = document.querySelector(
593596
".en__errorList"
594597
) as HTMLUListElement;
598+
const callbackType = ENGrid.getErrorCallbackKey(callback);
599+
600+
// Register callback so setError can trigger it too
601+
if (!errorCallbacks.has(callbackType)) {
602+
errorCallbacks.set(callbackType, callback);
603+
}
604+
605+
if (errorElement && !errorElement.dataset[callbackType]) {
606+
errorElement.dataset[callbackType] = "true";
607+
608+
const observer = new MutationObserver(function (mutations) {
609+
mutations.forEach(function (mutation) {
610+
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
611+
callback();
612+
}
613+
});
614+
});
615+
observer.observe(errorElement, { childList: true });
616+
}
617+
}
618+
619+
private static getErrorCallbackKey(callback: Function): string {
595620
const capitalize = (word: string) =>
596621
word.charAt(0).toUpperCase() + word.slice(1);
622+
597623
// Avoid duplicate callbacks
598624
let callbackType = callback.toString();
599625
if (callbackType.indexOf("function") === 0) {
@@ -606,19 +632,7 @@ export abstract class ENGrid {
606632
callbackType = callbackType.replace(/[^a-zA-Z0-9]/g, "");
607633
// Limit to 20 characters and add prefix
608634
callbackType = callbackType.substring(0, 20);
609-
callbackType = "engrid" + capitalize(callbackType);
610-
if (errorElement && !errorElement.dataset[callbackType]) {
611-
errorElement.dataset[callbackType] = "true";
612-
613-
const observer = new MutationObserver(function (mutations) {
614-
mutations.forEach(function (mutation) {
615-
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
616-
callback();
617-
}
618-
});
619-
});
620-
observer.observe(errorElement, { childList: true });
621-
}
635+
return "engrid" + capitalize(callbackType);
622636
}
623637
// Get the Payment Type
624638
static getPaymentType(): string {

0 commit comments

Comments
 (0)