rajoute le bouton 'voir source' sur la documentation publicode

pull/1058/head
Johan Girod 2020-05-27 11:40:25 +02:00
parent c766b36333
commit 3e978a6986
6 changed files with 48 additions and 28 deletions

View File

@ -1,27 +0,0 @@
import yaml from 'yaml'
import React from 'react'
import Engine from '../index'
import PublicodeHighlighter from './PublicodeHighlighter'
type Props<Rules extends string> = { dottedName: Rules; engine: Engine<Rules> }
export default function RuleSource<Rules extends string>({
engine,
dottedName
}: Props<Rules>) {
const source = engine.getRules()[dottedName]
if (!source) {
return
}
return (
<section>
<h3>Source publicode</h3>
<PublicodeHighlighter source={yaml.stringify({ [dottedName]: source })} />
<p className="ui__ notice">
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.{' '}
<a href="https://publi.codes">En savoir plus.</a>
</p>
</section>
)
}

View File

@ -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({
)}
<Algorithm rule={rule} />
<RuleSource key={dottedName} dottedName={dottedName} engine={engine} />
{rule['rend non applicable'] && (
<>
<h2>

View File

@ -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<Rules extends string> = { dottedName: Rules; engine: Engine<Rules> }
export default function RuleSource<Rules extends string>({
engine,
dottedName
}: Props<Rules>) {
const [showSource, setShowSource] = useState(false)
const source = engine.getParsedRules()[dottedName].rawRule
return showSource ? (
<section>
<h3>Source publicode</h3>
<PublicodeHighlighter source={yaml.stringify({ [dottedName]: source })} />
<p className="ui__ notice">
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.{' '}
<a href="https://publi.codes">En savoir plus.</a>
</p>
</section>
) : (
<p
css={`
text-align: right;
`}
>
<button
className="ui__ simple small button"
onClick={() => setShowSource(true)}
>
{emoji('✍️')} Voir la règle publicodes
</button>
</p>
)
}

View File

@ -51,6 +51,7 @@ export default class Engine<Names extends string> {
constructor(rules: string | Rules<Names> | ParsedRules<Names>) {
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<Names extends string> {
return this.warnings
}
getRules() {
return this.warnings
}
inversionFail(): boolean {
return !!this.cache._meta.inversionFail
}

View File

@ -64,6 +64,7 @@ export default function<Names extends string>(
const rule = {
...rawRule,
rawRule,
name,
dottedName,
type: rawRule.type,

View File

@ -39,6 +39,7 @@ export type ParsedRule<Name extends string = string> = Rule & {
name: string
title: string
parentDependencies: Array<any>
rawRule: Rule
unit?: Unit
summary?: string
defaultValue?: any