Skip to content

Commit

Permalink
Use tRPC in group stats
Browse files Browse the repository at this point in the history
  • Loading branch information
scastiel committed Oct 19, 2024
1 parent 30c28e2 commit 74c7b76
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 92 deletions.
27 changes: 27 additions & 0 deletions src/app/groups/[groupId]/stats/page.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Totals } from '@/app/groups/[groupId]/stats/totals'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { useTranslations } from 'next-intl'

export function TotalsPageClient({ groupId }: { groupId: string }) {
const t = useTranslations('Stats')

return (
<>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('Totals.title')}</CardTitle>
<CardDescription>{t('Totals.description')}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col space-y-4">
<Totals groupId={groupId} />
</CardContent>
</Card>
</>
)
}
39 changes: 2 additions & 37 deletions src/app/groups/[groupId]/stats/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { cached } from '@/app/cached-functions'
import { Totals } from '@/app/groups/[groupId]/stats/totals'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { getGroupExpenses } from '@/lib/api'
import { getTotalGroupSpending } from '@/lib/totals'
import { TotalsPageClient } from '@/app/groups/[groupId]/stats/page.client'
import { Metadata } from 'next'
import { getTranslations } from 'next-intl/server'
import { notFound } from 'next/navigation'

export const metadata: Metadata = {
title: 'Totals',
Expand All @@ -22,28 +10,5 @@ export default async function TotalsPage({
}: {
params: { groupId: string }
}) {
const t = await getTranslations('Stats')
const group = await cached.getGroup(groupId)
if (!group) notFound()

const expenses = await getGroupExpenses(groupId)
const totalGroupSpendings = getTotalGroupSpending(expenses)

return (
<>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('Totals.title')}</CardTitle>
<CardDescription>{t('Totals.description')}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col space-y-4">
<Totals
group={group}
expenses={expenses}
totalGroupSpendings={totalGroupSpendings}
/>
</CardContent>
</Card>
</>
)
return <TotalsPageClient groupId={groupId} />
}
32 changes: 9 additions & 23 deletions src/app/groups/[groupId]/stats/totals-your-share.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
'use client'
import { getGroup, getGroupExpenses } from '@/lib/api'
import { getTotalActiveUserShare } from '@/lib/totals'
import { cn, formatCurrency } from '@/lib/utils'
import { useLocale, useTranslations } from 'next-intl'
import { useEffect, useState } from 'react'

type Props = {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>
}

export function TotalsYourShare({ group, expenses }: Props) {
export function TotalsYourShare({
totalParticipantShare = 0,
currency,
}: {
totalParticipantShare?: number
currency: string
}) {
const locale = useLocale()
const t = useTranslations('Stats.Totals')
const [activeUser, setActiveUser] = useState('')

useEffect(() => {
const activeUser = localStorage.getItem(`${group.id}-activeUser`)
if (activeUser) setActiveUser(activeUser)
}, [group, expenses])

const totalActiveUserShare =
activeUser === '' || activeUser === 'None'
? 0
: getTotalActiveUserShare(activeUser, expenses)
const currency = group.currency

return (
<div>
<div className="text-muted-foreground">{t('yourShare')}</div>
<div
className={cn(
'text-lg',
totalActiveUserShare < 0 ? 'text-green-600' : 'text-red-600',
totalParticipantShare < 0 ? 'text-green-600' : 'text-red-600',
)}
>
{formatCurrency(currency, Math.abs(totalActiveUserShare), locale)}
{formatCurrency(currency, Math.abs(totalParticipantShare), locale)}
</div>
</div>
)
Expand Down
29 changes: 11 additions & 18 deletions src/app/groups/[groupId]/stats/totals-your-spending.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
'use client'
import { getGroup, getGroupExpenses } from '@/lib/api'
import { useActiveUser } from '@/lib/hooks'
import { getTotalActiveUserPaidFor } from '@/lib/totals'
import { cn, formatCurrency } from '@/lib/utils'
import { useLocale, useTranslations } from 'next-intl'

type Props = {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>
}

export function TotalsYourSpendings({ group, expenses }: Props) {
export function TotalsYourSpendings({
totalParticipantSpendings = 0,
currency,
}: {
totalParticipantSpendings?: number
currency: string
}) {
const locale = useLocale()
const t = useTranslations('Stats.Totals')
const activeUser = useActiveUser(group.id)

const totalYourSpendings =
activeUser === '' || activeUser === 'None'
? 0
: getTotalActiveUserPaidFor(activeUser, expenses)
const currency = group.currency
const balance = totalYourSpendings < 0 ? 'yourEarnings' : 'yourSpendings'
const balance =
totalParticipantSpendings < 0 ? 'yourEarnings' : 'yourSpendings'

return (
<div>
Expand All @@ -29,10 +22,10 @@ export function TotalsYourSpendings({ group, expenses }: Props) {
<div
className={cn(
'text-lg',
totalYourSpendings < 0 ? 'text-green-600' : 'text-red-600',
totalParticipantSpendings < 0 ? 'text-green-600' : 'text-red-600',
)}
>
{formatCurrency(currency, Math.abs(totalYourSpendings), locale)}
{formatCurrency(currency, Math.abs(totalParticipantSpendings), locale)}
</div>
</div>
)
Expand Down
51 changes: 37 additions & 14 deletions src/app/groups/[groupId]/stats/totals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,53 @@
import { TotalsGroupSpending } from '@/app/groups/[groupId]/stats/totals-group-spending'
import { TotalsYourShare } from '@/app/groups/[groupId]/stats/totals-your-share'
import { TotalsYourSpendings } from '@/app/groups/[groupId]/stats/totals-your-spending'
import { getGroup, getGroupExpenses } from '@/lib/api'
import { Skeleton } from '@/components/ui/skeleton'
import { useActiveUser } from '@/lib/hooks'
import { trpc } from '@/trpc/client'

export function Totals({
group,
expenses,
totalGroupSpendings,
}: {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>
totalGroupSpendings: number
}) {
const activeUser = useActiveUser(group.id)
export function Totals({ groupId }: { groupId: string }) {
const activeUser = useActiveUser(groupId)

const participantId =
activeUser && activeUser !== 'None' ? activeUser : undefined
const { data } = trpc.groups.stats.get.useQuery({ groupId, participantId })
const { data: groupData } = trpc.groups.get.useQuery({ groupId })

if (!data || !groupData)
return (
<div className="flex flex-col gap-7">
{[0, 1, 2].map((index) => (
<div key={index}>
<Skeleton className="mt-1 h-3 w-48" />
<Skeleton className="mt-3 h-4 w-20" />
</div>
))}
</div>
)

const {
totalGroupSpendings,
totalParticipantShare,
totalParticipantSpendings,
} = data
const { group } = groupData

return (
<>
<TotalsGroupSpending
totalGroupSpendings={totalGroupSpendings}
currency={group.currency}
/>
{activeUser && activeUser !== 'None' && (
{participantId && (
<>
<TotalsYourSpendings group={group} expenses={expenses} />
<TotalsYourShare group={group} expenses={expenses} />
<TotalsYourSpendings
totalParticipantSpendings={totalParticipantSpendings}
currency={group.currency}
/>
<TotalsYourShare
totalParticipantShare={totalParticipantShare}
currency={group.currency}
/>
</>
)}
</>
Expand Down
2 changes: 2 additions & 0 deletions src/trpc/routers/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { createTRPCRouter } from '@/trpc/init'
import { groupBalancesRouter } from '@/trpc/routers/groups/balances'
import { groupExpensesRouter } from '@/trpc/routers/groups/expenses'
import { getGroupProcedure } from '@/trpc/routers/groups/get.procedure'
import { groupStatsRouter } from '@/trpc/routers/groups/stats'
import { updateGroupProcedure } from '@/trpc/routers/groups/update.procedure'

export const groupsRouter = createTRPCRouter({
expenses: groupExpensesRouter,
balances: groupBalancesRouter,
stats: groupStatsRouter,

get: getGroupProcedure,
update: updateGroupProcedure,
Expand Down
35 changes: 35 additions & 0 deletions src/trpc/routers/groups/stats/get.procedure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getGroupExpenses } from '@/lib/api'
import {
getTotalActiveUserPaidFor,
getTotalActiveUserShare,
getTotalGroupSpending,
} from '@/lib/totals'
import { baseProcedure } from '@/trpc/init'
import { z } from 'zod'

export const getGroupStatsProcedure = baseProcedure
.input(
z.object({
groupId: z.string().min(1),
participantId: z.string().optional(),
}),
)
.query(async ({ input: { groupId, participantId } }) => {
const expenses = await getGroupExpenses(groupId)
const totalGroupSpendings = getTotalGroupSpending(expenses)

const totalParticipantSpendings =
participantId !== undefined
? getTotalActiveUserPaidFor(participantId, expenses)
: undefined
const totalParticipantShare =
participantId !== undefined
? getTotalActiveUserShare(participantId, expenses)
: undefined

return {
totalGroupSpendings,
totalParticipantSpendings,
totalParticipantShare,
}
})
6 changes: 6 additions & 0 deletions src/trpc/routers/groups/stats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createTRPCRouter } from '@/trpc/init'
import { getGroupStatsProcedure } from '@/trpc/routers/groups/stats/get.procedure'

export const groupStatsRouter = createTRPCRouter({
get: getGroupStatsProcedure,
})

0 comments on commit 74c7b76

Please sign in to comment.