Skip to content

Commit

Permalink
Use tRPC for balances
Browse files Browse the repository at this point in the history
  • Loading branch information
scastiel committed Oct 19, 2024
1 parent baed02a commit a91df3f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 54 deletions.
75 changes: 75 additions & 0 deletions src/app/groups/[groupId]/balances/balances-and-reimbursements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use client'

import { BalancesList } from '@/app/groups/[groupId]/balances-list'
import { ReimbursementList } from '@/app/groups/[groupId]/reimbursement-list'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { getGroup } from '@/lib/api'
import { trpc } from '@/trpc/client'
import { useTranslations } from 'next-intl'
import { useEffect } from 'react'

export default function BalancesAndReimbursements({
group,
}: {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
}) {
const utils = trpc.useUtils()

useEffect(() => {
// Until we use tRPC more widely and can invalidate the cache on expense
// update, it's easier and safer to invalidate the cache on page load.
utils.groups.balances.invalidate()
}, [utils])

const t = useTranslations('Balances')

const { data, isLoading } = trpc.groups.balances.list.useQuery({
groupId: group.id,
})

return (
<>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('title')}</CardTitle>
<CardDescription>{t('description')}</CardDescription>
</CardHeader>
<CardContent>
{isLoading || !data ? (
<p className="text-muted-foreground text-sm">Loading…</p>
) : (
<BalancesList
balances={data.balances}
participants={group.participants}
currency={group.currency}
/>
)}
</CardContent>
</Card>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('Reimbursements.title')}</CardTitle>
<CardDescription>{t('Reimbursements.description')}</CardDescription>
</CardHeader>
<CardContent>
{isLoading || !data ? (
<p className="text-muted-foreground text-sm">Loading…</p>
) : (
<ReimbursementList
reimbursements={data.reimbursements}
participants={group.participants}
currency={group.currency}
groupId={group.id}
/>
)}
</CardContent>
</Card>
</>
)
}
55 changes: 2 additions & 53 deletions src/app/groups/[groupId]/balances/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { cached } from '@/app/cached-functions'
import { BalancesList } from '@/app/groups/[groupId]/balances-list'
import { ReimbursementList } from '@/app/groups/[groupId]/reimbursement-list'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { getGroupExpenses } from '@/lib/api'
import {
getBalances,
getPublicBalances,
getSuggestedReimbursements,
} from '@/lib/balances'
import BalancesAndReimbursements from '@/app/groups/[groupId]/balances/balances-and-reimbursements'
import { Metadata } from 'next'
import { getTranslations } from 'next-intl/server'
import { notFound } from 'next/navigation'

export const metadata: Metadata = {
Expand All @@ -27,44 +12,8 @@ export default async function GroupPage({
}: {
params: { groupId: string }
}) {
const t = await getTranslations('Balances')
const group = await cached.getGroup(groupId)
if (!group) notFound()

const expenses = await getGroupExpenses(groupId)
const balances = getBalances(expenses)
const reimbursements = getSuggestedReimbursements(balances)
const publicBalances = getPublicBalances(reimbursements)

return (
<>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('title')}</CardTitle>
<CardDescription>{t('description')}</CardDescription>
</CardHeader>
<CardContent>
<BalancesList
balances={publicBalances}
participants={group.participants}
currency={group.currency}
/>
</CardContent>
</Card>
<Card className="mb-4">
<CardHeader>
<CardTitle>{t('Reimbursements.title')}</CardTitle>
<CardDescription>{t('Reimbursements.description')}</CardDescription>
</CardHeader>
<CardContent className="p-0">
<ReimbursementList
reimbursements={reimbursements}
participants={group.participants}
currency={group.currency}
groupId={groupId}
/>
</CardContent>
</Card>
</>
)
return <BalancesAndReimbursements group={group} />
}
2 changes: 1 addition & 1 deletion src/app/groups/[groupId]/reimbursement-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function ReimbursementList({
return (
<div className="text-sm">
{reimbursements.map((reimbursement, index) => (
<div className="border-t px-6 py-4 flex justify-between" key={index}>
<div className="py-4 flex justify-between" key={index}>
<div className="flex flex-col gap-1 items-start sm:flex-row sm:items-baseline sm:gap-4">
<div>
{t.rich('owes', {
Expand Down
6 changes: 6 additions & 0 deletions src/trpc/routers/groups/balances/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createTRPCRouter } from '@/trpc/init'
import { listGroupBalancesProcedure } from '@/trpc/routers/groups/balances/list.procedure'

export const groupBalancesRouter = createTRPCRouter({
list: listGroupBalancesProcedure,
})
19 changes: 19 additions & 0 deletions src/trpc/routers/groups/balances/list.procedure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getGroupExpenses } from '@/lib/api'
import {
getBalances,
getPublicBalances,
getSuggestedReimbursements,
} from '@/lib/balances'
import { baseProcedure } from '@/trpc/init'
import { z } from 'zod'

export const listGroupBalancesProcedure = baseProcedure
.input(z.object({ groupId: z.string().min(1) }))
.query(async ({ input: { groupId } }) => {
const expenses = await getGroupExpenses(groupId)
const balances = getBalances(expenses)
const reimbursements = getSuggestedReimbursements(balances)
const publicBalances = getPublicBalances(reimbursements)

return { balances: publicBalances, reimbursements }
})
2 changes: 2 additions & 0 deletions src/trpc/routers/groups/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createTRPCRouter } from '@/trpc/init'
import { groupBalancesRouter } from '@/trpc/routers/groups/balances'
import { groupExpensesRouter } from '@/trpc/routers/groups/expenses'

export const groupsRouter = createTRPCRouter({
expenses: groupExpensesRouter,
balances: groupBalancesRouter,
})

0 comments on commit a91df3f

Please sign in to comment.