-
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.
WIP affichage du graphe simple dans le test d'adresse
- Loading branch information
1 parent
c57daad
commit 3918fde
Showing
3 changed files
with
73 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
64 changes: 64 additions & 0 deletions
64
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,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; |
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