-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
12 changed files
with
237 additions
and
89 deletions.
There are no files selected for viewing
48 changes: 7 additions & 41 deletions
48
src/app/groups/[groupId]/expenses/[expenseId]/edit/page.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 |
---|---|---|
@@ -1,55 +1,21 @@ | ||
import { cached } from '@/app/cached-functions' | ||
import { ExpenseForm } from '@/components/expense-form' | ||
import { | ||
deleteExpense, | ||
getCategories, | ||
getExpense, | ||
updateExpense, | ||
} from '@/lib/api' | ||
import { EditExpenseForm } from '@/app/groups/[groupId]/expenses/edit-expense-form' | ||
import { getRuntimeFeatureFlags } from '@/lib/featureFlags' | ||
import { expenseFormSchema } from '@/lib/schemas' | ||
import { Metadata } from 'next' | ||
import { notFound, redirect } from 'next/navigation' | ||
import { Suspense } from 'react' | ||
|
||
export const metadata: Metadata = { | ||
title: 'Edit expense', | ||
title: 'Edit Expense', | ||
} | ||
|
||
export default async function EditExpensePage({ | ||
params: { groupId, expenseId }, | ||
}: { | ||
params: { groupId: string; expenseId: string } | ||
}) { | ||
const categories = await getCategories() | ||
const group = await cached.getGroup(groupId) | ||
if (!group) notFound() | ||
const expense = await getExpense(groupId, expenseId) | ||
if (!expense) notFound() | ||
|
||
async function updateExpenseAction(values: unknown, participantId?: string) { | ||
'use server' | ||
const expenseFormValues = expenseFormSchema.parse(values) | ||
await updateExpense(groupId, expenseId, expenseFormValues, participantId) | ||
redirect(`/groups/${groupId}`) | ||
} | ||
|
||
async function deleteExpenseAction(participantId?: string) { | ||
'use server' | ||
await deleteExpense(groupId, expenseId, participantId) | ||
redirect(`/groups/${groupId}`) | ||
} | ||
|
||
return ( | ||
<Suspense> | ||
<ExpenseForm | ||
group={group} | ||
expense={expense} | ||
categories={categories} | ||
onSubmit={updateExpenseAction} | ||
onDelete={deleteExpenseAction} | ||
runtimeFeatureFlags={await getRuntimeFeatureFlags()} | ||
/> | ||
</Suspense> | ||
<EditExpenseForm | ||
groupId={groupId} | ||
expenseId={expenseId} | ||
runtimeFeatureFlags={await getRuntimeFeatureFlags()} | ||
/> | ||
) | ||
} |
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,45 @@ | ||
'use client' | ||
import { RuntimeFeatureFlags } from '@/lib/featureFlags' | ||
import { trpc } from '@/trpc/client' | ||
import { useRouter } from 'next/navigation' | ||
import { ExpenseForm } from './expense-form' | ||
|
||
export function CreateExpenseForm({ | ||
groupId, | ||
runtimeFeatureFlags, | ||
}: { | ||
groupId: string | ||
expenseId?: string | ||
runtimeFeatureFlags: RuntimeFeatureFlags | ||
}) { | ||
const { data: groupData } = trpc.groups.get.useQuery({ groupId }) | ||
const group = groupData?.group | ||
|
||
const { data: categoriesData } = trpc.categories.list.useQuery() | ||
const categories = categoriesData?.categories | ||
|
||
const { mutateAsync: createExpenseMutateAsync } = | ||
trpc.groups.expenses.create.useMutation() | ||
|
||
const utils = trpc.useUtils() | ||
const router = useRouter() | ||
|
||
if (!group || !categories) return null | ||
|
||
return ( | ||
<ExpenseForm | ||
group={group} | ||
categories={categories} | ||
onSubmit={async (expenseFormValues, participantId) => { | ||
await createExpenseMutateAsync({ | ||
groupId, | ||
expenseFormValues, | ||
participantId, | ||
}) | ||
utils.groups.expenses.invalidate() | ||
router.push(`/groups/${group.id}`) | ||
}} | ||
runtimeFeatureFlags={runtimeFeatureFlags} | ||
/> | ||
) | ||
} |
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,40 +1,20 @@ | ||
import { cached } from '@/app/cached-functions' | ||
import { ExpenseForm } from '@/components/expense-form' | ||
import { createExpense, getCategories } from '@/lib/api' | ||
import { CreateExpenseForm } from '@/app/groups/[groupId]/expenses/create-expense-form' | ||
import { getRuntimeFeatureFlags } from '@/lib/featureFlags' | ||
import { expenseFormSchema } from '@/lib/schemas' | ||
import { Metadata } from 'next' | ||
import { notFound, redirect } from 'next/navigation' | ||
import { Suspense } from 'react' | ||
|
||
export const metadata: Metadata = { | ||
title: 'Create expense', | ||
title: 'Create Expense', | ||
} | ||
|
||
export default async function ExpensePage({ | ||
params: { groupId }, | ||
}: { | ||
params: { groupId: string } | ||
}) { | ||
const categories = await getCategories() | ||
const group = await cached.getGroup(groupId) | ||
if (!group) notFound() | ||
|
||
async function createExpenseAction(values: unknown, participantId?: string) { | ||
'use server' | ||
const expenseFormValues = expenseFormSchema.parse(values) | ||
await createExpense(expenseFormValues, groupId, participantId) | ||
redirect(`/groups/${groupId}`) | ||
} | ||
|
||
return ( | ||
<Suspense> | ||
<ExpenseForm | ||
group={group} | ||
categories={categories} | ||
onSubmit={createExpenseAction} | ||
runtimeFeatureFlags={await getRuntimeFeatureFlags()} | ||
/> | ||
</Suspense> | ||
<CreateExpenseForm | ||
groupId={groupId} | ||
runtimeFeatureFlags={await getRuntimeFeatureFlags()} | ||
/> | ||
) | ||
} |
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,65 @@ | ||
'use client' | ||
import { RuntimeFeatureFlags } from '@/lib/featureFlags' | ||
import { trpc } from '@/trpc/client' | ||
import { useRouter } from 'next/navigation' | ||
import { ExpenseForm } from './expense-form' | ||
|
||
export function EditExpenseForm({ | ||
groupId, | ||
expenseId, | ||
runtimeFeatureFlags, | ||
}: { | ||
groupId: string | ||
expenseId: string | ||
runtimeFeatureFlags: RuntimeFeatureFlags | ||
}) { | ||
const { data: groupData } = trpc.groups.get.useQuery({ groupId }) | ||
const group = groupData?.group | ||
|
||
const { data: categoriesData } = trpc.categories.list.useQuery() | ||
const categories = categoriesData?.categories | ||
|
||
const { data: expenseData } = trpc.groups.expenses.get.useQuery({ | ||
groupId, | ||
expenseId, | ||
}) | ||
const expense = expenseData?.expense | ||
|
||
const { mutateAsync: updateExpenseMutateAsync } = | ||
trpc.groups.expenses.update.useMutation() | ||
const { mutateAsync: deleteExpenseMutateAsync } = | ||
trpc.groups.expenses.delete.useMutation() | ||
|
||
const utils = trpc.useUtils() | ||
const router = useRouter() | ||
|
||
if (!group || !categories || !expense) return null | ||
|
||
return ( | ||
<ExpenseForm | ||
group={group} | ||
expense={expense} | ||
categories={categories} | ||
onSubmit={async (expenseFormValues, participantId) => { | ||
await updateExpenseMutateAsync({ | ||
expenseId, | ||
groupId, | ||
expenseFormValues, | ||
participantId, | ||
}) | ||
utils.groups.expenses.invalidate() | ||
router.push(`/groups/${group.id}`) | ||
}} | ||
onDelete={async (participantId) => { | ||
await deleteExpenseMutateAsync({ | ||
expenseId, | ||
groupId, | ||
participantId, | ||
}) | ||
utils.groups.expenses.invalidate() | ||
router.push(`/groups/${group.id}`) | ||
}} | ||
runtimeFeatureFlags={runtimeFeatureFlags} | ||
/> | ||
) | ||
} |
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
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,23 @@ | ||
import { createExpense } from '@/lib/api' | ||
import { expenseFormSchema } from '@/lib/schemas' | ||
import { baseProcedure } from '@/trpc/init' | ||
import { z } from 'zod' | ||
|
||
export const createGroupExpenseProcedure = baseProcedure | ||
.input( | ||
z.object({ | ||
groupId: z.string().min(1), | ||
expenseFormValues: expenseFormSchema, | ||
participantId: z.string().optional(), | ||
}), | ||
) | ||
.mutation( | ||
async ({ input: { groupId, expenseFormValues, participantId } }) => { | ||
const expense = await createExpense( | ||
expenseFormValues, | ||
groupId, | ||
participantId, | ||
) | ||
return { expenseId: expense.id } | ||
}, | ||
) |
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,16 @@ | ||
import { deleteExpense } from '@/lib/api' | ||
import { baseProcedure } from '@/trpc/init' | ||
import { z } from 'zod' | ||
|
||
export const deleteGroupExpenseProcedure = baseProcedure | ||
.input( | ||
z.object({ | ||
expenseId: z.string().min(1), | ||
groupId: z.string().min(1), | ||
participantId: z.string().optional(), | ||
}), | ||
) | ||
.mutation(async ({ input: { expenseId, groupId, participantId } }) => { | ||
await deleteExpense(groupId, expenseId, participantId) | ||
return {} | ||
}) |
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,17 @@ | ||
import { getExpense } from '@/lib/api' | ||
import { baseProcedure } from '@/trpc/init' | ||
import { TRPCError } from '@trpc/server' | ||
import { z } from 'zod' | ||
|
||
export const getGroupExpenseProcedure = baseProcedure | ||
.input(z.object({ groupId: z.string().min(1), expenseId: z.string().min(1) })) | ||
.query(async ({ input: { groupId, expenseId } }) => { | ||
const expense = await getExpense(groupId, expenseId) | ||
if (!expense) { | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'Expense not found', | ||
}) | ||
} | ||
return { expense } | ||
}) |
Oops, something went wrong.