-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Affiche le comparateur uniquement pour le Collectif
- Loading branch information
1 parent
6f41a59
commit c3433c1
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/components/ComparateurPublicodes/ComparateurPublicodesWidget.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { fr } from '@codegouvfr/react-dsfr'; | ||
import React from 'react'; | ||
|
||
import { FormProvider } from '@components/form/publicodes/FormProvider'; | ||
import Heading from '@components/ui/Heading'; | ||
import Link from '@components/ui/Link'; | ||
import { Table, type ColumnDef } from '@components/ui/Table'; | ||
import { LocationInfoResponse } from '@pages/api/location-infos'; | ||
import { postFetchJSON } from '@utils/network'; | ||
import { ObjectEntries } from '@utils/typescript'; | ||
|
||
import { addresseToPublicodesRules } from './ComparateurPublicodes'; | ||
import { modesDeChauffage } from './modes-de-chauffage'; | ||
import { simulatorTabs } from './Placeholder'; | ||
import useSimulatorEngine from './useSimulatorEngine'; | ||
|
||
type ComparateurPublicodesWidgetProps = React.HTMLAttributes<HTMLDivElement> & { | ||
coords?: [longitude?: number, latitude?: number]; | ||
city?: string; | ||
cityCode?: string; | ||
address?: string; | ||
}; | ||
|
||
export type TabId = (typeof simulatorTabs)[number]['tabId']; | ||
const ComparateurPublicodesWidget: React.FC<ComparateurPublicodesWidgetProps> = ({ | ||
children, | ||
coords, | ||
city, | ||
cityCode, | ||
address, | ||
...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]); | ||
|
||
const modesDeChauffageToDisplay = modesDeChauffage | ||
.filter(({ type }) => (type as any).includes('collectif')) | ||
.map((typeInstallation) => ({ | ||
id: typeInstallation.label, | ||
label: typeInstallation.label, | ||
bilan: engine.getFieldAsNumber(`Bilan x ${typeInstallation.coutPublicodeKey} . total avec aides`), | ||
emissionsCO2: engine.getFieldAsNumber(`env . Installation x ${typeInstallation.emissionsCO2PublicodesKey} . Total`), | ||
})) | ||
.sort((a, b) => a.bilan - b.bilan); | ||
|
||
const columns: ColumnDef<{ label: string; bilan: number }>[] = [ | ||
{ | ||
headerName: 'Mode de chauffage', | ||
field: 'label', | ||
flex: 3, | ||
}, | ||
{ | ||
headerName: 'Cout annuel chauffage', | ||
flex: 2, | ||
field: 'bilan', | ||
renderCell: ({ value }) => value.toLocaleString('fr-FR', { currency: 'EUR', maximumFractionDigits: 0, style: 'currency' }), | ||
}, | ||
{ | ||
headerName: 'Emissions CO2', | ||
flex: 2, | ||
field: 'emissionsCO2', | ||
renderCell: ({ value }) => `${value.toLocaleString('fr-FR', { maximumFractionDigits: 0 })} kgCO2e`, | ||
}, | ||
]; | ||
|
||
return ( | ||
<div {...props}> | ||
<FormProvider engine={engine}> | ||
<Heading as="h2" size="h4"> | ||
Comparaison des modes de chauffage | ||
</Heading> | ||
<Table | ||
style={{ width: '100%' }} | ||
getRowClassName={({ id }) => (id === 'Réseau de chaleur' ? fr.cx('fr-text--bold') : '')} | ||
columns={columns} | ||
hideFooter | ||
rows={modesDeChauffageToDisplay} | ||
autosizeOnMount | ||
disableRowSelectionOnClick | ||
autoHeight | ||
pageSize={20} | ||
/> | ||
<div className={fr.cx('fr-text--sm', 'fr-mt-2w')} style={{ textAlign: 'right', fontStyle: 'italic' }}> | ||
Accéder au{' '} | ||
<Link href={`/outils/comparateur-performances?address=${encodeURIComponent(address as string)}`}>comparateur complet</Link> | ||
</div> | ||
</FormProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ComparateurPublicodesWidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters