Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "infinite scroll" for expenses #95

Merged
merged 12 commits into from
May 30, 2024
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.47.0",
"react-intersection-observer": "^9.8.0",
"sharp": "^0.33.2",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.7",
Expand Down
78 changes: 78 additions & 0 deletions src/app/groups/[groupId]/expenses/expense-card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use client'
import { ActiveUserBalance } from '@/app/groups/[groupId]/expenses/active-user-balance'
import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
import { Button } from '@/components/ui/button'
import { getGroupExpenses } from '@/lib/api'
import { cn, formatCurrency, formatExpenseDate } from '@/lib/utils'
import { ChevronRight } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { Fragment } from 'react'

type Props = {
expense: Awaited<ReturnType<typeof getGroupExpenses>>[number]
currency: string
groupId: string
}

export function ExpenseCard({ expense, currency, groupId }: Props) {
const router = useRouter()

return (
<div
key={expense.id}
className={cn(
'flex justify-between sm:mx-6 px-4 sm:rounded-lg sm:pr-2 sm:pl-4 py-4 text-sm cursor-pointer hover:bg-accent gap-1 items-stretch',
expense.isReimbursement && 'italic',
)}
onClick={() => {
router.push(`/groups/${groupId}/expenses/${expense.id}/edit`)
}}
>
<CategoryIcon
category={expense.category}
className="w-4 h-4 mr-2 mt-0.5 text-muted-foreground"
/>
<div className="flex-1">
<div className={cn('mb-1', expense.isReimbursement && 'italic')}>
{expense.title}
</div>
<div className="text-xs text-muted-foreground">
Paid by <strong>{expense.paidBy.name}</strong> for{' '}
{expense.paidFor.map((paidFor, index) => (
<Fragment key={index}>
{index !== 0 && <>, </>}
<strong>{paidFor.participant.name}</strong>
</Fragment>
))}
</div>
<div className="text-xs text-muted-foreground">
<ActiveUserBalance {...{ groupId, currency, expense }} />
</div>
</div>
<div className="flex flex-col justify-between items-end">
<div
className={cn(
'tabular-nums whitespace-nowrap',
expense.isReimbursement ? 'italic' : 'font-bold',
)}
>
{formatCurrency(currency, expense.amount)}
</div>
<div className="text-xs text-muted-foreground">
{formatExpenseDate(expense.expenseDate)}
</div>
</div>
<Button
size="icon"
variant="link"
className="self-center hidden sm:flex"
asChild
>
<Link href={`/groups/${groupId}/expenses/${expense.id}/edit`}>
<ChevronRight className="w-4 h-4" />
</Link>
</Button>
</div>
)
}
16 changes: 16 additions & 0 deletions src/app/groups/[groupId]/expenses/expense-list-fetch-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use server'

import { getGroupExpenses } from '@/lib/api'

export async function getGroupExpensesAction(
groupId: string,
options?: { offset: number; length: number },
) {
'use server'

try {
return getGroupExpenses(groupId, options)
} catch {
return null
}
}
177 changes: 87 additions & 90 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use client'
import { ActiveUserBalance } from '@/app/groups/[groupId]/expenses/active-user-balance'
import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
import { ExpenseCard } from '@/app/groups/[groupId]/expenses/expense-card'
import { getGroupExpensesAction } from '@/app/groups/[groupId]/expenses/expense-list-fetch-action'
import { Button } from '@/components/ui/button'
import { SearchBar } from '@/components/ui/search-bar'
import { getGroupExpenses } from '@/lib/api'
import { cn, formatCurrency, formatExpenseDate } from '@/lib/utils'
import { Expense, Participant } from '@prisma/client'
import { Skeleton } from '@/components/ui/skeleton'
import { Participant } from '@prisma/client'
import dayjs, { type Dayjs } from 'dayjs'
import { ChevronRight } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { Fragment, useEffect, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { useInView } from 'react-intersection-observer'

type ExpensesType = NonNullable<
Awaited<ReturnType<typeof getGroupExpensesAction>>
>

type Props = {
expenses: Awaited<ReturnType<typeof getGroupExpenses>>
expensesFirstPage: ExpensesType
expenseCount: number
participants: Participant[]
currency: string
groupId: string
Expand Down Expand Up @@ -47,28 +50,32 @@ function getExpenseGroup(date: Dayjs, today: Dayjs) {
}
}

function getGroupedExpensesByDate(
expenses: Awaited<ReturnType<typeof getGroupExpenses>>,
) {
function getGroupedExpensesByDate(expenses: ExpensesType) {
const today = dayjs()
return expenses.reduce(
(result: { [key: string]: Expense[] }, expense: Expense) => {
const expenseGroup = getExpenseGroup(dayjs(expense.expenseDate), today)
result[expenseGroup] = result[expenseGroup] ?? []
result[expenseGroup].push(expense)
return result
},
{},
)
return expenses.reduce((result: { [key: string]: ExpensesType }, expense) => {
const expenseGroup = getExpenseGroup(dayjs(expense.expenseDate), today)
result[expenseGroup] = result[expenseGroup] ?? []
result[expenseGroup].push(expense)
return result
}, {})
}

export function ExpenseList({
expenses,
expensesFirstPage,
expenseCount,
currency,
participants,
groupId,
}: Props) {
const firstLen = expensesFirstPage.length
const [searchText, setSearchText] = useState('')
const [dataIndex, setDataIndex] = useState(firstLen)
const [dataLen, setDataLen] = useState(firstLen)
const [hasMoreData, setHasMoreData] = useState(expenseCount > firstLen)
const [isFetching, setIsFetching] = useState(false)
const [expenses, setExpenses] = useState(expensesFirstPage)
const { ref, inView } = useInView()

useEffect(() => {
const activeUser = localStorage.getItem('newGroup-activeUser')
const newUser = localStorage.getItem(`${groupId}-newUser`)
Expand All @@ -88,10 +95,43 @@ export function ExpenseList({
}
}, [groupId, participants])

const getParticipant = (id: string) => participants.find((p) => p.id === id)
const router = useRouter()
useEffect(() => {
const fetchNextPage = async () => {
setIsFetching(true)

const newExpenses = await getGroupExpensesAction(groupId, {
offset: dataIndex,
length: dataLen,
})

if (newExpenses !== null) {
const exp = expenses.concat(newExpenses)
setExpenses(exp)
setHasMoreData(exp.length < expenseCount)
setDataIndex(dataIndex + dataLen)
setDataLen(Math.ceil(1.5 * dataLen))
}

setTimeout(() => setIsFetching(false), 500)
}

if (inView && hasMoreData && !isFetching) fetchNextPage()
}, [
dataIndex,
dataLen,
expenseCount,
expenses,
groupId,
hasMoreData,
inView,
isFetching,
])

const groupedExpensesByDate = useMemo(
() => getGroupedExpensesByDate(expenses),
[expenses],
)

const groupedExpensesByDate = getGroupedExpensesByDate(expenses)
return expenses.length > 0 ? (
<>
<SearchBar onChange={(e) => setSearchText(e.target.value)} />
Expand All @@ -114,76 +154,33 @@ export function ExpenseList({
>
{expenseGroup}
</div>
{groupExpenses.map((expense: any) => (
<div
{groupExpenses.map((expense) => (
<ExpenseCard
key={expense.id}
className={cn(
'flex justify-between sm:mx-6 px-4 sm:rounded-lg sm:pr-2 sm:pl-4 py-4 text-sm cursor-pointer hover:bg-accent gap-1 items-stretch',
expense.isReimbursement && 'italic',
)}
onClick={() => {
router.push(`/groups/${groupId}/expenses/${expense.id}/edit`)
}}
>
<CategoryIcon
category={expense.category}
className="w-4 h-4 mr-2 mt-0.5 text-muted-foreground"
/>
<div className="flex-1">
<div
className={cn('mb-1', expense.isReimbursement && 'italic')}
>
{expense.title}
</div>
<div className="text-xs text-muted-foreground">
Paid by{' '}
<strong>{getParticipant(expense.paidById)?.name}</strong>{' '}
for{' '}
{expense.paidFor.map((paidFor: any, index: number) => (
<Fragment key={index}>
{index !== 0 && <>, </>}
<strong>
{
participants.find(
(p) => p.id === paidFor.participantId,
)?.name
}
</strong>
</Fragment>
))}
</div>
<div className="text-xs text-muted-foreground">
<ActiveUserBalance {...{ groupId, currency, expense }} />
</div>
</div>
<div className="flex flex-col justify-between items-end">
<div
className={cn(
'tabular-nums whitespace-nowrap',
expense.isReimbursement ? 'italic' : 'font-bold',
)}
>
{formatCurrency(currency, expense.amount)}
</div>
<div className="text-xs text-muted-foreground">
{formatExpenseDate(expense.expenseDate)}
</div>
</div>
<Button
size="icon"
variant="link"
className="self-center hidden sm:flex"
asChild
>
<Link href={`/groups/${groupId}/expenses/${expense.id}/edit`}>
<ChevronRight className="w-4 h-4" />
</Link>
</Button>
</div>
expense={expense}
currency={currency}
groupId={groupId}
/>
))}
</div>
)
})}
{expenses.length < expenseCount &&
[0, 1, 2].map((i) => (
<div
key={i}
className="border-t flex justify-between items-center px-6 py-4 text-sm"
ref={i === 0 ? ref : undefined}
>
<div className="flex flex-col gap-2">
<Skeleton className="h-4 w-16 rounded-full" />
<Skeleton className="h-4 w-32 rounded-full" />
</div>
<div>
<Skeleton className="h-4 w-16 rounded-full" />
</div>
</div>
))}
</>
) : (
<p className="px-6 text-sm py-6">
Expand Down
3 changes: 1 addition & 2 deletions src/app/groups/[groupId]/expenses/export/json/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { getPrisma } from '@/lib/prisma'
import { prisma } from '@/lib/prisma'
import contentDisposition from 'content-disposition'
import { NextResponse } from 'next/server'

export async function GET(
req: Request,
{ params: { groupId } }: { params: { groupId: string } },
) {
const prisma = await getPrisma()
const group = await prisma.group.findUnique({
where: { id: groupId },
select: {
Expand Down
Loading
Loading