From f6ad02f8c47bf32c0f7395cb414a27866858b313 Mon Sep 17 00:00:00 2001 From: Emile Rolley Date: Thu, 2 Nov 2023 11:29:53 +0100 Subject: [PATCH] refactor(utils): clean getSimulationResults.ts --- source/pages/groupe-dashboard/index.tsx | 5 ++-- source/pages/rejoindre-groupe/index.tsx | 4 +-- source/sites/publicodes/Simulateur.tsx | 6 ++-- source/utils/fetchUpdateGroupMember.ts | 2 +- source/utils/getSimulationResults.ts | 37 +++++++++++-------------- 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/source/pages/groupe-dashboard/index.tsx b/source/pages/groupe-dashboard/index.tsx index 530ae8fd63..25751e299b 100644 --- a/source/pages/groupe-dashboard/index.tsx +++ b/source/pages/groupe-dashboard/index.tsx @@ -57,9 +57,8 @@ export default function GroupeDashboard() { const currentSimulation = useGetCurrentSimulation() - const resultsOfUser = getSimulationResults({ - engine, - }) + const resultsOfUser = getSimulationResults({ engine }) + const handleFetchGroup = useCallback(async () => { try { const response = await fetch(`${GROUP_URL}/${groupId}`) diff --git a/source/pages/rejoindre-groupe/index.tsx b/source/pages/rejoindre-groupe/index.tsx index ed448eb980..1c65e27f4a 100644 --- a/source/pages/rejoindre-groupe/index.tsx +++ b/source/pages/rejoindre-groupe/index.tsx @@ -111,9 +111,7 @@ export default function RejoindreGroupe() { return } - const results = getSimulationResults({ - engine, - }) + const results = getSimulationResults({ engine }) try { await fetchAddUserToGroup({ diff --git a/source/sites/publicodes/Simulateur.tsx b/source/sites/publicodes/Simulateur.tsx index 635879288a..40f98b956e 100644 --- a/source/sites/publicodes/Simulateur.tsx +++ b/source/sites/publicodes/Simulateur.tsx @@ -32,7 +32,7 @@ import { enquêteSelector } from '@/sites/publicodes/enquête/enquêteSelector' import { questionConfig } from '@/sites/publicodes/questionConfig' import ScoreBar from '@/sites/publicodes/ScoreBar' import { getQuestionURLSearchParams } from '@/sites/publicodes/utils' -import { Group, SimulationResults } from '@/types/groups' +import { Group } from '@/types/groups' import { fetchUpdateGroupMember } from '@/utils/fetchUpdateGroupMember' import { getSimulationResults } from '@/utils/getSimulationResults' import { motion } from 'framer-motion' @@ -304,9 +304,7 @@ const MainSimulationEnding = ({ rules, engine }) => { // Should use setSituationForValidKeys ? engine.setSituation(currentSimulation?.situation) - const results: SimulationResults = getSimulationResults({ - engine, - }) + const results = getSimulationResults({ engine }) try { await fetchUpdateGroupMember({ diff --git a/source/utils/fetchUpdateGroupMember.ts b/source/utils/fetchUpdateGroupMember.ts index c3f6db4343..2c44031644 100644 --- a/source/utils/fetchUpdateGroupMember.ts +++ b/source/utils/fetchUpdateGroupMember.ts @@ -6,7 +6,7 @@ type Props = { group: Group userId: string simulation: SavedSimulation - results: SimulationResults + results: SimulationResults | undefined } export const fetchUpdateGroupMember = async ({ diff --git a/source/utils/getSimulationResults.ts b/source/utils/getSimulationResults.ts index e60aebf8f6..cd0e10e450 100644 --- a/source/utils/getSimulationResults.ts +++ b/source/utils/getSimulationResults.ts @@ -5,31 +5,26 @@ import Engine from 'publicodes' export const getSimulationResults = ({ engine, }: { - engine: Engine -}): SimulationResults => { - let resultsObject - - if (engine) { - resultsObject = {} - - const rules = engine.getParsedRules() + engine: Engine | undefined +}): SimulationResults | undefined => { + if (!engine) { + return undefined + } - const categories = extractCategories(rules, engine) + const resultsObject = {} as SimulationResults + const rules = engine.getParsedRules() + const categories = extractCategories(rules, engine) - categories.forEach((category) => { - resultsObject[ - category.name === 'transport' ? 'transports' : category.name - ] = ( - Math.round(((category.nodeValue as number) ?? 0) / 10) / 100 - ).toFixed(2) - }) + categories.forEach((category) => { + resultsObject[category.name] = ( + Math.round(((category.nodeValue as number) ?? 0) / 10) / 100 + ).toFixed(2) + }) - const evaluation = engine.evaluate('bilan') - const { nodeValue: rawNodeValue, unit } = evaluation - const valueTotal = correctValue({ nodeValue: rawNodeValue, unit }) + const { nodeValue: rawNodeValue, unit } = engine.evaluate('bilan') + const valueTotal = correctValue({ nodeValue: rawNodeValue, unit }) - resultsObject.total = ((valueTotal as number) / 10 / 100).toFixed(2) - } + resultsObject.total = ((valueTotal as number) / 10 / 100).toFixed(2) return resultsObject }