Skip to content

Commit

Permalink
feat: add iframe integration for bal-widget
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Jan 25, 2024
1 parent 04f2663 commit 3245655
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
96 changes: 96 additions & 0 deletions components/bal-widget/bal-widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {useState, useEffect, useMemo} from 'react'
import {useRouter} from 'next/router'
import {push as matomoPush} from '@socialgouv/matomo-next'
import getConfig from 'next/config'

const BAL_WIDGET_URL = process.env.NEXT_PUBLIC_BAL_WIDGET_URL

const BAL_ADMIN_API_URL =
process.env.NEXT_PUBLIC_BAL_ADMIN_API_URL || 'https://bal-admin.adresse.data.gouv.fr/api'

const {isDevMode} = getConfig().publicRuntimeConfig
const matomoCategoryName = `${isDevMode ? 'DEVMODE - ' : ''}BAL Widget (Front)`

function BALWidget() {
const [isBalWidgetOpen, setIsBalWidgetOpen] = useState(false)
const [balWidgetConfig, setBalWidgetConfig] = useState(null)
const router = useRouter()

// Fetch BAL widget config
useEffect(() => {
async function fetchBalWidgetConfig() {
try {
const response = await fetch(`${BAL_ADMIN_API_URL}/bal-widget/config`)
const data = await response.json()
if (response.status !== 200) {
throw new Error(data.message)
}

setBalWidgetConfig(data)
} catch (error) {
console.error(error)
}
}

fetchBalWidgetConfig()
}, [])

useEffect(() => {
function BALWidgetMessageHandler(event) {
switch (event.data?.type) {
case 'BAL_WIDGET_OPENED':
matomoPush(['trackEvent', matomoCategoryName, 'Location changed', '/', 1])
setIsBalWidgetOpen(true)
break
case 'BAL_WIDGET_CLOSED':
// Wait for transition to end before closing the iframe
setTimeout(() => {
setIsBalWidgetOpen(false)
}, 300)
break
case 'BAL_WIDGET_LOCATION':
if (isBalWidgetOpen) {
matomoPush(['trackEvent', matomoCategoryName, 'Location changed', event.data.content, 1])
}

break
default:
break
}
}

window.addEventListener('message', BALWidgetMessageHandler)

return () => {
window.removeEventListener('message', BALWidgetMessageHandler)
}
}, [isBalWidgetOpen])

const isWidgetDisabled = useMemo(() => {
if (balWidgetConfig) {
const availablePages = balWidgetConfig.global.showOnPages || []
return balWidgetConfig.global.hideWidget ||
(availablePages.length > 0 && availablePages.every(page => router.pathname !== page))
}

return true
}, [router.pathname, balWidgetConfig])

const isWidgetDisplayed = !isWidgetDisabled || isBalWidgetOpen

return isWidgetDisplayed ? (
<iframe
src={BAL_WIDGET_URL}
width={isBalWidgetOpen ? 450 : 60}
height={isBalWidgetOpen ? 800 : 60}
style={{
position: 'fixed',
bottom: 40,
right: 40,
zIndex: 999,
}}
/>
) : null
}

export default BALWidget
6 changes: 2 additions & 4 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {createNextDsfrIntegrationApi} from '@codegouvfr/react-dsfr/next-pagesdir
import {useIsDark} from '@codegouvfr/react-dsfr/useIsDark'
import {DeviceContextProvider} from '@/contexts/device'
import {init as matomoInit} from '@socialgouv/matomo-next'
import BALWidget from '@/components/bal-widget/bal-widget'

import '@/styles/template-data-gouv-to-dsfr/normalizer.css'
import '@/styles/template-data-gouv-to-dsfr/main-alternate.css'
import Script from 'next/script'

const MATOMO_URL = process.env.NEXT_PUBLIC_MATOMO_URL
const MATOMO_SITE_ID = process.env.NEXT_PUBLIC_MATOMO_SITE_ID
const BAL_WIDGET_URL = process.env.NEXT_PUBLIC_BAL_WIDGET_URL

const {
withDsfr,
Expand Down Expand Up @@ -44,9 +43,8 @@ function MyApp({Component, pageProps}) {
<DeviceContextProvider>
<div id='alert-root' />
<Component {...pageProps} />
<BALWidget />
</DeviceContextProvider>
{BAL_WIDGET_URL && (
<Script src={`${BAL_WIDGET_URL}/bal-widget.js`} strategy='lazyOnload' />)}
<style global jsx>{`
body,
html,
Expand Down

0 comments on commit 3245655

Please sign in to comment.