🔥 Ajoute une fonction UNSAFE_isNotApplicable

pull/1312/head
Johan Girod 2020-12-16 18:24:01 +01:00
parent 2bbf4738ce
commit 8c0b925956
4 changed files with 49 additions and 35 deletions

View File

@ -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

View File

@ -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 (
<div>

View File

@ -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) {
<div className="main">
<div className="header">
<label htmlFor={dottedName}>
<span className="optionTitle">{rule.question || rule.title}</span>
<p className="ui__ notice">{rule.résumé}</p>
<span className="optionTitle">
{rule.rawNode.question || rule.title}
</span>
<p className="ui__ notice">{rule.rawNode.résumé}</p>
</label>
</div>
<div className="targetInputOrValue">
@ -105,11 +109,13 @@ type WarningProps = {
}
function Warning({ dottedName }: WarningProps) {
const warning = UNSAFE_evaluateRule(useContext(EngineContext), dottedName)
if (!warning.nodeValue) {
return null
}
return <li>{warning.description}</li>
const description = useContext(EngineContext).getRule(dottedName).rawNode
.description
return (
<Condition expression={dottedName}>
<li>{description}</li>
</Condition>
)
}
const ResultLine = styled.div`

View File

@ -189,25 +189,16 @@ export default class Engine<Name extends string = string> {
}
/**
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<DottedName extends string = string>(
export function UNSAFE_isNotApplicable<DottedName extends string = string>(
engine: Engine<DottedName>,
dottedName: DottedName,
modifiers: Object = {}
): EvaluatedRule<DottedName> {
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<boolean>(
dottedName: DottedName
): boolean {
const rule = engine.getRule(dottedName)
return reduceAST<boolean>(
function (isNotApplicable, node, fn) {
if (isNotApplicable) return isNotApplicable
if (!('nodeValue' in node)) {
@ -235,10 +226,30 @@ export function UNSAFE_evaluateRule<DottedName extends string = string>(
}
},
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<DottedName extends string = string>(
engine: Engine<DottedName>,
dottedName: DottedName,
modifiers: Object = {}
): EvaluatedRule<DottedName> {
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,