Merged in fix/Sj-widget-error (pull request #3332)
refactor loading of SJ widget to avoid undefined window * refactor loading of widget to avoid undefined window Approved-by: Bianca Widstam Approved-by: Erik Tiekstra Approved-by: Joakim Jäderberg Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -10,24 +10,22 @@ const SJSupportedLangs = ["en", "sv"] as const
|
|||||||
export function SJWidget() {
|
export function SJWidget() {
|
||||||
const lang = useLang()
|
const lang = useLang()
|
||||||
const [scriptInitialized, setScriptInitialized] = useState(
|
const [scriptInitialized, setScriptInitialized] = useState(
|
||||||
!!window.SJ?.widget?.init
|
() => typeof window !== "undefined" && !!window.SJ?.widget?.init
|
||||||
)
|
)
|
||||||
const componentInitialized = useRef(false)
|
const componentInitialized = useRef(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initWidget = () => {
|
if (!scriptInitialized) return
|
||||||
if (componentInitialized.current === true) return
|
if (componentInitialized.current) return
|
||||||
if (!window.SJ?.widget?.init) return
|
|
||||||
|
|
||||||
window.SJ.widget.init({
|
if (!window.SJ?.widget?.init) return
|
||||||
micrositeId: "12952d0f-c70f-452c-9598-6586a64c7b60",
|
|
||||||
language: isSJSupportedLang(lang) ? lang : "en",
|
|
||||||
})
|
|
||||||
|
|
||||||
componentInitialized.current = true
|
window.SJ.widget.init({
|
||||||
}
|
micrositeId: "12952d0f-c70f-452c-9598-6586a64c7b60",
|
||||||
|
language: isSJSupportedLang(lang) ? lang : "en",
|
||||||
|
})
|
||||||
|
|
||||||
initWidget()
|
componentInitialized.current = true
|
||||||
}, [lang, scriptInitialized])
|
}, [lang, scriptInitialized])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user