one-click-accessibility/modules/settings/assets/js/components/widget-loader/index.js
Pavlo Kniazevych 82499ee78c
Fix: Fix the QA bugs [n/a] (#138)
* New: Finish the BE integration [n/a]

* Fix: Fix some bugs [n/a]
2025-01-19 13:11:09 +02:00

67 lines
1.6 KiB
JavaScript

import { useSettings } from '@ea11y/hooks';
import { useEffect } from '@wordpress/element';
const WidgetLoader = ({ src, onLoad, onError }) => {
const { planData } = useSettings();
useEffect(() => {
const handleScriptLoad = () => {
console.log('External script loaded!');
};
const handleScriptError = () => {
console.error('Failed to load the external script.');
};
// Check if the script already exists
const existingScript = document.querySelector(`script[src="${src}"]`);
if (existingScript) {
console.log(`Script with src "${src}" already loaded.`);
return;
}
// Create a new script element
const script = document.createElement('script');
if (src) {
script.src = src;
} else {
script.src = `${window?.ea11ySettingsData?.widgetUrl}?api_key=${planData?.public_api_key}`;
}
script.async = true;
// Attach onLoad and onError handlers
script.onload = () => {
console.log(`Script loaded successfully: ${src}`);
if (onLoad) {
onLoad();
window?.ea11yWidget?.widget?.open();
} else {
handleScriptLoad();
window?.ea11yWidget?.widget?.open();
}
};
script.onerror = () => {
console.error(`Failed to load script: ${src}`);
if (onError) {
onError();
} else {
handleScriptError();
}
};
// Append the script to the document head
document.head.appendChild(script);
// Cleanup: Remove the script if the component unmounts
return () => {
document.head.removeChild(script);
console.log(`Script removed: ${src}`);
};
}, [src, onLoad, onError]);
return null;
};
export default WidgetLoader;