Skip to content

Commit

Permalink
add integration
Browse files Browse the repository at this point in the history
  • Loading branch information
desoindx committed Nov 19, 2024
1 parent c62bfad commit 09b2303
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 95 deletions.
17 changes: 16 additions & 1 deletion src/components/form/RadioInput.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@
&:hover {
color: var(--neutral-80);
}
}
}

.disabled {
cursor: default;
color: var(--neutral-30) !important;

input[type='radio'] {
cursor: default;
border-color: var(--neutral-30) !important;
background-color: var(--neutral-00) !important;

&:before {
background-color: var(--neutral-30) !important;
}
}
}
6 changes: 5 additions & 1 deletion src/components/form/RadioInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const RadioInput = ({
} & InputHTMLAttributes<HTMLInputElement>) => {
return (
<div className={styles.container}>
<label className={classNames(styles.legend, { [styles.checked]: selected === value })}>
<label
className={classNames(styles.legend, {
[styles.checked]: selected === value,
[styles.disabled]: inputProps.disabled,
})}>
<input
{...inputProps}
type='radio'
Expand Down
2 changes: 2 additions & 0 deletions src/components/outils/AlimentationSimulator.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
}

.categories {
margin-bottom: -0.75rem;
position: relative;
padding: 1.5rem;
border-top: 2px solid var(--primary-30);
background-color: var(--primary-10);
Expand Down
11 changes: 9 additions & 2 deletions src/components/outils/AlimentationSimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useTranslations } from 'next-intl'
import React, { useMemo } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import { computedEquivalents } from 'src/providers/equivalents'
import { AlimentationCategories, equivalentsByCategory } from 'utils/alimentation'
import { track } from 'utils/matomo'
import HiddenLabel from 'components/form/HiddenLabel'
Expand All @@ -14,13 +15,19 @@ import AlimentationSubCategory from './alimentation/AlimentationSubCategory'

const AlimentationSimulator = () => {
const {
alimentation: { category, setCategory },
alimentation: { category, setCategory, customList, equivalents },
} = useParamContext()

const t = useTranslations('alimentation')
const values = useMemo(() => equivalentsByCategory[category], [category])
const [openCategories, setOpenCategories] = React.useState<Record<string, boolean>>({})
return (
return customList ? (
<CategorySimulator
equivalents={computedEquivalents.filter((equivalent) => equivalents.includes(equivalent.slug))}
tracking='Alimentation'
reverse
/>
) : (
<>
<div className={styles.simulator}>
<p>{t.rich('title')}</p>
Expand Down
2 changes: 1 addition & 1 deletion src/components/outils/CategorySimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const CategorySimulator = ({
</span>{' '}
kg CO₂e
</div>
<p className='hidden'>{barExplanation}</p>
{hasUsage && <p className='hidden'>{barExplanation}</p>}
</div>
</IframeableLink>
{!!equivalent.carpool && type && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const AlimentationSubCategory = ({
return (
<div className={styles.box}>
<button
data-testid={`alimentation-category-${name}`}
className={styles.button}
aria-expanded={display}
aria-controls={`alimentation-category-${name}`}
Expand Down
5 changes: 0 additions & 5 deletions src/components/shareable/Shareable.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@
}
}

.withoutBorder {
border: none;
padding-top: 0.75rem;
}

.bottomLanguage,
.language {
position: relative;
Expand Down
46 changes: 26 additions & 20 deletions src/components/shareable/Shareable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import classNames from 'classnames'
import { useTranslations } from 'next-intl'
import React, { ReactNode, useCallback, useEffect, useMemo, useRef } from 'react'
import { useSearchParams } from 'next/navigation'
import React, { ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import TranslationProvider from 'src/providers/TranslationProvider'
import { SiteLanguage } from 'types/languages'
Expand Down Expand Up @@ -55,6 +56,12 @@ const Shareable = ({

const onClose = useCallback(() => setOverscreen(slug, ''), [slug])

const [showButtons, setShowButtons] = useState(true)
const searchParams = useSearchParams()
useEffect(() => {
setShowButtons(searchParams.get('hideButtons') !== 'true')
}, [searchParams])

useEffect(() => {
if (overScreenToDisplay && overscreenRef.current) {
const modalElement = overscreenRef.current
Expand Down Expand Up @@ -155,11 +162,8 @@ const Shareable = ({
</div>
{secondary === undefined && (
<>
{overScreens && ('hypothesis' in overScreens || 'data' in overScreens) ? (
<div
className={classNames('no-screenshot', styles.ressources, {
[styles.withoutBorder]: overScreens.hypothesis?.noBorder,
})}>
{showButtons && overScreens && ('hypothesis' in overScreens || 'data' in overScreens) ? (
<div className={classNames('no-screenshot', styles.ressources)}>
{'data' in overScreens && (
<Feature
slug={slug}
Expand Down Expand Up @@ -208,20 +212,22 @@ const Shareable = ({
</>
)}
</div>
<div className={classNames(styles.actions, { [styles.secondaryActions]: secondary !== undefined })}>
<Actions
onClick={(action) => {
if (action === 'telecharger') {
takeScreenshot()
} else {
setOverscreen(slug, action)
}
}}
tracking={tracking}
withoutIntegration={withoutIntegration}
withoutShare={withoutShare}
/>
</div>
{showButtons && (
<div className={classNames(styles.actions, { [styles.secondaryActions]: secondary !== undefined })}>
<Actions
onClick={(action) => {
if (action === 'telecharger') {
takeScreenshot()
} else {
setOverscreen(slug, action)
}
}}
tracking={tracking}
withoutIntegration={withoutIntegration}
withoutShare={withoutShare}
/>
</div>
)}
</div>
)
}
Expand Down
78 changes: 78 additions & 0 deletions src/components/shareable/overScreens/AlimentationIntegrate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use client'

import { useTranslations } from 'next-intl'
import React, { useEffect, useMemo, useState } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import ClipboardBox from 'components/base/ClipboardBox'
import AlimentationListParam from './AlimentationListParam'
import CustomParam, { CustomParamValue } from './CustomParam'
import styles from './CustomParam.module.css'
import IntegratePreview from './IntegratePreview'
import shareStyles from './Share.module.css'

const AlimentationIntegrate = () => {
const t = useTranslations('alimentation.integrate')
const { theme, language, alimentation: alimentationParams } = useParamContext()

const [hideButtons, setHideButtons] = useState(false)
const [category, setCategory] = useState(alimentationParams.category)
const [customList, setCustomList] = useState(alimentationParams.customList)
const [equivalents, setEquivalents] = useState(alimentationParams.equivalents)

useEffect(() => {
setCategory(alimentationParams.category)
}, [alimentationParams.category])

const search = useMemo(() => {
const result = `&theme=${theme}&language=${language}`
const hideButtonsParam = hideButtons ? '&hideButtons=true' : ''
if (customList) {
return `alimentationEquivalents=${equivalents.join(',')}${hideButtonsParam}${result}`
}
return `alimentationCategory=${category}${hideButtonsParam}${result}`
}, [category, customList, equivalents, hideButtons, theme, language])

return (
<>
<form id='alimentation-integrate'>
<CustomParam
tracking='Alimentation'
slug='alimentationCategoryIntegrate'
integration
visible
disabled={customList}
param={{ value: category, setter: setCategory } as CustomParamValue}
/>
<div className={shareStyles.separator} />
<fieldset>
<legend className={styles.title}>{t('customList')}</legend>
<CustomParam
tracking='Alimentation'
slug='customList'
integration
visible
param={{ value: customList, setter: setCustomList } as CustomParamValue}
/>
{customList && <AlimentationListParam equivalents={equivalents} setEquivalents={setEquivalents} />}
</fieldset>
<div className={shareStyles.separator} />
<fieldset>
<legend className={styles.title}>{t('hideButtons')}</legend>
<CustomParam
tracking='Alimentation'
slug='hideButtons'
integration
visible
param={{ value: hideButtons, setter: setHideButtons } as CustomParamValue}
/>
</fieldset>
</form>
<ClipboardBox
form='alimentation-integrate'
tracking='Alimentation'>{`<script name="impact-co2" src="${process.env.NEXT_PUBLIC_URL}/iframe.js" data-type="/alimentation" data-search="?${search}"></script>`}</ClipboardBox>
<IntegratePreview path='/alimentation' urlParams={search} />
</>
)
}

export default AlimentationIntegrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.categories {
display: flex;
flex-direction: column;
gap: 2.25rem;
margin-top: -0.5rem;
}

.category {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;
}

.categoryName {
color: var(--neutral-50);
font-size: 0.875rem;
font-weight: 500;
line-height: 1.5rem;
text-transform: uppercase;
}
72 changes: 72 additions & 0 deletions src/components/shareable/overScreens/AlimentationListParam.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import classNames from 'classnames'
import { useTranslations } from 'next-intl'
import { SetStateAction } from 'preact/compat'
import React, { Dispatch } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import { getNameWithoutSuffix } from 'utils/Equivalent/equivalent'
import { AlimentationCategories, equivalentsByCategory } from 'utils/alimentation'
import EquivalentIcon from 'components/base/EquivalentIcon'
import Button from 'components/base/buttons/Button'
import CheckboxInput from 'components/form/CheckboxInput'
import styles from './AlimentationListParam.module.css'
import listStyles from './TransportListParam.module.css'

const AlimentationListParam = ({
equivalents,
setEquivalents,
}: {
equivalents: string[]
setEquivalents: Dispatch<SetStateAction<string[]>>
}) => {
const t = useTranslations('alimentation')
const { language } = useParamContext()
return (
<ul className={styles.categories}>
{equivalentsByCategory[AlimentationCategories.Group].map((category) => (
<li key={category.name} className={styles.list}>
<div className={styles.category}>
<p className={styles.categoryName}>{t(category.name)}</p>
<Button
asLink
type='button'
onClick={() =>
setEquivalents(
equivalents.filter((equivalent) => category.equivalents.every(({ slug }) => equivalent !== slug))
)
}>
{t('deselect')}
</Button>
</div>
<ul className={listStyles.equivalents}>
{category.equivalents.map((equivalent) => (
<li key={equivalent.slug} className={listStyles.list}>
<CheckboxInput
id={`alimentation-list-${equivalent.slug}-checkbox`}
reversed
className={classNames(listStyles.mode, listStyles.active)}
labelClassName={listStyles.modeLabel}
checked={equivalents.includes(equivalent.slug)}
setChecked={(checked) =>
setEquivalents(
checked
? [...equivalents, equivalent.slug]
: equivalents.filter((value) => value !== equivalent.slug)
)
}
label={
<span className={listStyles.left}>
<EquivalentIcon equivalent={equivalent} height={2.5} />
<span className={listStyles.name}>{getNameWithoutSuffix(language, equivalent)}</span>
</span>
}
/>
</li>
))}
</ul>
</li>
))}
</ul>
)
}

export default AlimentationListParam
Loading

0 comments on commit 09b2303

Please sign in to comment.