Skip to content

Commit

Permalink
refactor(groupe): use categories to filter group result value to show
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Nov 2, 2023
1 parent f6ad02f commit 7b4e196
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion source/components/publicodesUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ const categoryColorOverride = {
}

export function extractCategories(
rules: any,
rules: NGCRules,
engine: Engine<DottedName>,
valuesFromURL?: any,
parentRule = MODEL_ROOT_RULE_NAME,
Expand Down
74 changes: 42 additions & 32 deletions source/pages/groupe-dashboard/components/VotreEmpreinte.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Category } from '@/components/publicodesUtils'
import { formatValue } from 'publicodes'
import { Trans } from 'react-i18next'
import { ValueObject } from '../hooks/useGetGroupStats'
import PercentageDiff from './PercentageDiff'

export default function VotreEmpreinte({
categoriesFootprints,
categories,
membersLength,
}: {
categoriesFootprints?: Record<string, ValueObject> | undefined
categoriesFootprints: Record<string, ValueObject> | undefined
categories: Category[]
membersLength: number
}) {
console.log('categories', categories)
console.log('categoriesFootprints', categoriesFootprints)
return (
<>
<h2 className="text-lg mb-1 mt-0" data-cypress-id="votre-empreinte-title">
Expand All @@ -24,38 +29,43 @@ export default function VotreEmpreinte({
<ul className="mt-6 pl-0 mb-16">
{Object.entries(categoriesFootprints ?? {}).reduce(
(acc, [key, { icon, title, variation, value }]) => {
if (!icon || !title) return acc
return [
...acc,
<li
key={`cat-${key}`}
className="flex items-center justify-between py-4 border-solid border-0 border-b-[1px] last:border-b-0 border-gray-200"
>
<div className="flex items-center">
<div className="flex-shrink-0 text-2xl">
<span>{icon}</span>
</div>
<div className="ml-4">
<div className="text-md font-bold text-gray-900">
{title}
return title !== undefined &&
icon !== undefined &&
categories.find(
({ dottedName }) => dottedName === title.toLowerCase()
)
? [
...acc,
<li
key={`cat-${key}`}
className="flex items-center justify-between py-4 border-solid border-0 border-b-[1px] last:border-b-0 border-gray-200"
>
<div className="flex items-center">
<div className="flex-shrink-0 text-2xl">
<span>{icon}</span>
</div>
<div className="ml-4">
<div className="text-md font-bold text-gray-900">
{title}
</div>
</div>
{membersLength > 1 && (
<PercentageDiff variation={variation ?? 0} />
)}
</div>
</div>
{membersLength > 1 && (
<PercentageDiff variation={variation ?? 0} />
)}
</div>
<div className="flex items-center gap-4">
<div className="text-sm text-primary bg-primaryLight border-solid border-[1px] border-primaryBorder rounded-[5px] p-1">
<strong>
{formatValue(value / 1000, {
precision: 1,
})}
</strong>{' '}
t
</div>
</div>
</li>,
]
<div className="flex items-center gap-4">
<div className="text-sm text-primary bg-primaryLight border-solid border-[1px] border-primaryBorder rounded-[5px] p-1">
<strong>
{formatValue(value / 1000, {
precision: 1,
})}
</strong>{' '}
t
</div>
</div>
</li>,
]
: acc
},
[] as JSX.Element[]
)}
Expand Down
4 changes: 4 additions & 0 deletions source/pages/groupe-dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { matomoEventUpdateGroupName } from '@/analytics/matomo-events'
import Button from '@/components/groupe/Button'
import InlineTextInput from '@/components/groupe/InlineTextInput'
import Separator from '@/components/groupe/Separator'
import { extractCategories } from '@/components/publicodesUtils'
import AutoCanonicalTag from '@/components/utils/AutoCanonicalTag'
import { useGetCurrentSimulation } from '@/hooks/useGetCurrentSimulation'

Expand Down Expand Up @@ -54,6 +55,8 @@ export default function GroupeDashboard() {
})

const engine = useEngine()
const rules = useSelector((state: AppState) => state.rules)
const categories = extractCategories(rules, engine)

const currentSimulation = useGetCurrentSimulation()

Expand Down Expand Up @@ -224,6 +227,7 @@ export default function GroupeDashboard() {

<VotreEmpreinte
categoriesFootprints={results?.currentMemberAllFootprints}
categories={categories}
membersLength={group?.members?.length}
/>
</main>
Expand Down

0 comments on commit 7b4e196

Please sign in to comment.