Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 29 changed files with 404 additions and 259 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nosgestesclimat-site-nextjs",
"license": "MIT",
"version": "1.5.2",
"version": "1.5.3",
"description": "The leading open source climate footprint calculator",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"@babel/runtime": "^7.23.1",
"@incubateur-ademe/nosgestesclimat": "2.5.1",
"@incubateur-ademe/nosgestesclimat": "2.5.2",
"@mdx-js/loader": "^3.0.0",
"@mdx-js/react": "^3.0.0",
"@next/bundle-analyzer": "^14.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import GroupLoader from '@/components/groups/GroupLoader'
import GroupNotFound from '@/components/groups/GroupNotFound'
import GoBackLink from '@/design-system/inputs/GoBackLink'
import { useFetchGroup } from '@/hooks/groups/useFetchGroup'
import { useFixComputedResults } from '@/hooks/groups/useFixComputedResults'
import { useGroupIdInQueryParams } from '@/hooks/groups/useGroupIdInQueryParams'
import { useGroupPagesGuard } from '@/hooks/navigation/useGroupPagesGuard'
import EditableGroupTitle from './_components/EditableGroupTitle'
Expand All @@ -18,23 +19,26 @@ export default function GroupResultsPage() {
const { groupIdInQueryParams } = useGroupIdInQueryParams()
const { data: group, isLoading } = useFetchGroup(groupIdInQueryParams)

// TODO : Remove this hook when the computed results are fixed
const groupWithSafeComputedResults = useFixComputedResults(group)

// If we are still fetching the group (or we are redirecting the user), we display a loader
if (!isGuardInit || isGuardRedirecting || isLoading) {
return <GroupLoader />
}

// If the group doesn't exist, we display a 404 page
if (!group) {
if (!groupWithSafeComputedResults) {
return <GroupNotFound />
}

return (
<div>
<GoBackLink className="mb-4 font-bold" />

<EditableGroupTitle group={group} />
<EditableGroupTitle group={groupWithSafeComputedResults} />

<GroupResults group={group} />
<GroupResults group={groupWithSafeComputedResults} />
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Trans from '@/components/translation/Trans'
import {
LIST_MAIN_NEWSLETTER,
LIST_NOS_GESTES_LOGEMENT_NEWSLETTER,
LIST_NOS_GESTES_TRANSPORT_NEWSLETTER,
} from '@/constants/brevo'
import { endClickSaveSimulation } from '@/constants/tracking/pages/end'
Expand All @@ -17,9 +18,10 @@ import { useSaveSimulation } from '@/hooks/simulation/useSaveSimulation'
import { useClientTranslation } from '@/hooks/useClientTranslation'
import { useLocale } from '@/hooks/useLocale'
import { useNumberSubscribers } from '@/hooks/useNumberSubscriber'
import { useCurrentSimulation, useUser } from '@/publicodes-state'
import { useCurrentSimulation, useEngine, useUser } from '@/publicodes-state'
import { isEmailValid } from '@/utils/isEmailValid'
import { trackEvent } from '@/utils/matomo/trackEvent'
import { captureException } from '@sentry/react'
import { useEffect, useRef, useState } from 'react'
import { SubmitHandler, useForm as useReactHookForm } from 'react-hook-form'
import { twMerge } from 'tailwind-merge'
Expand All @@ -30,6 +32,7 @@ type Inputs = {
email?: string
'newsletter-saisonniere': boolean
'newsletter-transports': boolean
'newsletter-logement': boolean
}

export default function GetResultsByEmail({
Expand All @@ -44,6 +47,8 @@ export default function GetResultsByEmail({

const currentSimulation = useCurrentSimulation()

const { getComputedResults } = useEngine()

// Avoid refetching useGetNewsletterSubscriptions when defining an email for the first time
const emailRef = useRef<string>(user?.email ?? '')

Expand All @@ -53,10 +58,15 @@ export default function GetResultsByEmail({

const isSubscribedMainNewsletter =
newsletterSubscriptions?.includes(LIST_MAIN_NEWSLETTER)

const isSubscribedTransportNewsletter = newsletterSubscriptions?.includes(
LIST_NOS_GESTES_TRANSPORT_NEWSLETTER
)

const isSubscribedLogementNewsletter = newsletterSubscriptions?.includes(
LIST_NOS_GESTES_LOGEMENT_NEWSLETTER
)

const { register, handleSubmit, setValue } = useReactHookForm<Inputs>({
defaultValues: {
name: user?.name,
Expand All @@ -74,6 +84,11 @@ export default function GetResultsByEmail({
'newsletter-transports',
newsletterSubscriptions.includes(LIST_NOS_GESTES_TRANSPORT_NEWSLETTER)
)

setValue(
'newsletter-logement',
newsletterSubscriptions.includes(LIST_NOS_GESTES_LOGEMENT_NEWSLETTER)
)
}, [newsletterSubscriptions, setValue])

const { saveSimulation, isPending, isSuccess, isError, error } =
Expand Down Expand Up @@ -102,10 +117,21 @@ export default function GetResultsByEmail({

updateEmail(formEmail)

if (currentSimulation?.computedResults?.bilan === 0) {
// Send an error to Sentry
captureException(
new Error('GetResultsByEmail: computedResults.bilan === 0')
)
}

// We save the simulation (and signify the backend to send the email)
await saveSimulation({
simulation: {
...currentSimulation,
computedResults:
currentSimulation?.computedResults?.bilan === 0
? getComputedResults(currentSimulation.situation)
: currentSimulation.computedResults,
savedViaEmail: true,
},
shouldSendSimulationEmail: true,
Expand Down Expand Up @@ -199,6 +225,21 @@ export default function GetResultsByEmail({
{...register('newsletter-transports')}
/>
)}

{!isSubscribedLogementNewsletter && (
<CheckboxInputGroup
label={
<span>
<Emoji>🏡</Emoji>{' '}
<Trans>
<strong>Nos Gestes Logement</strong> : informez-vous sur
l'impact carbone du logement, en quelques e-mails
</Trans>
</span>
}
{...register('newsletter-logement')}
/>
)}
</div>

<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const questions = [
Il est quasiment impossible de vivre à moins de 2 tonnes dans notre
société actuellement.{' '}
<span className="text-secondary-700">
L’État, le secteur privé et les citoyens
L’État, les collectivités locales, le secteur privé et les citoyens
</span>{' '}
devront tous contribuer pour atteindre cet objectif.
</Trans>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fragment } from 'react'
import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'
import { twMerge } from 'tailwind-merge'
Expand Down Expand Up @@ -28,9 +29,8 @@ export default function RepartitionChart({
className
)}>
{items.map(({ value, shouldBeHighlighted }, index) => (
<>
<Fragment key={`repartition-chart-item-${index}`}>
<BarItem
key={`repartition-chart-item-${index}`}
value={value}
shouldBeHighlighted={shouldBeHighlighted}
maxValue={maxValue}
Expand All @@ -42,7 +42,7 @@ export default function RepartitionChart({
className="z-20"
id={`tooltip-repartition-chart-${id}-${index}`}
/>
</>
</Fragment>
))}
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import PollLoader from '@/components/organisations/PollLoader'
import PollStatistics from '@/components/organisations/PollStatistics'
import Trans from '@/components/translation/Trans'
import Title from '@/design-system/layout/Title'
import { filterExtremes } from '@/helpers/organisations/filterExtremes'
import { filterSimulationRecaps } from '@/helpers/organisations/filterSimulationRecaps'
import { handleMissingComputedResults } from '@/helpers/polls/handleMissingComputedResults'
import { useFetchPollData } from '@/hooks/organisations/useFetchPollData'
import { useHandleRedirectFromLegacy } from '@/hooks/organisations/useHandleRedirectFromLegacy'
import { useEngine } from '@/publicodes-state'
import dayjs from 'dayjs'
import { useParams, useSearchParams } from 'next/navigation'
import { useContext } from 'react'
import { useContext, useMemo } from 'react'
import AdminSection from './_components/AdminSection'
import { FiltersContext } from './_components/FiltersProvider'
import PollNotFound from './_components/PollNotFound'
Expand All @@ -21,6 +24,8 @@ export default function CampagnePage() {

const searchParams = useSearchParams()

const { getComputedResults } = useEngine()

const isRedirectFromLegacy = Boolean(searchParams.get('isRedirectFromLegacy'))

useHandleRedirectFromLegacy()
Expand All @@ -37,10 +42,26 @@ export default function CampagnePage() {

const { ageFilters, postalCodeFilters } = useContext(FiltersContext)

// TODO : Remove this hook when the computed results are fixed
const fixedMissingComputedResultsSimulationRecaps = useMemo(() => {
if (!pollData?.simulationRecaps) return []

return handleMissingComputedResults({
simulationRecaps: pollData?.simulationRecaps ?? [],
getComputedResults,
})
}, [pollData?.simulationRecaps, getComputedResults])

// Remove the values that are too high to avoid polluting the statistics
const simulationRecapsWithoutExtremes = useMemo(
() => filterExtremes(fixedMissingComputedResultsSimulationRecaps),
[fixedMissingComputedResultsSimulationRecaps]
)

const filteredSimulationRecaps =
pollData &&
filterSimulationRecaps({
simulationRecaps: pollData?.simulationRecaps,
simulationRecaps: simulationRecapsWithoutExtremes,
ageFilters,
postalCodeFilters,
})
Expand Down Expand Up @@ -90,15 +111,14 @@ export default function CampagnePage() {
<AdminSection pollData={pollData} />

<PollStatistics
simulationRecaps={
pollData?.simulationRecaps?.filter(({ bilan }) => bilan !== 0) ?? []
}
simulationRecaps={fixedMissingComputedResultsSimulationRecaps}
simulationRecapsWithoutExtremes={simulationRecapsWithoutExtremes}
funFacts={pollData?.funFacts}
title={<Trans>Résultats de campagne</Trans>}
/>

<PollStatisticsFilters
simulationRecaps={pollData?.simulationRecaps ?? []}
simulationRecaps={simulationRecapsWithoutExtremes ?? []}
filteredSimulationRecaps={filteredSimulationRecaps ?? []}
defaultAdditionalQuestions={
pollData?.defaultAdditionalQuestions ?? []
Expand Down

This file was deleted.

Loading

0 comments on commit 3c4f782

Please sign in to comment.