Skip to content

Commit

Permalink
feature: added locks to ensure only 1 user can get the group expenses…
Browse files Browse the repository at this point in the history
… created
  • Loading branch information
neonshobhit committed Mar 8, 2024
1 parent 10263bb commit 42797c2
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 88 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"next13-progressbar": "^1.1.1",
"openai": "^4.25.0",
"pg": "^8.11.3",
"prisma": "^5.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.47.0",
Expand All @@ -53,13 +54,12 @@
"ts-pattern": "^5.0.6",
"uuid": "^9.0.1",
"vaul": "^0.8.0",
"zod": "^3.22.4",
"prisma": "^5.7.0"
"zod": "^3.22.4"
},
"devDependencies": {
"@total-typescript/ts-reset": "^0.5.1",
"@types/content-disposition": "^0.5.8",
"@types/node": "^20",
"@types/node": "^20.11.20",
"@types/pg": "^8.10.9",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
Expand Down
10 changes: 9 additions & 1 deletion src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCategories,
getExpense,
updateExpense,
archiveExpenseStateUpdate,
} from '@/lib/api'
import { getRuntimeFeatureFlags } from '@/lib/featureFlags'
import { expenseFormSchema } from '@/lib/schemas'
Expand Down Expand Up @@ -36,7 +37,13 @@ export default async function EditExpensePage({

async function deleteExpenseAction() {
'use server'
await deleteExpense(expenseId)
await deleteExpense(groupId, expenseId)
redirect(`/groups/${groupId}`)
}

async function archiveExpenseStateUpdateAction(state: boolean) {
'use server'
await archiveExpenseStateUpdate(expenseId, state)
redirect(`/groups/${groupId}`)
}

Expand All @@ -48,6 +55,7 @@ export default async function EditExpensePage({
categories={categories}
onSubmit={updateExpenseAction}
onDelete={deleteExpenseAction}
onArchive={archiveExpenseStateUpdateAction}
runtimeFeatureFlags={await getRuntimeFeatureFlags()}
/>
</Suspense>
Expand Down
5 changes: 5 additions & 0 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export function ExpenseList({
<div
className={cn('mb-1', expense.isReimbursement && 'italic')}
>
<span>
<strong>
{(expense.isArchive ? "Archived \u00B7 " :"")}
</strong>
</span>
{expense.title}
</div>
<div className="text-xs text-muted-foreground">
Expand Down
2 changes: 1 addition & 1 deletion src/app/groups/[groupId]/expenses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default async function GroupExpensesPage({
async function Expenses({ groupId }: { groupId: string }) {
const group = await cached.getGroup(groupId)
if (!group) notFound()
const expenses = await getGroupExpenses(group.id)
const expenses = (await getGroupExpenses(group.id))// .filter(e => !e.isArchive)

return (
<ExpenseList
Expand Down
27 changes: 26 additions & 1 deletion src/components/expense-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { RuntimeFeatureFlags } from '@/lib/featureFlags'
import { ExpenseFormValues, expenseFormSchema } from '@/lib/schemas'
import { cn } from '@/lib/utils'
import { zodResolver } from '@hookform/resolvers/zod'
import { Save, Trash2 } from 'lucide-react'
import { Save, Trash2, Archive } from 'lucide-react'
import Link from 'next/link'
import { useSearchParams } from 'next/navigation'
import { useState } from 'react'
Expand All @@ -53,6 +53,7 @@ export type Props = {
categories: NonNullable<Awaited<ReturnType<typeof getCategories>>>
onSubmit: (values: ExpenseFormValues) => Promise<void>
onDelete?: () => Promise<void>
onArchive?: (state: boolean) => Promise<void>
runtimeFeatureFlags: RuntimeFeatureFlags
}

Expand All @@ -62,9 +63,11 @@ export function ExpenseForm({
categories,
onSubmit,
onDelete,
onArchive,
runtimeFeatureFlags,
}: Props) {
const isCreate = expense === undefined
const isArchived = expense?.isArchive
const searchParams = useSearchParams()
const getSelectedPayer = (field?: { value: string }) => {
if (isCreate && typeof window !== 'undefined') {
Expand Down Expand Up @@ -615,6 +618,28 @@ export function ExpenseForm({
<Save className="w-4 h-4 mr-2" />
{isCreate ? <>Create</> : <>Save</>}
</SubmitButton>
{!isCreate && !isArchived && onArchive && (
<AsyncButton
type="button"
variant="secondary"
loadingContent="Archiving…"
action={() => onArchive(true)}
>
<Archive className="w-4 h-4 mr-2" />
Archive
</AsyncButton>
)}
{!isCreate && isArchived && onArchive && (
<AsyncButton
type="button"
variant="secondary"
loadingContent="Unarchiving…"
action={() => onArchive(false)}
>
<Archive className="w-4 h-4 mr-2" />
Unarchive
</AsyncButton>
)}
{!isCreate && onDelete && (
<AsyncButton
type="button"
Expand Down
Loading

0 comments on commit 42797c2

Please sign in to comment.