From db49a62fe5ead0185558623d98c9875a8c1c3d7d Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Thu, 7 Sep 2023 17:23:45 +0200 Subject: [PATCH] =?UTF-8?q?Enl=C3=A8ve=20les=20async=20si=20pas=20besoin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 10 ++-- .../components/Simulation/SimulationGoal.tsx | 5 ++ .../components/conversation/DateInput.tsx | 19 +++---- .../components/conversation/NumberInput.tsx | 7 +-- .../components/conversation/RuleInput.tsx | 40 +++++---------- .../conversation/useNavigateQuestions.ts | 2 +- site/source/hooks/useNextQuestion.tsx | 30 +++++------- .../store/selectors/simulationSelectors.ts | 40 +++++++-------- yarn.lock | 49 +++++++++---------- 9 files changed, 90 insertions(+), 112 deletions(-) diff --git a/package.json b/package.json index b095dab56..3b2d03697 100644 --- a/package.json +++ b/package.json @@ -67,11 +67,11 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "styled-components": "^6.0.7", - "@publicodes/api": "betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/api&v0", - "publicodes": "betagouv/publicodes#head=publicodes-in-worker&workspace=publicodes&v0", - "publicodes-react": "betagouv/publicodes#head=publicodes-in-worker&workspace=publicodes-react&v0", - "@publicodes/worker": "betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/worker&v0", - "@publicodes/worker-react": "betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/worker-react&v0" + "@publicodes/api": "portal:/home/johan/Projets/publicodes/packages/api", + "publicodes": "portal:/home/johan/Projets/publicodes/packages/core", + "publicodes-react": "portal:/home/johan/Projets/publicodes/packages/react-ui", + "@publicodes/worker": "portal:/home/johan/Projets/publicodes/packages/worker", + "@publicodes/worker-react": "portal:/home/johan/Projets/publicodes/packages/worker-react" }, "packageManager": "yarn@3.6.3", "engines": { diff --git a/site/source/components/Simulation/SimulationGoal.tsx b/site/source/components/Simulation/SimulationGoal.tsx index 38c61f30a..bc6cf862b 100644 --- a/site/source/components/Simulation/SimulationGoal.tsx +++ b/site/source/components/Simulation/SimulationGoal.tsx @@ -50,6 +50,7 @@ export function SimulationGoal({ }: SimulationGoalProps) { const dispatch = useDispatch() const currentUnit = useSelector(targetUnitSelector) + const workerEngine = useWorkerEngine() const evaluation = usePromise( () => @@ -60,7 +61,9 @@ export function SimulationGoal({ }), [workerEngine, dottedName, round, isTypeBoolean, currentUnit] ) + const rule = workerEngine.getRule(dottedName) + const initialRender = useInitialRender() const [isFocused, setFocused] = useState(false) const onChange = useCallback( @@ -72,7 +75,9 @@ export function SimulationGoal({ ) if ( + // Si le champs est non applicable evaluation?.nodeValue === null || + // Ou si il est petit, non editable et qu'il n'est pas défini (small && !editable && evaluation?.nodeValue === undefined) ) { return null diff --git a/site/source/components/conversation/DateInput.tsx b/site/source/components/conversation/DateInput.tsx index f0b8c645a..3f5547fd4 100644 --- a/site/source/components/conversation/DateInput.tsx +++ b/site/source/components/conversation/DateInput.tsx @@ -1,10 +1,11 @@ +// import { useEngine } from '../utils/EngineContext' +import { useWorkerEngine } from '@publicodes/worker-react' import { useCallback } from 'react' import { InputProps } from '@/components/conversation/RuleInput' import { DateField } from '@/design-system/field' import { DateFieldProps } from '@/design-system/field/DateField' -// import { useEngine } from '../utils/EngineContext' import InputSuggestions from './InputSuggestions' export default function DateInput({ @@ -17,7 +18,7 @@ export default function DateInput({ value, type, }: InputProps & { type: DateFieldProps['type'] }) { - // const engine = useEngine() + const engine = useWorkerEngine() const convertDate = (val?: unknown) => { if (!val || typeof val !== 'string') { @@ -46,13 +47,13 @@ export default function DateInput({ {suggestions && ( { - // const value = engine.evaluate(node) - // handleDateChange( - // 'nodeValue' in value && typeof value.nodeValue === 'string' - // ? value.nodeValue - // : undefined - // ) + onFirstClick={async (node) => { + const value = await engine.asyncEvaluate(node) + handleDateChange( + 'nodeValue' in value && typeof value.nodeValue === 'string' + ? value.nodeValue + : undefined + ) }} onSecondClick={() => { onSubmit?.('suggestion') diff --git a/site/source/components/conversation/NumberInput.tsx b/site/source/components/conversation/NumberInput.tsx index e97e8adfd..a23e9af7a 100644 --- a/site/source/components/conversation/NumberInput.tsx +++ b/site/source/components/conversation/NumberInput.tsx @@ -1,6 +1,7 @@ +import { useWorkerEngine } from '@publicodes/worker-react' import { NumberFieldProps } from '@react-types/numberfield' import { ASTNode, parseUnit, serializeUnit, Unit } from 'publicodes' -import { useCallback, useContext, useEffect, useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { styled } from 'styled-components' @@ -30,7 +31,7 @@ export default function NumberInput({ ) const { i18n, t } = useTranslation() const parsedDisplayedUnit = displayedUnit ? parseUnit(displayedUnit) : unit - + const workerEngine = useWorkerEngine() useEffect(() => { if (value !== currentValue) { setCurrentValue( @@ -99,7 +100,7 @@ export default function NumberInput({ className="print-hidden" suggestions={suggestions} onFirstClick={async (node: ASTNode) => { - const evaluatedNode = await asyncEvaluate(node) + const evaluatedNode = await workerEngine.asyncEvaluate(node) if (serializeUnit(evaluatedNode.unit) === serializeUnit(unit)) { setCurrentValue(evaluatedNode.nodeValue as number) } diff --git a/site/source/components/conversation/RuleInput.tsx b/site/source/components/conversation/RuleInput.tsx index 120e336e1..2a412c99d 100644 --- a/site/source/components/conversation/RuleInput.tsx +++ b/site/source/components/conversation/RuleInput.tsx @@ -16,9 +16,11 @@ import React from 'react' import NumberInput from '@/components/conversation/NumberInput' import SelectCommune from '@/components/conversation/select/SelectCommune' +import { DateFieldProps } from '@/design-system/field/DateField' import { getMeta, isNotNull } from '@/utils' import { Choice, MultipleAnswerInput, OuiNonInput } from './ChoicesInput' +import DateInput from './DateInput' import { MultipleChoicesInput } from './MulipleChoicesInput' type InputType = 'radio' | 'card' | 'toggle' | 'select' @@ -91,7 +93,6 @@ export default function RuleInput({ const workerEngine = useWorkerEngine() - // const rule = useAsyncGetRule(dottedName) const rule = workerEngine.getRule(dottedName) // const evaluation = engineValue.evaluate({ valeur: dottedName, ...modifiers }) @@ -107,31 +108,14 @@ export default function RuleInput({ const value = evaluation?.nodeValue - const isMultipleChoices = usePromise( - async () => - rule && isMultiplePossibilities(workerEngine, engineId, dottedName), - [dottedName, engineId, rule, workerEngine] - ) - - console.log('=>', dottedName) + const isMultipleChoices = + rule && isMultiplePossibilities(workerEngine, engineId, dottedName) const choice = usePromise( () => getOnePossibilityOptions(workerEngine, dottedName), [workerEngine, dottedName] ) - dottedName === 'entreprise . activité . nature' && - console.log( - 'choice', - isMultipleChoices, - choice, - rule && isOnePossibility(rule) - ) - - if (!rule || isMultipleChoices === undefined) { - return

Chargement...

- } - const commonProps: InputProps = { dottedName, value, @@ -203,10 +187,12 @@ export default function RuleInput({ } if (rule.rawNode.type?.startsWith('date')) { - return `` + /> + ) } if ( @@ -269,7 +255,7 @@ const getOnePossibilityOptions = async ( variant && (!variant['choix obligatoire'] || variant['choix obligatoire'] === 'non') - const ttt = Object.assign( + const choice = Object.assign( node, variant ? { @@ -296,9 +282,7 @@ const getOnePossibilityOptions = async ( : null ) as Choice - console.log('choice=>', ttt) - - return ttt + return choice } export type RuleWithMultiplePossibilities = RuleNode & { @@ -307,12 +291,12 @@ export type RuleWithMultiplePossibilities = RuleNode & { } } -async function isMultiplePossibilities( +function isMultiplePossibilities( workerEngine: WorkerEngine, engineId: number, // Engine, dottedName: DottedName -): Promise { +): boolean { // return !!(engine.getRule(dottedName) as RuleWithMultiplePossibilities) // .rawNode['plusieurs possibilités'] diff --git a/site/source/components/conversation/useNavigateQuestions.ts b/site/source/components/conversation/useNavigateQuestions.ts index 77401f116..3a7e2ad8b 100644 --- a/site/source/components/conversation/useNavigateQuestions.ts +++ b/site/source/components/conversation/useNavigateQuestions.ts @@ -1,4 +1,4 @@ -import { useWorkerEngine, WorkerEngine } from '@publicodes/worker-react' +import { WorkerEngine } from '@publicodes/worker-react' import { useCallback, useEffect, useRef } from 'react' import { useDispatch, useSelector } from 'react-redux' diff --git a/site/source/hooks/useNextQuestion.tsx b/site/source/hooks/useNextQuestion.tsx index c863eba03..9d75e45ab 100644 --- a/site/source/hooks/useNextQuestion.tsx +++ b/site/source/hooks/useNextQuestion.tsx @@ -1,6 +1,5 @@ import { useWorkerEngine, WorkerEngine } from '@publicodes/worker-react' import { DottedName } from 'modele-social' -import Engine, { RuleNode } from 'publicodes' import { useMemo } from 'react' import { useSelector } from 'react-redux' @@ -12,8 +11,6 @@ import { } from '@/store/selectors/simulationSelectors' import { ImmutableType } from '@/types/utils' -import { usePromise } from './usePromise' - // import { useEngine } from '../components/utils/EngineContext' type MissingVariables = Partial> @@ -77,23 +74,22 @@ export const useNextQuestions = function ( const workerEngine = useWorkerEngine() const missingVariables = useMissingVariables(workerEngines) - const nextQuestions = usePromise( - async () => { - const next = getNextQuestions( - missingVariables, - config.questions ?? {}, - answeredQuestions - ) + return useMemo(() => { + const next = getNextQuestions( + missingVariables, + config.questions ?? {}, + answeredQuestions + ) - const rules = next.map((question) => workerEngine.getRule(question)) + const rules = next.map((question) => workerEngine.getRule(question)) - return next.filter((_, i) => rules[i].rawNode.question !== undefined) - }, - [missingVariables, config.questions, answeredQuestions, workerEngine], - [] as DottedName[] - ) + const nextQuestions = next.filter( + (_, i) => rules[i].rawNode.question !== undefined + ) + console.log('nextQuestions', nextQuestions) - return nextQuestions + return nextQuestions + }, [answeredQuestions, config, missingVariables]) } export function useSimulationProgress(): { diff --git a/site/source/store/selectors/simulationSelectors.ts b/site/source/store/selectors/simulationSelectors.ts index 2e5601b0c..a8cc670d4 100644 --- a/site/source/store/selectors/simulationSelectors.ts +++ b/site/source/store/selectors/simulationSelectors.ts @@ -88,34 +88,30 @@ export const shouldFocusFieldSelector = (state: RootState) => * * For instance, the commune field (API) will fill `commune . nom` `commune . taux versement transport`, `commune . département`, etc. */ -async function treatAPIMissingVariables( +function treatAPIMissingVariables( missingVariables: Partial>, workerEngine: WorkerEngine -): Promise>> { - return ( - await Promise.all( - (Object.entries(missingVariables) as [DottedName, number][]).map( - ([name, value]) => { - const parentName = utils.ruleParent(name) as DottedName - const rule = parentName && workerEngine.getRule(parentName) +): Partial> { + return (Object.entries(missingVariables) as [DottedName, number][]) + .map(([name, value]) => { + const parentName = utils.ruleParent(name) as DottedName + const rule = parentName && workerEngine.getRule(parentName) - return [name, value, parentName, rule.rawNode.API] as const + return [name, value, parentName, rule.rawNode.API] as const + }) + .reduce( + (missings, [name, value, parentName, API]) => { + if (API) { + missings[parentName] = (missings[parentName] ?? 0) + value + + return missings } - ) - ) - ).reduce( - (missings, [name, value, parentName, API]) => { - if (API) { - missings[parentName] = (missings[parentName] ?? 0) + value + missings[name] = value return missings - } - missings[name] = value - - return missings - }, - {} as Partial> - ) + }, + {} as Partial> + ) } const mergeMissing = ( diff --git a/yarn.lock b/yarn.lock index b35392b58..04455edbe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7388,9 +7388,9 @@ __metadata: languageName: node linkType: hard -"@publicodes/api@betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/api&v0": - version: 1.0.0-beta.72 - resolution: "@publicodes/api@https://github.com/betagouv/publicodes.git#workspace=%40publicodes%2Fapi&v0=&commit=6eab7e0019e133a012b216da6d60e3314aec5707" +"@publicodes/api@portal:/home/johan/Projets/publicodes/packages/api::locator=root%40workspace%3A.": + version: 0.0.0-use.local + resolution: "@publicodes/api@portal:/home/johan/Projets/publicodes/packages/api::locator=root%40workspace%3A." dependencies: "@koa/cors": ^3.3.0 "@koa/router": ^10.1.1 @@ -7399,31 +7399,28 @@ __metadata: openapi-validator-middleware: ^3.2.6 peerDependencies: publicodes: ^1.0.0-beta.47 - checksum: 4e0c9c6967e26468612501ea396f93b9ff9304c25d59550b1428baf09e7d1e2b2857716f18d84f582183051e3c7e6078034e7ea8e170314a10fd7b6c0785b96f languageName: node - linkType: hard + linkType: soft -"@publicodes/worker-react@betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/worker-react&v0": - version: 1.0.0-beta.71 - resolution: "@publicodes/worker-react@https://github.com/betagouv/publicodes.git#workspace=%40publicodes%2Fworker-react&v0=&commit=6eab7e0019e133a012b216da6d60e3314aec5707" +"@publicodes/worker-react@portal:/home/johan/Projets/publicodes/packages/worker-react::locator=root%40workspace%3A.": + version: 0.0.0-use.local + resolution: "@publicodes/worker-react@portal:/home/johan/Projets/publicodes/packages/worker-react::locator=root%40workspace%3A." dependencies: - "@publicodes/worker": ^1.0.0-beta.71 + "@publicodes/worker": "workspace:^" peerDependencies: publicodes: ^1.0.0-beta.40 react: ^17 || ^18 react-dom: ^17 || ^18 - checksum: 2428c2acd4a5cde2432c532562044b94fef0e9aa430406cc07c164caa71ec6484cdcf7906face107968d42bba70b1aa86e35f37bbcc63b9b2f000ba17c7e327d languageName: node - linkType: hard + linkType: soft -"@publicodes/worker@betagouv/publicodes#head=publicodes-in-worker&workspace=@publicodes/worker&v0": - version: 1.0.0-beta.71 - resolution: "@publicodes/worker@https://github.com/betagouv/publicodes.git#workspace=%40publicodes%2Fworker&v0=&commit=6eab7e0019e133a012b216da6d60e3314aec5707" +"@publicodes/worker@portal:/home/johan/Projets/publicodes/packages/worker::locator=root%40workspace%3A.": + version: 0.0.0-use.local + resolution: "@publicodes/worker@portal:/home/johan/Projets/publicodes/packages/worker::locator=root%40workspace%3A." peerDependencies: publicodes: ^1.0.0-beta.40 - checksum: 26c9335ef5b4f28f241ed21f2409dc2509293432f070b76524dea877ffdf82f08e349183f87463e9f5c61ee6548640af741c40e5970f13973c8b1a250d39f6c1 languageName: node - linkType: hard + linkType: soft "@react-aria/accordion@npm:^3.0.0-alpha.17": version: 3.0.0-nightly.3854 @@ -25148,28 +25145,26 @@ __metadata: languageName: node linkType: hard -"publicodes-react@betagouv/publicodes#head=publicodes-in-worker&workspace=publicodes-react&v0": - version: 1.0.0-beta.72 - resolution: "publicodes-react@https://github.com/betagouv/publicodes.git#workspace=publicodes-react&v0=&commit=6eab7e0019e133a012b216da6d60e3314aec5707" +"publicodes-react@portal:/home/johan/Projets/publicodes/packages/react-ui::locator=root%40workspace%3A.": + version: 0.0.0-use.local + resolution: "publicodes-react@portal:/home/johan/Projets/publicodes/packages/react-ui::locator=root%40workspace%3A." dependencies: - "@publicodes/worker-react": ^1.0.0-beta.71 + "@publicodes/worker-react": "workspace:^" styled-components: ^6.0.7 peerDependencies: publicodes: ^1.0.0-beta.72 react: ^18 react-dom: ^18 - checksum: e775ffb71a63949fbb2f3f55e97981b376464e3cc075f3c4f4c443e516d66910d7346a1f3952ffc905237680ba532253fde57a24b17a46e48c6940321383dedb languageName: node - linkType: hard + linkType: soft -"publicodes@betagouv/publicodes#head=publicodes-in-worker&workspace=publicodes&v0": - version: 1.0.0-beta.72 - resolution: "publicodes@https://github.com/betagouv/publicodes.git#workspace=publicodes&v0=&commit=6eab7e0019e133a012b216da6d60e3314aec5707" +"publicodes@portal:/home/johan/Projets/publicodes/packages/core::locator=root%40workspace%3A.": + version: 0.0.0-use.local + resolution: "publicodes@portal:/home/johan/Projets/publicodes/packages/core::locator=root%40workspace%3A." peerDependencies: "@types/mocha": ^9.0.0 - checksum: ab67797de175ae5b6991cf6de0163d927160599afb5d002b611b7e39844d06a3c855615cd4ebced250c60e1e9e86335579ba61e417e3fdcac9cf18f5c8ec003d languageName: node - linkType: hard + linkType: soft "pump@npm:3.0.0, pump@npm:^3.0.0": version: 3.0.0