From 911c71aec265453b67fbd8ebb5ecc8476d485b7d Mon Sep 17 00:00:00 2001 From: Mael Date: Tue, 19 Nov 2019 15:53:15 +0100 Subject: [PATCH] Utilisation du moteur de calcul sur publi.codes, v0 --- source/engine/rules.js | 17 ++++++++----- source/reducers/rootReducer.ts | 17 ++++++++++--- source/sites/publi.codes/App.js | 7 +++++- source/sites/publi.codes/Header.js | 17 +++++++++++++ source/sites/publi.codes/Landing.js | 18 ++++---------- source/sites/publi.codes/Yo.js | 37 +++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 source/sites/publi.codes/Header.js create mode 100644 source/sites/publi.codes/Yo.js diff --git a/source/engine/rules.js b/source/engine/rules.js index bdb8a1ac3..83588f66a 100644 --- a/source/engine/rules.js +++ b/source/engine/rules.js @@ -229,16 +229,21 @@ export let translateAll = (translations, flatRules) => { ) } -const rulesList = Object.entries(rawRules).map(([dottedName, rule]) => ({ - dottedName, - ...rule -})) +const rulesToList = rulesObject => + Object.entries(rulesObject).map(([dottedName, rule]) => ({ + dottedName, + ...rule + })) + +export const buildFlatRules = rulesObject => + rulesToList(rulesObject).map(enrichRule) // On enrichit la base de règles avec des propriétés dérivées de celles du YAML -export let rules = translateAll(translations, rulesList).map(rule => +export let rules = translateAll(translations, rulesToList(rawRules)).map(rule => enrichRule(rule) ) -export let rulesFr = rulesList.map(rule => enrichRule(rule)) + +export let rulesFr = buildFlatRules(rawRules) export let findParentDependencies = (rules, rule) => { // A parent dependency means that one of a rule's parents is not just a namespace holder, it is a boolean question. E.g. is it a fixed-term contract, yes / no diff --git a/source/reducers/rootReducer.ts b/source/reducers/rootReducer.ts index cee27f027..c496e64f6 100644 --- a/source/reducers/rootReducer.ts +++ b/source/reducers/rootReducer.ts @@ -6,12 +6,23 @@ import reduceReducers from 'reduce-reducers' import { combineReducers, Reducer } from 'redux' import { analysisWithDefaultsSelector } from 'Selectors/analyseSelectors' import { SavedSimulation } from 'Selectors/storageSelectors' -import { DottedName, Rule } from 'Types/rule' +import { DottedName } from 'Types/rule' import i18n, { AvailableLangs } from '../i18n' import inFranceAppReducer, { Company } from './inFranceAppReducer' import storageRootReducer from './storageReducer' -function explainedVariable(state: DottedName | null = null, action: Action) { +function rules(state = null, action) { + switch (action.type) { + case 'SET_RULES': + return action.rules + default: + return state + } +} +function explainedVariable( + state: DottedName | null = null, + action: Action +): DottedName | null { switch (action.type) { case 'EXPLAIN_VARIABLE': return action.variableName @@ -267,7 +278,7 @@ const existingCompanyReducer = (state, action: Action) => { const mainReducer = (state, action: Action) => combineReducers({ lang, - rules: defaultTo(null) as Reducer>, + rules, explainedVariable, // We need to access the `rules` in the simulation reducer simulation: (a: Simulation | null = null, b: Action): Simulation | null => diff --git a/source/sites/publi.codes/App.js b/source/sites/publi.codes/App.js index 6b5d43a61..dc512b546 100644 --- a/source/sites/publi.codes/App.js +++ b/source/sites/publi.codes/App.js @@ -18,7 +18,12 @@ function Router({ basename, language }) { language={language} reduxMiddlewares={[]} initialStore={{ - rules + rules, + simulation: { + config: { + objectifs: ['a'] + } + } }} > diff --git a/source/sites/publi.codes/Header.js b/source/sites/publi.codes/Header.js new file mode 100644 index 000000000..0f519f89e --- /dev/null +++ b/source/sites/publi.codes/Header.js @@ -0,0 +1,17 @@ +import React from 'react' +export const Header = () => ( +
+

+ + publi + + + codes + +

+

+ Un nouveau langage de calcul pour encoder les algorithmes d'intérêt + public. +

+
+) diff --git a/source/sites/publi.codes/Landing.js b/source/sites/publi.codes/Landing.js index a0d40ad07..2c2630bf1 100644 --- a/source/sites/publi.codes/Landing.js +++ b/source/sites/publi.codes/Landing.js @@ -3,6 +3,8 @@ import exemple2 from '!!raw-loader!./exemple2.yaml' import ColoredYaml from 'Components/rule/ColoredYaml' import React, { useEffect } from 'react' import emoji from 'react-easy-emoji' +import { Header } from './Header' +import Yo from './Yo' export default function Landing() { useEffect(() => { @@ -21,20 +23,8 @@ export default function Landing() { return (
-
-

- - publi - - - codes - -

-

- Un nouveau langage de calcul pour encoder les algorithmes d'intérêt - public. -

-
+
+

Pourquoi ?

Certains algorithmes sont bien trop importants pour être maintenus diff --git a/source/sites/publi.codes/Yo.js b/source/sites/publi.codes/Yo.js new file mode 100644 index 000000000..e80e42b0f --- /dev/null +++ b/source/sites/publi.codes/Yo.js @@ -0,0 +1,37 @@ +import { buildFlatRules } from 'Engine/rules' +import { safeLoad } from 'js-yaml' +import React, { useEffect, useState } from 'react' +import { useDispatch, useSelector } from 'react-redux' +import { analysisWithDefaultsSelector } from 'Selectors/analyseSelectors' +import { setSimulationConfig } from '../../actions/actions' + +let initialInput = ` +a: + formule: 10 +b: + formule: a + 18 +` + +export default function Yo() { + const [ready, setReady] = useState(false) + const dispatch = useDispatch() + const stateConfig = useSelector(state => state.simulation?.config) + useEffect(() => { + dispatch({ + type: 'SET_RULES', + rules: buildFlatRules(safeLoad(initialInput)) + }) + dispatch(setSimulationConfig({ objectifs: ['b'] })) + setReady(true) + }, []) + return ( +

+ Saissez des formules + {ready && } +
+ ) +} +export const Results = () => { + const analysis = useSelector(state => analysisWithDefaultsSelector(state)) + return
{analysis.targets[0].nodeValue}
+}