Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Montrer les stats sur les amendement proposé et cosignés #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading