Skip to content

Commit

Permalink
Group expenses my date
Browse files Browse the repository at this point in the history
  • Loading branch information
acuteengle authored and scastiel committed Jan 16, 2024
1 parent 6b6d58e commit fa006e9
Showing 1 changed file with 86 additions and 58 deletions.
144 changes: 86 additions & 58 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ type Props = {
groupId: string
}

function getGroupedExpensesByDate(expenses) {

Check failure on line 19 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'expenses' implicitly has an 'any' type.
return expenses.reduce((result, expense) => {

Check failure on line 20 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'result' implicitly has an 'any' type.

Check failure on line 20 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'expense' implicitly has an 'any' type.
const dateString = formatDate(expense.expenseDate)
result[dateString] = result[dateString] ?? []
result[dateString].push(expense)
return result
}, {})
}

function getOrderedDates(dates) {

Check failure on line 28 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'dates' implicitly has an 'any' type.
return dates.sort(function (a, b) {

Check failure on line 29 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'a' implicitly has an 'any' type.

Check failure on line 29 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'b' implicitly has an 'any' type.
return new Date(b) - new Date(a);

Check failure on line 30 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

Check failure on line 30 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
});
}

export function ExpenseList({
expenses,
currency,
Expand Down Expand Up @@ -44,68 +59,81 @@ export function ExpenseList({
const getParticipant = (id: string) => participants.find((p) => p.id === id)
const router = useRouter()

const groupedExpensesByDate = getGroupedExpensesByDate(expenses)

return expenses.length > 0 ? (
expenses.map((expense) => (
<div
key={expense.id}
className={cn(
'border-t flex justify-between px-4 sm:pr-2 sm:pl-6 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, index) => (
<Fragment key={index}>
{index !== 0 && <>, </>}
<strong>
{
participants.find((p) => p.id === paidFor.participantId)
?.name
}
</strong>
</Fragment>
))}
</div>
</div>
<div className="flex flex-col justify-between items-end">
getOrderedDates(Object.keys(groupedExpensesByDate)).map(dateString => {

Check failure on line 65 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'dateString' implicitly has an 'any' type.
const dateExpenses = groupedExpensesByDate[dateString];
return (
<Fragment key={dateString}>
<div
className={cn(
'tabular-nums whitespace-nowrap',
expense.isReimbursement ? 'italic' : 'font-bold',
)}
className={'border-t text-md pl-3 py-2 font-semibold'}
>
{currency} {(expense.amount / 100).toFixed(2)}
</div>
<div className="text-xs text-muted-foreground">
{formatDate(expense.expenseDate)}
{dateString}
</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>
))
) : (
{dateExpenses.map((expense) => (

Check failure on line 74 in src/app/groups/[groupId]/expenses/expense-list.tsx

View workflow job for this annotation

GitHub Actions / checks

Parameter 'expense' implicitly has an 'any' type.
<div
key={expense.id}
className={cn(
'border-t flex justify-between px-4 sm:pr-2 sm:pl-6 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, index) => (
<Fragment key={index}>
{index !== 0 && <>, </>}
<strong>
{
participants.find((p) => p.id === paidFor.participantId)
?.name
}
</strong>
</Fragment>
))}
</div>
</div>
<div className="flex flex-col justify-between items-end">
<div
className={cn(
'tabular-nums whitespace-nowrap',
expense.isReimbursement ? 'italic' : 'font-bold',
)}
>
{currency} {(expense.amount / 100).toFixed(2)}
</div>
<div className="text-xs text-muted-foreground">
{formatDate(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>
))}
</Fragment>
)
})) : (
<p className="px-6 text-sm py-6">
Your group doesn’t contain any expense yet.{' '}
<Button variant="link" asChild className="-m-4">
Expand Down

0 comments on commit fa006e9

Please sign in to comment.