Skip to content

Commit

Permalink
WIP affichage du graphe simple dans le test d'adresse
Browse files Browse the repository at this point in the history
  • Loading branch information
martinratinaud committed Nov 19, 2024
1 parent c57daad commit 3918fde
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ComparateurPublicodesProps = React.HTMLAttributes<HTMLDivElement> & {

export type TabId = (typeof simulatorTabs)[number]['tabId'];

const addresseToPublicodesRules = {
export const addresseToPublicodesRules = {
'caractéristique réseau de chaleur . contenu CO2': (infos) => infos.nearestReseauDeChaleur?.['contenu CO2'],
'caractéristique réseau de chaleur . contenu CO2 ACV': (infos) => infos.nearestReseauDeChaleur?.['contenu CO2 ACV'],
'caractéristique réseau de chaleur . livraisons totales': (infos) => infos.nearestReseauDeChaleur?.['livraisons_totale_MWh'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';

import { FormProvider } from '@components/form/publicodes/FormProvider';
import { LocationInfoResponse } from '@pages/api/location-infos';
import { postFetchJSON } from '@utils/network';
import { ObjectEntries } from '@utils/typescript';

import { addresseToPublicodesRules } from './ComparateurPublicodes';
import { simulatorTabs } from './Placeholder';
import useSimulatorEngine from './useSimulatorEngine';

type ComparateurPublicodesWidgetProps = React.HTMLAttributes<HTMLDivElement> & {
coords?: [longitude?: number, latitude?: number];
city?: string;
cityCode?: string;
};

export type TabId = (typeof simulatorTabs)[number]['tabId'];

const ComparateurPublicodesWidget: React.FC<ComparateurPublicodesWidgetProps> = ({
children,
className,
coords,
city,
cityCode,
...props
}) => {
const engine = useSimulatorEngine();

React.useEffect(() => {
if (!coords || !city || !cityCode) {
return;
}
const loadInfos = async () => {
const infos: LocationInfoResponse = await postFetchJSON('/api/location-infos', {
lon: coords[0],
lat: coords[1],
city,
cityCode,
});

engine.setSituation(
ObjectEntries(addresseToPublicodesRules).reduce(
(acc, [key, infoGetter]) => ({
...acc,
[key]: infoGetter(infos) ?? null,
}),
{}
)
);
};
loadInfos();
}, [coords, city, cityCode]);

return (
<div style={{ border: '1px solid red' }} {...props}>
<FormProvider engine={engine}>
<pre>{JSON.stringify(engine.getSituation(), null, 2)}</pre>
</FormProvider>
</div>
);
};

export default ComparateurPublicodesWidget;
8 changes: 8 additions & 0 deletions src/components/EligibilityForm/EligibilityFormContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Alert } from '@codegouvfr/react-dsfr/Alert';
import Image from 'next/image';
import { useCallback, useMemo, useState } from 'react';

import ComparateurPublicodesWidget from '@components/ComparateurPublicodes/ComparateurPublicodesWidget';
import Map from '@components/Map/Map';
import MarkdownWrapper from '@components/MarkdownWrapper';
import Box from '@components/ui/Box';
Expand Down Expand Up @@ -154,6 +155,13 @@ const EligibilityFormContact = ({ addressData, cardMode, onSubmit }: Eligibility
/>
)
)}
{process.env.NEXT_PUBLIC_FLAG_ENABLE_COMPARATEUR === 'true' && (
<ComparateurPublicodesWidget
coords={[addressData?.coords?.lon, addressData?.coords?.lat]}
city={addressData.geoAddress?.properties.city}
cityCode={addressData.geoAddress?.properties.citycode}
/>
)}
</ContactFormContentWrapper>
<ContactFormContentWrapper>
{!cardMode && (
Expand Down

0 comments on commit 3918fde

Please sign in to comment.