-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Montrer les stats sur les amendement proposé et cosignés (#57)
- Loading branch information
1 parent
17ceafb
commit eea450a
Showing
2 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/depute/[slug]/amendements/AmendementsStatistics.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters