Skip to content

Commit

Permalink
Montrer les stats sur les amendement proposé et cosignés (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Sep 14, 2024
1 parent 17ceafb commit eea450a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
106 changes: 106 additions & 0 deletions app/depute/[slug]/amendements/AmendementsStatistics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from "react";

import Stack from "@mui/material/Stack";

import { prisma } from "@/prisma";

async function getDeputeAmendementStatsUnCached(uid: string) {
try {
const etatAmendementsCosignes = await prisma.amendement.groupBy({
by: ["sortAmendement", "etatLibelle"],
where: {
coSignataires: {
some: {
acteurRefUid: uid,
},
},
},
_count: { uid: true },
});

const etatAmendementsProposes = await prisma.amendement.groupBy({
by: ["sortAmendement", "etatLibelle"],
where: {
acteurRefUid: uid,
},
_count: { uid: true },
});

return { etatAmendementsCosignes, etatAmendementsProposes };
} catch (error) {
console.error(`Error fetching amendement from depute ${uid}:`, error);
throw error;
}
}

const getDeputeStatsAmendement = React.cache(getDeputeAmendementStatsUnCached);

const sorts = ["Adopté", "Non soutenu", "Rejeté", "Retiré", "Tombé"];
const etats = ["A discuter", "En traitement", "Irrecevable", "Irrecevable 40"];

export async function AmendementsStatistics({
deputeUid,
}: {
deputeUid: string;
}) {
const { etatAmendementsCosignes, etatAmendementsProposes } =
await getDeputeStatsAmendement(deputeUid);

const nbCoSigne = etatAmendementsCosignes.reduce<{ [key: string]: number }>(
(acc, item) => {
if (item.etatLibelle && etats.includes(item.etatLibelle)) {
const nb = item._count.uid;
return { ...acc, [item.etatLibelle]: nb, total: (acc.total ?? 0) + nb };
}
if (item.sortAmendement && sorts.includes(item.sortAmendement)) {
const nb = item._count.uid;
return {
...acc,
[item.sortAmendement]: nb,
total: (acc.total ?? 0) + nb,
};
}
return acc;
},
{}
);

const nbPropose = etatAmendementsProposes.reduce<{ [key: string]: number }>(
(acc, item) => {
if (item.etatLibelle && etats.includes(item.etatLibelle)) {
const nb = item._count.uid;
return { ...acc, [item.etatLibelle]: nb, total: (acc.total ?? 0) + nb };
}
if (item.sortAmendement && sorts.includes(item.sortAmendement)) {
const nb = item._count.uid;
return {
...acc,
[item.sortAmendement]: nb,
total: (acc.total ?? 0) + nb,
};
}
return acc;
},
{}
);
return (
<table>
<thead>
<tr>
<td>Etat</td>
<td>proposés</td>
<td>signés</td>
</tr>
</thead>
<tbody>
{[...sorts, ...etats].map((state) => (
<tr key={state}>
<td>{state}</td>
<td>{nbPropose[state] ?? 0}</td>
<td>{nbCoSigne[state] ?? 0}</td>
</tr>
))}
</tbody>
</table>
);
}
5 changes: 4 additions & 1 deletion app/depute/[slug]/amendements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import Stack from "@mui/material/Stack";

import AmendementCard from "@/components/folders/AmendementCard";
import { prisma } from "@/prisma";
import { AmendementsStatistics } from "./AmendementsStatistics";

async function getDeputeAmendementUnCached(slug: string) {
try {
return await prisma.acteur.findFirst({
where: { slug },
select: {
uid: true,
amendements: {
include: { texteLegislatifRef: { select: { numNotice: true } } },
},
Expand All @@ -30,11 +32,12 @@ export default async function Amendements({
}) {
const deputeWithAmendements = await getDeputeAmendement(params.slug);

const { amendements } = deputeWithAmendements!;
const { amendements, uid } = deputeWithAmendements!;

return (
<Stack>
<p>Amendements</p>
<AmendementsStatistics deputeUid={uid} />
{amendements &&
amendements
.sort((a, b) =>
Expand Down

0 comments on commit eea450a

Please sign in to comment.