Skip to content

Commit

Permalink
[site,newsletter][s]: fix hero newsletter form (broken after Brevo up…
Browse files Browse the repository at this point in the history
…date)
  • Loading branch information
demenech committed Aug 18, 2023
1 parent 8c5c6a2 commit 4bc7ce5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
25 changes: 0 additions & 25 deletions site/components/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,6 @@ import ButtonLink from './ButtonLink';
import NewsletterForm from './NewsletterForm';
import Image from 'next/image';
import DatahubExampleImg from '@/public/images/showcases/datahub.webp';

const codeLanguage = 'javascript';
const code = `export default {
strategy: 'predictive',
engine: {
cpus: 12,
backups: ['./storage/cache.wtf'],
},
}`;

const tabs = [
{ name: 'cache-advance.config.js', isActive: true },
{ name: 'package.json', isActive: false },
];

function TrafficLightsIcon(props) {
return (
<svg aria-hidden="true" viewBox="0 0 42 10" fill="none" {...props}>
<circle cx="5" cy="5" r="4.5" />
<circle cx="21" cy="5" r="4.5" />
<circle cx="37" cy="5" r="4.5" />
</svg>
);
}

/* eslint jsx-a11y/label-has-associated-control: off */
export function Hero() {
const el = useRef(null);
Expand Down
52 changes: 21 additions & 31 deletions site/components/NewsletterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { loadScripts } from '@/lib/loadNewsletterScripts';
import Script from 'next/script';
import { useEffect } from 'react';

export default function NewsletterForm() {
useEffect(() => {
/*
* The newsletter scripts MUST be loaded after
* the document is loaded, as they depend on
* the presence of some elements
*
*/
if (document.readyState === 'complete') {
const { resetElements } = loadScripts();

return () => {
resetElements();
};
} else {
window.addEventListener('load', loadScripts);
return () => window.removeEventListener('load', loadScripts);
}
}, []);

return (
<div>
<div
Expand Down Expand Up @@ -88,37 +109,6 @@ export default function NewsletterForm() {
</div>
</div>
</div>
<Script
id="newsletter-form-validation-message"
dangerouslySetInnerHTML={{
__html: `
window.REQUIRED_CODE_ERROR_MESSAGE = 'Please choose a country code';
window.LOCALE = 'en';
window.EMAIL_INVALID_MESSAGE = window.SMS_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
window.REQUIRED_ERROR_MESSAGE = "This field cannot be left blank. ";
window.GENERIC_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
window.translation = {
common: {
selectedList: '{quantity} list selected',
selectedLists: '{quantity} lists selected'
}
};
var AUTOHIDE = Boolean(0);
`,
}}
/>
<Script
strategy="worker"
id="newsletter-submit-form"
src="https://sibforms.com/forms/end-form/build/main.js"
/>
</div>
);
}
45 changes: 45 additions & 0 deletions site/lib/loadNewsletterScripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import { HTMLProps } from "react";

const loadScript = (
props: HTMLProps<HTMLScriptElement> & { textContent?: string }
): (() => void) => {
const script = document.createElement("script");
Object.assign(script, props);
document.head.appendChild(script);
return () => document.head.removeChild(script);
};

export const loadScripts = () => {
const formValidationScript = loadScript({
id: "newsletter-form-validation-message",
textContent: `
window.LOCALE = 'en';
window.EMAIL_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
window.REQUIRED_ERROR_MESSAGE = "This field cannot be left blank. ";
window.GENERIC_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
window.translation = {
common: {
selectedList: '{quantity} list selected',
selectedLists: '{quantity} lists selected'
}
};
var AUTOHIDE = Boolean(0);
`,
});

const formSubmitScript = loadScript({
id: "newsletter-submit-form",
src: "https://sibforms.com/forms/end-form/build/main.js",
async: true,
defer: true,
});

return {
resetElements: () => {
formSubmitScript();
formValidationScript();
},
};
};

1 comment on commit 4bc7ce5

@vercel
Copy link

@vercel vercel bot commented on 4bc7ce5 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.