-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
54 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/app/groups/[groupId]/balances/balances-and-reimbursements.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,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> | ||
</> | ||
) | ||
} |
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
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
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,6 @@ | ||
import { createTRPCRouter } from '@/trpc/init' | ||
import { listGroupBalancesProcedure } from '@/trpc/routers/groups/balances/list.procedure' | ||
|
||
export const groupBalancesRouter = createTRPCRouter({ | ||
list: listGroupBalancesProcedure, | ||
}) |
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,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 } | ||
}) |
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 |
---|---|---|
@@ -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, | ||
}) |