Skip to content

Commit

Permalink
Merge branch 'master' into vsagniez/del-page-indicateur
Browse files Browse the repository at this point in the history
  • Loading branch information
vinsag authored Jun 4, 2024
2 parents d018541 + 274d55b commit 7a584c2
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ GHOST_KEY=
NEXT_PUBLIC_BAL_ADMIN_API_URL=https://bal-admin.adresse.data.gouv.fr/api
NEXT_PUBLIC_BAL_API_URL=https://api-bal.adresse.data.gouv.fr/v2
NEXT_PUBLIC_MES_ADRESSES=https://mes-adresses.data.gouv.fr
NEXT_PUBLIC_BAL_WIDGET_URL=https://baseadressenationale.github.io/bal-widget

# -----------------------------------------
# --- Utilisation Matomo pour les stats ---
Expand Down
131 changes: 131 additions & 0 deletions components/bal-widget/bal-widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {useState, useEffect, useMemo, useRef} from 'react'
import styled from 'styled-components'
import {useRouter} from 'next/router'
import {push as matomoPush} from '@socialgouv/matomo-next'
import getConfig from 'next/config'

const {
isDevMode,
NEXT_PUBLIC_BAL_WIDGET_URL: BAL_WIDGET_URL,
NEXT_PUBLIC_BAL_ADMIN_API_URL: BAL_ADMIN_API_URL
} = getConfig().publicRuntimeConfig

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

const StyledIFrame = styled.iframe`
position: fixed;
bottom: 40px;
right: 40px;
z-index: 999;
${({$isOpen}) => $isOpen ? 'height: 600px; width: 450px;' : 'height: 60px; width: 60px;'}
@media screen and (max-width: 450px) {
bottom: 10px;
right: 10px;
${({$isOpen}) => $isOpen && 'width: calc(100% - 20px);'}
}
`

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

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

if (!isWidgetDisplayed && isBalWidgetReady) {
setIsBalWidgetReady(false)
}

// 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()
}, [])

// Send config to BAL widget
// once it's ready
useEffect(() => {
if (balWidgetRef.current && isBalWidgetReady) {
balWidgetRef.current.contentWindow.postMessage(
{
type: 'BAL_WIDGET_CONFIG',
content: balWidgetConfig,
},
'*'
)
}
}, [isBalWidgetReady, balWidgetRef, balWidgetConfig])

useEffect(() => {
let transitionTimeout = null
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
transitionTimeout = setTimeout(() => {
setIsBalWidgetOpen(false)
}, 300)
break
case 'BAL_WIDGET_LOCATION':
if (isBalWidgetOpen) {
matomoPush(['trackEvent', matomoCategoryName, 'Location changed', event.data.content, 1])
}

break
case 'BAL_WIDGET_READY':
setIsBalWidgetReady(true)
break
default:
break
}
}

window.addEventListener('message', BALWidgetMessageHandler)

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

return isWidgetDisplayed ? (
<StyledIFrame
ref={balWidgetRef}
src={BAL_WIDGET_URL}
$isOpen={isBalWidgetOpen}
/>
) : null
}

export default BALWidget
17 changes: 11 additions & 6 deletions components/evenement/event-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import theme from '@/styles/theme'

import {formatTag} from '@/lib/tag'
import {dateWithDay} from '@/lib/date'
import {removeAccents} from '@/lib/format-strings'

import ButtonLink from '@/components/button-link'
import SectionText from '@/components/section-text'
Expand Down Expand Up @@ -35,13 +36,13 @@ function EventModal({event, isPassed, onClose}) {
return (
<div className='modal-wrapper'>
<div className='modal' ref={modalRef}>
<div className={`header ${type}-backg`}>
<div className={`header ${removeAccents(type)}-backg`}>
<ActionButtonNeutral label='Fermer la fenêtre'>
<XCircle onClick={onClose} color={theme.colors.white} />
</ActionButtonNeutral>

<div className='presentation'>
<Image src={`/images/icons/event-${type}.svg`} height={150} width={170} alt='' aria-hidden='true' />
<div className='presentation-wrapper'>
<Image src={`/images/icons/event-${removeAccents(type)}.svg`} height={150} width={170} alt='' aria-hidden='true' />
<div className='header-infos'>
<div className='title-container'>
<h5>{title}</h5>
Expand Down Expand Up @@ -87,7 +88,7 @@ function EventModal({event, isPassed, onClose}) {
align-items: center;
}
.modal, .header-infos, .presentation, .target, .tags {
.modal, .header-infos, .presentation-wrapper, .target, .tags {
display: flex;
}
Expand Down Expand Up @@ -127,7 +128,7 @@ function EventModal({event, isPassed, onClose}) {
opacity: 70%;
}
.presentation {
.presentation-wrapper {
gap: 2em;
justify-content: space-between;
margin-top: .5em;
Expand Down Expand Up @@ -184,6 +185,10 @@ function EventModal({event, isPassed, onClose}) {
background: ${theme.colors.blue};
}
.presentation-backg {
background: rgb(26, 168, 255);
}
.adresse-region-backg {
background: ${theme.colors.purple};
}
Expand All @@ -205,7 +210,7 @@ function EventModal({event, isPassed, onClose}) {
}
@media (max-width: 624px) {
.presentation {
.presentation-wrapper {
flex-wrap: wrap;
justify-content: flex-end;
}
Expand Down
13 changes: 9 additions & 4 deletions components/evenement/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import Image from 'next/legacy/image'

import {dateWithDay} from '@/lib/date'
import {removeAccents} from '@/lib/format-strings'

import theme from '@/styles/theme'

Expand All @@ -17,9 +18,9 @@ function Event({event, background, isPassed}) {

return (
<div className='event-container'>
<div className={`header ${type}`}>
<div className='presentation'>
<Image src={`/images/icons/event-${type}.svg`} height={50} width={50} alt='' aria-hidden='true' />
<div className={`header ${removeAccents(type)}`}>
<div className='presentation-wrapper'>
<Image src={`/images/icons/event-${removeAccents(type)}.svg`} height={50} width={50} alt='' aria-hidden='true' />
<div className='title-container'>
<h5>{title}</h5>
<div>{subtitle}</div>
Expand Down Expand Up @@ -70,7 +71,7 @@ function Event({event, background, isPassed}) {
gap: 1em;
}
.presentation {
.presentation-wrapper {
display: flex;
flex-direction: column;
align-items: center;
Expand Down Expand Up @@ -119,6 +120,10 @@ function Event({event, background, isPassed}) {
background: ${theme.colors.green};
}
.presentation {
background: rgb(26, 168, 255);
}
.partenaire {
background: ${theme.colors.blue};
}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ services:
- NEXT_PUBLIC_GHOST_URL_IMAGES_SOURCE=${NEXT_PUBLIC_GHOST_URL_IMAGES_SOURCE}
- NEXT_PUBLIC_BAL_ADMIN_API_URL=${NEXT_PUBLIC_BAL_ADMIN_API_URL}
- NEXT_PUBLIC_BAL_API_URL=${NEXT_PUBLIC_BAL_API_URL}
- NEXT_PUBLIC_BAL_WIDGET_URL=${NEXT_PUBLIC_BAL_WIDGET_URL}
- NEXT_PUBLIC_MES_ADRESSES=${NEXT_PUBLIC_MES_ADRESSES}
- S3_CONFIG_ACCESS_KEY_ID=${S3_CONFIG_ACCESS_KEY_ID}
- S3_CONFIG_SECRET_ACCESS_KEY=${S3_CONFIG_SECRET_ACCESS_KEY}
Expand Down
1 change: 1 addition & 0 deletions lib/format-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const removeAccents = str => str.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // eslint-disable-line unicorn/escape-case
2 changes: 2 additions & 0 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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'
Expand Down Expand Up @@ -45,6 +46,7 @@ function MyApp({Component, pageProps}) {
<DeviceContextProvider>
<div id='alert-root' />
<Component {...pageProps} />
<BALWidget />
</DeviceContextProvider>
<style global jsx>{`
body,
Expand Down
30 changes: 30 additions & 0 deletions public/images/icons/event-presentation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7a584c2

Please sign in to comment.