diff --git a/mon-entreprise/source/components/simulationExplanation/SalaryExplanation.tsx b/mon-entreprise/source/components/simulationExplanation/SalaryExplanation.tsx index 7738fb086..8f3d35569 100644 --- a/mon-entreprise/source/components/simulationExplanation/SalaryExplanation.tsx +++ b/mon-entreprise/source/components/simulationExplanation/SalaryExplanation.tsx @@ -1,16 +1,14 @@ import Distribution from 'Components/Distribution' import PaySlip from 'Components/PaySlip' import StackedBarChart from 'Components/StackedBarChart' +import * as Animate from 'Components/ui/animate' import { ThemeColorsContext } from 'Components/utils/colors' -import { useEngine, useInversionFail } from 'Components/utils/EngineContext' +import { useInversionFail } from 'Components/utils/EngineContext' import { useContext, useRef } from 'react' import emoji from 'react-easy-emoji' import { Trans, useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' -import * as Animate from 'Components/ui/animate' import { answeredQuestionsSelector } from 'Selectors/simulationSelectors' -import { UNSAFE_evaluateRule } from 'publicodes' -import { DottedName } from 'modele-social' export default function SalaryExplanation() { const showDistributionFirst = !useSelector(answeredQuestionsSelector).length diff --git a/mon-entreprise/source/site/pages/Gérer/AideDéclarationIndépendant/index.tsx b/mon-entreprise/source/site/pages/Gérer/AideDéclarationIndépendant/index.tsx index 4af303125..ce24f8186 100644 --- a/mon-entreprise/source/site/pages/Gérer/AideDéclarationIndépendant/index.tsx +++ b/mon-entreprise/source/site/pages/Gérer/AideDéclarationIndépendant/index.tsx @@ -47,8 +47,7 @@ export default function AideDéclarationIndépendant() { [dispatch, updateSituation] ) const displayForm = - UNSAFE_evaluateRule(engine, 'dirigeant . rémunération totale').nodeValue !== - null + engine.evaluate('dirigeant . rémunération totale').nodeValue !== null return (
diff --git a/mon-entreprise/source/site/pages/Simulateurs/ArtisteAuteur.tsx b/mon-entreprise/source/site/pages/Simulateurs/ArtisteAuteur.tsx index d90cefd7b..cde421c19 100644 --- a/mon-entreprise/source/site/pages/Simulateurs/ArtisteAuteur.tsx +++ b/mon-entreprise/source/site/pages/Simulateurs/ArtisteAuteur.tsx @@ -8,7 +8,7 @@ import 'Components/TargetSelection.css' import Animate from 'Components/ui/animate' import { EngineContext, useEngine } from 'Components/utils/EngineContext' import { DottedName } from 'modele-social' -import { UNSAFE_evaluateRule } from 'publicodes' +import { UNSAFE_isNotApplicable } from 'publicodes' import { createContext, useContext, useEffect, useState } from 'react' import { Trans } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' @@ -64,13 +64,15 @@ function SimpleField({ dottedName }: SimpleFieldProps) { const dispatch = useDispatch() const engine = useEngine() const situation = useSelector(situationSelector) - const rule = UNSAFE_evaluateRule(engine, dottedName) + const isNotApplicable = UNSAFE_isNotApplicable(engine, dottedName) + const evaluation = engine.evaluate(dottedName) + const rule = engine.getRule(dottedName) const initialRender = useContext(InitialRenderContext) if ( - rule.isNotApplicable === true || + isNotApplicable === true || (!(dottedName in situation) && - rule.nodeValue === false && - !(dottedName in rule.missingVariables)) + evaluation.nodeValue === false && + !(dottedName in evaluation.missingVariables)) ) { return null } @@ -81,8 +83,10 @@ function SimpleField({ dottedName }: SimpleFieldProps) {
@@ -105,11 +109,13 @@ type WarningProps = { } function Warning({ dottedName }: WarningProps) { - const warning = UNSAFE_evaluateRule(useContext(EngineContext), dottedName) - if (!warning.nodeValue) { - return null - } - return
  • {warning.description}
  • + const description = useContext(EngineContext).getRule(dottedName).rawNode + .description + return ( + +
  • {description}
  • +
    + ) } const ResultLine = styled.div` diff --git a/publicodes/core/source/index.ts b/publicodes/core/source/index.ts index 1ba321345..318962f52 100644 --- a/publicodes/core/source/index.ts +++ b/publicodes/core/source/index.ts @@ -189,25 +189,16 @@ export default class Engine { } /** - This function allows smother migration to the new Engine API + This function allows to mimic the old 'isApplicable' property on evaluatedRules It will be deprecated when applicability will be encoded as a Literal type - Prefer the use of `engine.evaluate(engine.getRule(dottedName))` */ -export function UNSAFE_evaluateRule( +export function UNSAFE_isNotApplicable( engine: Engine, - dottedName: DottedName, - modifiers: Object = {} -): EvaluatedRule { - const evaluation = simplifyNodeUnit( - engine.evaluate({ valeur: dottedName, ...modifiers }) - ) - const rule = engine.getRule(dottedName) as RuleNode & { - dottedName: DottedName - } - - // HACK while waiting for applicability to have its own type - const isNotApplicable = reduceAST( + dottedName: DottedName +): boolean { + const rule = engine.getRule(dottedName) + return reduceAST( function (isNotApplicable, node, fn) { if (isNotApplicable) return isNotApplicable if (!('nodeValue' in node)) { @@ -235,10 +226,30 @@ export function UNSAFE_evaluateRule( } }, false, - evaluation + engine.evaluate(dottedName) ) +} + +/** + This function allows smother migration to the new Engine API + + It will be deprecated when applicability will be encoded as a Literal type + Prefer the use of `engine.evaluate(engine.getRule(dottedName))` +*/ +export function UNSAFE_evaluateRule( + engine: Engine, + dottedName: DottedName, + modifiers: Object = {} +): EvaluatedRule { + const evaluation = simplifyNodeUnit( + engine.evaluate({ valeur: dottedName, ...modifiers }) + ) + const rule = engine.getRule(dottedName) as RuleNode & { + dottedName: DottedName + } + return { - isNotApplicable, + isNotApplicable: UNSAFE_isNotApplicable(engine, dottedName), ...rule.rawNode, ...rule, ...evaluation,