From baa39e81f05f54fd69a3aa0208b8adf832c49e4b Mon Sep 17 00:00:00 2001 From: nzambello Date: Fri, 23 Jun 2023 12:27:11 +0200 Subject: [PATCH] feat: add totals in reports --- app/routes/reports.tsx | 43 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/app/routes/reports.tsx b/app/routes/reports.tsx index 18e69c8..2260ab8 100644 --- a/app/routes/reports.tsx +++ b/app/routes/reports.tsx @@ -48,13 +48,21 @@ export async function loader({ request }: LoaderArgs) { await updateDuration(user.id); + const timeByProject = await getTimeEntriesByDateAndProject({ + userId: user.id, + dateFrom, + dateTo + }); + + const total = timeByProject.reduce( + (acc, curr) => acc + (curr._sum.duration || 0), + 0 + ); + return json({ user, - timeByProject: await getTimeEntriesByDateAndProject({ - userId: user.id, - dateFrom, - dateTo - }), + timeByProject, + total, projects: await getProjects({ userId: user.id }) }); } @@ -228,8 +236,8 @@ export default function ReportPage() { Project - Time - {hourlyRate && Billing} + Time + {hourlyRate && Billing} @@ -240,7 +248,7 @@ export default function ReportPage() { }[] ).map((projectData) => ( - + ))} + {!!reports.data?.timeByProject?.length && ( + + + Totals + {(reports.data.total / 1000 / 60 / 60).toFixed(2)} h + {hourlyRate && ( + + {( + (reports.data.total * hourlyRate) / + 1000 / + 60 / + 60 + ).toFixed(2)}{' '} + {reports.data?.user?.currency ?? '€'} + + )} + + + )} )}