From 3e978a69866f8a2dc0ad3da1ac99ec81e3d06a48 Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Wed, 27 May 2020 11:40:25 +0200 Subject: [PATCH] rajoute le bouton 'voir source' sur la documentation publicode --- publicodes/source/components/RuleSource.tsx | 27 ------------- publicodes/source/components/rule/Rule.tsx | 3 +- .../source/components/rule/RuleSource.tsx | 39 +++++++++++++++++++ publicodes/source/index.ts | 5 +++ publicodes/source/parseRule.tsx | 1 + publicodes/source/types/index.ts | 1 + 6 files changed, 48 insertions(+), 28 deletions(-) delete mode 100644 publicodes/source/components/RuleSource.tsx create mode 100644 publicodes/source/components/rule/RuleSource.tsx diff --git a/publicodes/source/components/RuleSource.tsx b/publicodes/source/components/RuleSource.tsx deleted file mode 100644 index 983bacc82..000000000 --- a/publicodes/source/components/RuleSource.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import yaml from 'yaml' -import React from 'react' -import Engine from '../index' -import PublicodeHighlighter from './PublicodeHighlighter' - -type Props = { dottedName: Rules; engine: Engine } -export default function RuleSource({ - engine, - dottedName -}: Props) { - const source = engine.getRules()[dottedName] - if (!source) { - return - } - return ( -
-

Source publicode

- -

- Ci-dessus la règle d'origine, écrite en publicode. Publicode est un - langage déclaratif développé par beta.gouv.fr en partenariat avec - l'Acoss pour encoder les algorithmes d'intérêt public.{' '} - En savoir plus. -

-
- ) -} diff --git a/publicodes/source/components/rule/Rule.tsx b/publicodes/source/components/rule/Rule.tsx index 4b1eb7090..6e02e8476 100644 --- a/publicodes/source/components/rule/Rule.tsx +++ b/publicodes/source/components/rule/Rule.tsx @@ -8,6 +8,7 @@ import { RuleLinkWithContext } from '../RuleLink' import Algorithm from './Algorithm' import RuleHeader from './Header' import References from './References' +import RuleSource from './RuleSource' // let LazySource = React.lazy(() => import('../../../../mon-entreprise/source/components/RuleSource')) @@ -69,7 +70,7 @@ export default function Rule({ )} - + {rule['rend non applicable'] && ( <>

diff --git a/publicodes/source/components/rule/RuleSource.tsx b/publicodes/source/components/rule/RuleSource.tsx new file mode 100644 index 000000000..e8c4ff601 --- /dev/null +++ b/publicodes/source/components/rule/RuleSource.tsx @@ -0,0 +1,39 @@ +import yaml from 'yaml' +import React, { useState } from 'react' +import Engine from '../../index' +import PublicodeHighlighter from '../PublicodeHighlighter' +import emoji from 'react-easy-emoji' + +type Props = { dottedName: Rules; engine: Engine } +export default function RuleSource({ + engine, + dottedName +}: Props) { + const [showSource, setShowSource] = useState(false) + const source = engine.getParsedRules()[dottedName].rawRule + return showSource ? ( +
+

Source publicode

+ +

+ Ci-dessus la règle d'origine, écrite en publicodes. Publicodes est un + langage déclaratif développé par beta.gouv.fr en partenariat avec + l'Acoss pour encoder les algorithmes d'intérêt public.{' '} + En savoir plus. +

+
+ ) : ( +

+ +

+ ) +} diff --git a/publicodes/source/index.ts b/publicodes/source/index.ts index 68e6b534d..64d217556 100644 --- a/publicodes/source/index.ts +++ b/publicodes/source/index.ts @@ -51,6 +51,7 @@ export default class Engine { constructor(rules: string | Rules | ParsedRules) { this.cache = emptyCache() this.cacheWithoutDefault = emptyCache() + this.parsedRules = typeof rules === 'string' || !(Object.values(rules)[0] as any)?.dottedName ? parseRules(rules) @@ -175,6 +176,10 @@ export default class Engine { return this.warnings } + getRules() { + return this.warnings + } + inversionFail(): boolean { return !!this.cache._meta.inversionFail } diff --git a/publicodes/source/parseRule.tsx b/publicodes/source/parseRule.tsx index 7c38cc080..f3ed56685 100644 --- a/publicodes/source/parseRule.tsx +++ b/publicodes/source/parseRule.tsx @@ -64,6 +64,7 @@ export default function( const rule = { ...rawRule, + rawRule, name, dottedName, type: rawRule.type, diff --git a/publicodes/source/types/index.ts b/publicodes/source/types/index.ts index 053a72732..e83f69e8c 100644 --- a/publicodes/source/types/index.ts +++ b/publicodes/source/types/index.ts @@ -39,6 +39,7 @@ export type ParsedRule = Rule & { name: string title: string parentDependencies: Array + rawRule: Rule unit?: Unit summary?: string defaultValue?: any