diff --git a/src/app/groups/page.tsx b/src/app/groups/page.tsx index 86c63ac..0c46d99 100644 --- a/src/app/groups/page.tsx +++ b/src/app/groups/page.tsx @@ -1,5 +1,7 @@ import { RecentGroupList } from '@/app/groups/recent-group-list' import { Button } from '@/components/ui/button' +import { getGroups } from '@/lib/api' +import { Plus } from 'lucide-react' import { Metadata } from 'next' import Link from 'next/link' @@ -8,15 +10,25 @@ export const metadata: Metadata = { } export default async function GroupsPage() { + async function getGroupsAction(groupIds: string[]) { + 'use server' + return getGroups(groupIds) + } + return ( - - Recently visited groups - - - New group - - + + + Recently visited groups + + + + + Create group + + + + ) } diff --git a/src/app/groups/recent-group-list.tsx b/src/app/groups/recent-group-list.tsx index e728193..ef43372 100644 --- a/src/app/groups/recent-group-list.tsx +++ b/src/app/groups/recent-group-list.tsx @@ -1,6 +1,8 @@ 'use client' import { getRecentGroups } from '@/app/groups/recent-groups-helpers' import { Button } from '@/components/ui/button' +import { getGroups } from '@/lib/api' +import { Loader2 } from 'lucide-react' import Link from 'next/link' import { useEffect, useState } from 'react' import { z } from 'zod' @@ -13,31 +15,69 @@ const recentGroupsSchema = z.array( ) type RecentGroups = z.infer -type State = { status: 'pending' } | { status: 'success'; groups: RecentGroups } +type State = + | { status: 'pending' } + | { status: 'partial'; groups: RecentGroups } + | { + status: 'complete' + groups: RecentGroups + groupsDetails: Awaited> + } -export function RecentGroupList() { +type Props = { + getGroupsAction: (groupIds: string[]) => ReturnType +} + +export function RecentGroupList({ getGroupsAction }: Props) { const [state, setState] = useState({ status: 'pending' }) useEffect(() => { const groupsInStorage = getRecentGroups() - setState({ status: 'success', groups: groupsInStorage }) - }, []) + setState({ status: 'partial', groups: groupsInStorage }) + getGroupsAction(groupsInStorage.map((g) => g.id)).then((groupsDetails) => { + setState({ status: 'complete', groups: groupsInStorage, groupsDetails }) + }) + }, [getGroupsAction]) + + if (state.status === 'pending') + return ( + + Loading recent + groups… + + ) return ( - - {state.status === 'pending' ? ( - - Loading recent groups… - - ) : ( - state.groups.map(({ id, name }) => ( - - - {name} + + {state.groups.map((group) => { + const details = + state.status === 'complete' + ? state.groupsDetails.find((d) => d.id === group.id) + : null + return ( + + + + + {group.name} + + {details ? ( + <> + {details._count.participants} participants ·{' '} + {details.currency} + > + ) : ( + <> + + > + )} + + + - )) - )} + ) + })} ) } diff --git a/src/components/group-form.tsx b/src/components/group-form.tsx index 7bf0f8e..0d6ea68 100644 --- a/src/components/group-form.tsx +++ b/src/components/group-form.tsx @@ -128,7 +128,7 @@ export function GroupForm({ {fields.map((item, index) => ( - +
+ Loading recent + groups… +