Skip to content

Commit

Permalink
refactor(utils): clean getSimulationResults.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Nov 2, 2023
1 parent c2d6e70 commit f6ad02f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
5 changes: 2 additions & 3 deletions source/pages/groupe-dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
4 changes: 1 addition & 3 deletions source/pages/rejoindre-groupe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ export default function RejoindreGroupe() {
return
}

const results = getSimulationResults({
engine,
})
const results = getSimulationResults({ engine })

try {
await fetchAddUserToGroup({
Expand Down
6 changes: 2 additions & 4 deletions source/sites/publicodes/Simulateur.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion source/utils/fetchUpdateGroupMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Props = {
group: Group
userId: string
simulation: SavedSimulation
results: SimulationResults
results: SimulationResults | undefined
}

export const fetchUpdateGroupMember = async ({
Expand Down
37 changes: 16 additions & 21 deletions source/utils/getSimulationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f6ad02f

Please sign in to comment.