1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-10 13:55:03 +00:00

refactor: utilisation de useEngine partout

This commit is contained in:
Alice Dahan 2025-01-31 17:44:06 +01:00 committed by liliced
parent 9b0df7b1c2
commit 20549405d7
12 changed files with 43 additions and 42 deletions

View file

@ -1,10 +1,9 @@
import { DottedName } from 'modele-social'
import { formatValue } from 'publicodes'
import { useContext } from 'react'
import { useTranslation } from 'react-i18next'
import RuleLink from '@/components/RuleLink'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { findReferenceInNode } from '@/utils/publicodes'
export default function CotisationLine({
@ -13,7 +12,7 @@ export default function CotisationLine({
dottedName: DottedName
}) {
const language = useTranslation().i18n.language
const engine = useContext(EngineContext)
const engine = useEngine()
const partSalariale = engine.evaluate(
findReferenceInNode(
dottedName,

View file

@ -1,9 +1,8 @@
import { DottedName } from 'modele-social'
import { utils } from 'publicodes'
import { useContext } from 'react'
import { styled } from 'styled-components'
import { EngineContext, useEngine } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { Grid } from '@/design-system/layout'
import { Link } from '@/design-system/typography/link'
import { Li, Ul } from '@/design-system/typography/list'
@ -131,7 +130,7 @@ const getDomain = (link: string) =>
)
export function RuleReferences({ dottedNames }: { dottedNames: DottedName[] }) {
const engine = useContext(EngineContext)
const engine = useEngine()
return (
<Ul>

View file

@ -1,12 +1,12 @@
import { RuleLink as EngineRuleLink } from '@publicodes/react-ui'
import { DottedName } from 'modele-social'
import Engine from 'publicodes'
import React, { ReactNode, useContext } from 'react'
import React, { ReactNode } from 'react'
import { Link } from '@/design-system/typography/link'
import { useSitePaths } from '@/sitePaths'
import { EngineContext } from './utils/EngineContext'
import { useEngine } from './utils/EngineContext'
// TODO : quicklink -> en cas de variations ou de somme avec un seul élément actif, faire un lien vers cet élément
export default function RuleLink(
@ -20,7 +20,7 @@ export default function RuleLink(
} & Omit<React.ComponentProps<typeof Link>, 'to' | 'children'>
) {
const { absoluteSitePaths } = useSitePaths()
const defaultEngine = useContext(EngineContext)
const defaultEngine = useEngine()
const engineUsed = props?.engine ?? defaultEngine

View file

@ -1,7 +1,6 @@
import { DottedName } from 'modele-social'
import { useContext } from 'react'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { Markdown } from '@/components/utils/markdown'
import HelpButtonWithPopover from '@/design-system/buttons/HelpButtonWithPopover'
import { Spacing } from '@/design-system/layout'
@ -23,8 +22,8 @@ export function ExplicableRule<Names extends string = DottedName>({
bigPopover?: boolean
title?: string
}) {
const engine = useContext(EngineContext)
const rule = engine.getRule(dottedName)
const engine = useEngine()
const rule = engine.getRule(dottedName as DottedName)
const références = useReferences(rule)
if (rule.rawNode.description == null) {

View file

@ -1,10 +1,10 @@
import { NumberFieldProps } from '@react-types/numberfield'
import { ASTNode, parseUnit, serializeUnit, Unit } from 'publicodes'
import { useCallback, useContext, useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { styled } from 'styled-components'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { NumberField } from '@/design-system/field'
import { debounce } from '@/utils'
@ -33,7 +33,7 @@ export default function NumberInput({
)
const { i18n } = useTranslation()
const parsedDisplayedUnit = displayedUnit ? parseUnit(displayedUnit) : unit
const engine = useContext(EngineContext)
const engine = useEngine()
useEffect(() => {
if (value !== currentValue) {
setCurrentValue(

View file

@ -7,11 +7,11 @@ import Engine, {
reduceAST,
RuleNode,
} from 'publicodes'
import React, { useContext } from 'react'
import React from 'react'
import NumberInput from '@/components/conversation/NumberInput'
import SelectCommune from '@/components/conversation/select/SelectCommune'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { DateFieldProps } from '@/design-system/field/DateField'
import { Spacing } from '@/design-system/layout'
import { getMeta } from '@/utils/publicodes'
@ -84,7 +84,7 @@ export default function RuleInput<Names extends string = DottedName>({
engine,
...props
}: Props<Names>) {
const defaultEngine = useContext(EngineContext)
const defaultEngine = useEngine() as Engine<string>
const engineValue = (engine ?? defaultEngine) as Engine<Names>

View file

@ -1,4 +1,4 @@
import { useCallback, useContext, useState } from 'react'
import { useCallback, useState } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { styled } from 'styled-components'

View file

@ -1,14 +1,14 @@
import { useSSRSafeId } from '@react-aria/ssr'
import { DottedName } from 'modele-social'
import { PublicodesExpression, RuleNode } from 'publicodes'
import { useCallback, useContext } from 'react'
import { useCallback } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { IStyledComponent, styled } from 'styled-components'
import { ExplicableRule } from '@/components/conversation/Explicable'
import RuleInput from '@/components/conversation/RuleInput'
import { FadeIn } from '@/components/ui/animate'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { Markdown } from '@/components/utils/markdown'
import { Spacing } from '@/design-system/layout'
import { H3 } from '@/design-system/typography/heading'
@ -32,7 +32,7 @@ export function SubSection({
hideTitle = false,
without = [],
}: SubSectionProp) {
const engine = useContext(EngineContext)
const engine = useEngine()
const ruleTitle = engine.getRule(sectionDottedName)?.title
const nextSteps = useNextQuestions()
const situation = useSelector(situationSelector)
@ -86,7 +86,7 @@ export function SimpleField(props: SimpleFieldProps) {
labelStyle,
} = props
const dispatch = useDispatch()
const engine = useContext(EngineContext)
const engine = useEngine()
const evaluation = engine.evaluate(dottedName)
const rule = engine.getRule(dottedName)
const meta = getMeta<{ requis?: 'oui' | 'non' }>(rule.rawNode, {})

View file

@ -1,12 +1,12 @@
import { BlobProvider } from '@react-pdf/renderer'
import { RuleNode, utils } from 'publicodes'
import { lazy, Suspense, useContext, useRef, useState } from 'react'
import { lazy, Suspense, useRef, useState } from 'react'
import SignaturePad from 'react-signature-pad-wrapper'
import { useTheme } from 'styled-components'
import { TrackPage, useTracking } from '@/components/ATInternetTracking'
import { Condition } from '@/components/EngineValue/Condition'
import { EngineContext, EngineProvider } from '@/components/utils/EngineContext'
import { EngineProvider, useEngine } from '@/components/utils/EngineContext'
import { Message, PopoverWithTrigger } from '@/design-system'
import { Button } from '@/design-system/buttons'
import { Emoji } from '@/design-system/emoji'
@ -30,7 +30,7 @@ type EndBlockProps = {
export default function EndBlock({ fields, missingValues }: EndBlockProps) {
const [isCertified, setCertified] = useState(false)
const [place, setPlace] = useState<string>()
const engine = useContext(EngineContext)
const engine = useEngine()
const signatureRef = useRef<SignaturePad | null>(null)
const tracker = useTracking()
const { colors } = useTheme()

View file

@ -1,8 +1,7 @@
import PDFElement from '@react-pdf/renderer'
import { formatValue, RuleNode } from 'publicodes'
import { useContext } from 'react'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
const { StyleSheet, Text, View } = PDFElement
type FieldsPDFProps = {
@ -10,7 +9,7 @@ type FieldsPDFProps = {
}
export default function FieldsPDF({ fields }: FieldsPDFProps) {
const engine = useContext(EngineContext)
const engine = useEngine()
return (
<>

View file

@ -1,6 +1,7 @@
import { DottedName } from 'modele-social'
import Engine, { PublicodesExpression } from 'publicodes'
import { Fragment, lazy, Suspense, useCallback, useContext } from 'react'
import { Names } from 'modele-social/dist/names'
import Engine, { PublicodesExpression, Rule } from 'publicodes'
import { Fragment, lazy, Suspense, useCallback } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { TrackPage } from '@/components/ATInternetTracking'
@ -9,7 +10,7 @@ import { WhenApplicable } from '@/components/EngineValue/WhenApplicable'
import { WhenNotApplicable } from '@/components/EngineValue/WhenNotApplicable'
import { Appear } from '@/components/ui/animate'
import BrowserOnly from '@/components/utils/BrowserOnly'
import { EngineContext, EngineProvider } from '@/components/utils/EngineContext'
import { EngineProvider, useEngine } from '@/components/utils/EngineContext'
import { Markdown } from '@/components/utils/markdown'
import { usePersistingState } from '@/components/utils/persistState'
import { Button } from '@/design-system/buttons'
@ -32,7 +33,7 @@ const LazyEndBlock = import.meta.env.SSR
: lazy(() => import('./EndBlock'))
export default function PageMobilité() {
const engine = new Engine(formulaire)
const engine = new Engine(formulaire as Record<Names, Rule>)
return (
<>
@ -70,7 +71,7 @@ const useFields = (
const VERSION = hash(JSON.stringify(formulaire))
function FormulairePublicodes() {
const engine = useContext(EngineContext)
const engine = useEngine()
const [situation, setSituation] = usePersistingState<
Record<string, PublicodesExpression>
>(`formulaire-détachement:${VERSION}`, {})
@ -81,7 +82,9 @@ function FormulairePublicodes() {
(dottedName: string, value?: PublicodesExpression) => {
if (value === undefined) {
setSituation((situation) => omit(situation, dottedName))
} else if (engine.getRule(dottedName).rawNode.API === 'commune') {
} else if (
engine.getRule(dottedName as DottedName).rawNode.API === 'commune'
) {
type Value = {
batchUpdate: {
nom: PublicodesExpression
@ -123,7 +126,7 @@ function FormulairePublicodes() {
}),
{}
)
).map((dottedName) => engine.getRule(dottedName))
).map((dottedName) => engine.getRule(dottedName as DottedName))
return (
<>
@ -180,7 +183,7 @@ function FormulairePublicodes() {
<Markdown>
{evaluateQuestion(
engine,
engine.getRule(dottedName)
engine.getRule(dottedName as DottedName)
) ?? ''}
</Markdown>
</div>
@ -193,7 +196,10 @@ function FormulairePublicodes() {
hideDefaultValue
aria-label={
question &&
evaluateQuestion(engine, engine.getRule(dottedName))
evaluateQuestion(
engine,
engine.getRule(dottedName as DottedName)
)
}
/>
{question && type === undefined && description && (

View file

@ -1,4 +1,3 @@
import { useContext } from 'react'
import { Trans } from 'react-i18next'
import { useLocation } from 'react-router-dom'
@ -12,7 +11,7 @@ import Simulation, {
} from '@/components/Simulation'
import { DistributionBranch } from '@/components/simulationExplanation/DistributionDesCotisations'
import { InstitutionsPartenairesArtisteAuteur } from '@/components/simulationExplanation/InstitutionsPartenaires'
import { EngineContext } from '@/components/utils/EngineContext'
import { useEngine } from '@/components/utils/EngineContext'
import { H2 } from '@/design-system/typography/heading'
import { Body } from '@/design-system/typography/paragraphs'
import useSimulationConfig from '@/hooks/useSimulationConfig'
@ -85,7 +84,7 @@ const branches = [
] as const
function RepartitionCotisations() {
const engine = useContext(EngineContext)
const engine = useEngine()
const cotisations = branches.map((branch) => ({
...branch,
value: engine.evaluate(branch.dottedName).nodeValue as number,