From bf078b293809c48a75980a6bad8c67d6cb2739fc Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Thu, 26 Mar 2020 16:03:19 +0100 Subject: [PATCH] :fire: change l'api du moteur --- publicode/rules/salarié.yaml | 2 +- source/components/Distribution.tsx | 7 +- source/components/PaySlip.tsx | 4 +- source/components/PaySlipSections.tsx | 6 +- source/components/RuleLink.tsx | 2 +- source/components/RulePage.tsx | 9 +- source/components/SalaryExplanation.tsx | 2 +- source/components/SchemeComparaison.tsx | 2 +- source/components/SearchBar.tsx | 12 +- source/components/SearchButton.tsx | 6 +- source/components/conversation/Aide.tsx | 7 +- source/components/conversation/AnswerList.tsx | 2 +- .../components/conversation/Conversation.tsx | 11 +- source/components/conversation/Explicable.tsx | 7 +- .../components/conversation/FormDecorator.tsx | 10 +- source/components/rule/Namespace.tsx | 3 +- source/components/rule/Rule.js | 31 +- source/components/rule/RuleSource.tsx | 2 +- source/engine/RuleInput.tsx | 15 +- source/engine/controls.js | 25 +- source/engine/generateQuestions.ts | 7 - source/engine/getSituationValue.js | 2 +- source/engine/index.ts | 84 ++-- source/engine/mecanismViews/common.tsx | 17 +- source/engine/mecanisms.js | 44 +- source/engine/parseReference.js | 11 +- source/engine/parseRule.tsx | 71 ++- source/engine/parseRules.ts | 77 ++++ source/engine/react.tsx | 11 +- source/engine/ruleUtils.js | 179 ++++++++ source/engine/rules.js | 310 ------------- source/engine/traverse.ts | 168 ------- source/reducers/rootReducer.ts | 5 +- source/selectors/analyseSelectors.ts | 121 ++--- source/selectors/ficheDePaieSelectors.ts | 5 +- source/selectors/repartitionSelectors.ts | 2 +- .../mon-entreprise.fr/pages/Coronavirus.tsx | 2 +- .../pages/Documentation/RulesList.tsx | 6 +- .../AideDéclarationIndépendant/index.tsx | 10 +- .../pages/Simulateurs/ArtisteAuteur.tsx | 7 +- .../pages/Simulateurs/AutoEntrepreneur.tsx | 2 +- .../pages/Simulateurs/Indépendant.tsx | 2 +- source/sites/mon-entreprise.fr/sitePaths.ts | 2 +- source/sites/publi.codes/App.js | 4 +- source/types/rule.ts | 7 +- test/bug-cotisations.test.js | 93 ---- test/contrôles.test.js | 103 ++--- test/conversation.test.js | 105 ++--- test/ficheDePaieSelector.test.js | 3 +- test/generateQuestions.test.js | 418 +++++++++--------- test/inversion.test.js | 177 +++----- test/library.test.js | 56 +-- test/mecanisms.test.js | 109 +++-- test/mécanismes/conversion-unité.yaml | 1 - test/mécanismes/encadrement.yaml | 4 +- test/mécanismes/recalcul.yaml | 3 - test/mécanismes/remplace.yaml | 8 +- test/mécanismes/rend-non-applicable.yaml | 1 - test/mécanismes/régularisation.yaml | 2 - test/mécanismes/variable-temporelle.yaml | 9 - test/real-rules.test.js | 6 +- .../simulations-artiste-auteur.yaml | 4 +- .../simulations-auto-entrepreneur.yaml | 6 +- .../simulations-rémunération-dirigeant.yaml | 4 +- test/regressions/simulations.jest.js | 3 +- test/ruleUtils.test.js | 40 ++ test/rules.test.js | 140 ------ test/rules/co2.yaml | 3 - test/rules/sasu.yaml | 16 +- test/traverse.test.js | 393 ---------------- test/unit-translations.test.js | 6 +- 71 files changed, 1090 insertions(+), 1944 deletions(-) create mode 100644 source/engine/parseRules.ts create mode 100644 source/engine/ruleUtils.js delete mode 100644 source/engine/rules.js delete mode 100644 source/engine/traverse.ts delete mode 100644 test/bug-cotisations.test.js create mode 100644 test/ruleUtils.test.js delete mode 100644 test/rules.test.js delete mode 100644 test/traverse.test.js diff --git a/publicode/rules/salarié.yaml b/publicode/rules/salarié.yaml index 16d2e0ebd..627b04c3f 100644 --- a/publicode/rules/salarié.yaml +++ b/publicode/rules/salarié.yaml @@ -1230,7 +1230,6 @@ contrat salarié . rémunération . primes . activité: contrat salarié . rémunération . primes . activité . base: titre: primes d'activité unité: €/mois - question: Quel est le montant des primes liées à l'activité du salarié ? par défaut: 0 @@ -3136,6 +3135,7 @@ contrat salarié . taxe sur les salaires . barème: contrat salarié . profession spécifique: question: Le salarié exerce t-il l'une des professions suivantes ? + par défaut: non formule: une possibilité: possibilités: diff --git a/source/components/Distribution.tsx b/source/components/Distribution.tsx index f6d1a0b5d..ce35eac17 100644 --- a/source/components/Distribution.tsx +++ b/source/components/Distribution.tsx @@ -1,12 +1,11 @@ import { ThemeColorsContext } from 'Components/utils/colors' import useDisplayOnIntersecting from 'Components/utils/useDisplayOnIntersecting' import Value from 'Components/Value' -import { findRuleByDottedName } from 'Engine/rules' import React, { useContext } from 'react' import emoji from 'react-easy-emoji' import { useSelector } from 'react-redux' import { animated, config, useSpring } from 'react-spring' -import { flatRulesSelector } from 'Selectors/analyseSelectors' +import { parsedRulesSelector } from 'Selectors/analyseSelectors' import répartitionSelector from 'Selectors/repartitionSelectors' import { Rule } from 'Types/rule' import { isIE } from '../utils' @@ -53,12 +52,12 @@ export function DistributionBranch({ icon, distribution }: DistributionBranchProps) { - const rules = useSelector(flatRulesSelector) + const rules = useSelector(parsedRulesSelector) const [intersectionRef, brancheInViewport] = useDisplayOnIntersecting({ threshold: 0.5 }) const { color } = useContext(ThemeColorsContext) - const branche = findRuleByDottedName(rules, dottedName) + const branche = rules[dottedName] const montant = brancheInViewport ? value : 0 const styles = useSpring({ config: ANIMATION_SPRING, diff --git a/source/components/PaySlip.tsx b/source/components/PaySlip.tsx index 7eda9358d..4159c270e 100644 --- a/source/components/PaySlip.tsx +++ b/source/components/PaySlip.tsx @@ -1,6 +1,6 @@ import { ThemeColorsContext } from 'Components/utils/colors' import Value from 'Components/Value' -import { findRuleByDottedName, getRuleFromAnalysis } from 'Engine/rules' +import { getRuleFromAnalysis } from 'Engine/ruleUtils' import React, { Fragment, useContext } from 'react' import { Trans } from 'react-i18next' import { useSelector } from 'react-redux' @@ -64,7 +64,7 @@ export default function PaySlip() { Part salarié {cotisations.map(([brancheDottedName, cotisationList]) => { - let branche = findRuleByDottedName(parsedRules, brancheDottedName) + let branche = parsedRules[brancheDottedName] return (
diff --git a/source/components/PaySlipSections.tsx b/source/components/PaySlipSections.tsx index e7b4dde09..4c1675053 100644 --- a/source/components/PaySlipSections.tsx +++ b/source/components/PaySlipSections.tsx @@ -92,15 +92,15 @@ export let SalaireNetSection = ({ getRule }) => { Salaire net
{netImposable && } - {(avantagesEnNature.nodeValue || retenueTitresRestaurant.nodeValue) && ( + {(avantagesEnNature?.nodeValue || retenueTitresRestaurant?.nodeValue) && ( )} - {!!avantagesEnNature.nodeValue && ( + {!!avantagesEnNature?.nodeValue && ( )} - {!!retenueTitresRestaurant.nodeValue && ( + {!!retenueTitresRestaurant?.nodeValue && ( )} diff --git a/source/components/RuleLink.tsx b/source/components/RuleLink.tsx index 37fcca80d..53fd75e96 100644 --- a/source/components/RuleLink.tsx +++ b/source/components/RuleLink.tsx @@ -1,6 +1,6 @@ import { ThemeColorsContext } from 'Components/utils/colors' import { SitePathsContext } from 'Components/utils/withSitePaths' -import { nameLeaf } from 'Engine/rules' +import { nameLeaf } from 'Engine/ruleUtils' import React, { useContext } from 'react' import { Link } from 'react-router-dom' import { Rule } from 'Types/rule' diff --git a/source/components/RulePage.tsx b/source/components/RulePage.tsx index 97e9840e5..003bb5d08 100644 --- a/source/components/RulePage.tsx +++ b/source/components/RulePage.tsx @@ -1,13 +1,13 @@ import { goBackToSimulation } from 'Actions/actions' import { ScrollToTop } from 'Components/utils/Scroll' -import { decodeRuleName, findRuleByDottedName } from 'Engine/rules.js' +import { decodeRuleName } from 'Engine/ruleUtils.js' import React from 'react' import { Trans } from 'react-i18next' import { connect, useSelector } from 'react-redux' import { Redirect } from 'react-router-dom' import { - flatRulesSelector, noUserInputSelector, + parsedRulesSelector, situationBranchNameSelector } from 'Selectors/analyseSelectors' import { DottedName } from 'Types/rule' @@ -16,7 +16,7 @@ import './RulePage.css' import SearchButton from './SearchButton' export default function RulePage({ match }) { - const flatRules = useSelector(flatRulesSelector) + const parsedRules = useSelector(parsedRulesSelector) const brancheName = useSelector(situationBranchNameSelector) const valuesToShow = !useSelector(noUserInputSelector) let name = match?.params?.name, @@ -36,8 +36,7 @@ export default function RulePage({ match }) { ) } - if (!findRuleByDottedName(flatRules, decodedRuleName)) - return + if (!parsedRules[decodedRuleName]) return return renderRule(decodedRuleName as DottedName) } diff --git a/source/components/SalaryExplanation.tsx b/source/components/SalaryExplanation.tsx index b4c8b3cd9..94ce70af2 100644 --- a/source/components/SalaryExplanation.tsx +++ b/source/components/SalaryExplanation.tsx @@ -2,7 +2,7 @@ import Distribution from 'Components/Distribution' import PaySlip from 'Components/PaySlip' import StackedBarChart from 'Components/StackedBarChart' import { ThemeColorsContext } from 'Components/utils/colors' -import { getRuleFromAnalysis } from 'Engine/rules' +import { getRuleFromAnalysis } from 'Engine/ruleUtils' import React, { useContext, useRef } from 'react' import emoji from 'react-easy-emoji' import { Trans, useTranslation } from 'react-i18next' diff --git a/source/components/SchemeComparaison.tsx b/source/components/SchemeComparaison.tsx index 147077a9e..30b1db75b 100644 --- a/source/components/SchemeComparaison.tsx +++ b/source/components/SchemeComparaison.tsx @@ -10,7 +10,7 @@ import PeriodSwitch from 'Components/PeriodSwitch' import ComparaisonConfig from 'Components/simulationConfigs/rémunération-dirigeant.yaml' import { SitePathsContext } from 'Components/utils/withSitePaths' import Value from 'Components/Value' -import { getRuleFromAnalysis } from 'Engine/rules.js' +import { getRuleFromAnalysis } from 'Engine/ruleUtils.js' import revenusSVG from 'Images/revenus.svg' import { default as React, useCallback, useContext, useState } from 'react' import emoji from 'react-easy-emoji' diff --git a/source/components/SearchBar.tsx b/source/components/SearchBar.tsx index 3abc4f327..22b4e024f 100644 --- a/source/components/SearchBar.tsx +++ b/source/components/SearchBar.tsx @@ -1,11 +1,11 @@ import { SitePathsContext } from 'Components/utils/withSitePaths' -import { parentName } from 'Engine/rules.js' +import { parentName } from 'Engine/ruleUtils.js' import { pick, sortBy, take } from 'ramda' import React, { useContext, useEffect, useState } from 'react' import FuzzyHighlighter, { Highlighter } from 'react-fuzzy-highlighter' import { useTranslation } from 'react-i18next' import { Link, Redirect, useHistory } from 'react-router-dom' -import { Rule } from 'Types/rule' +import { DottedName, Rule } from 'Types/rule' import Worker from 'worker-loader!./SearchBar.worker.js' import { capitalise0 } from '../utils' import './SearchBar.css' @@ -13,7 +13,7 @@ import './SearchBar.css' const worker = new Worker() type SearchBarProps = { - rules: Array + rules: { [name in DottedName]: Rule } showDefaultList: boolean finally?: () => void } @@ -60,7 +60,7 @@ export default function SearchBar({ useEffect(() => { worker.postMessage({ - rules: rules.map( + rules: Object.values(rules).map( pick(['title', 'espace', 'description', 'name', 'dottedName']) ) }) @@ -256,7 +256,9 @@ export default function SearchBar({ i18n.t('noresults', { defaultValue: "Nous n'avons rien trouvé…" })} - {showDefaultList && !input ? renderOptions(rules) : renderOptions()} + {showDefaultList && !input + ? renderOptions(Object.values(rules)) + : renderOptions()} ) } diff --git a/source/components/SearchButton.tsx b/source/components/SearchButton.tsx index 440ec99c8..8e91b1d03 100644 --- a/source/components/SearchButton.tsx +++ b/source/components/SearchButton.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react' import emoji from 'react-easy-emoji' import { Trans } from 'react-i18next' import { useSelector } from 'react-redux' -import { flatRulesSelector } from 'Selectors/analyseSelectors' +import { parsedRulesSelector } from 'Selectors/analyseSelectors' import Overlay from './Overlay' import SearchBar from './SearchBar' @@ -11,7 +11,7 @@ type SearchButtonProps = { } export default function SearchButton({ invisibleButton }: SearchButtonProps) { - const flatRules = useSelector(flatRulesSelector) + const rules = useSelector(parsedRulesSelector) const [visible, setVisible] = useState(false) useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { @@ -34,7 +34,7 @@ export default function SearchButton({ invisibleButton }: SearchButtonProps) {

Chercher dans la documentation

- + ) : invisibleButton ? null : (