From f881aff5f9993c3a6b4b0d8ed7e67d2f812f39d6 Mon Sep 17 00:00:00 2001 From: Sebastien Castiel Date: Tue, 19 Dec 2023 09:44:09 -0500 Subject: Revert "Use modal dialogs for expense creation & edition (#10)" This reverts commit 1e66efe5169dfad9a0c62ba42fbf31ce977bf49e. --- src/components/expense-form.tsx | 388 ++++++++++++++++++++-------------------- 1 file changed, 193 insertions(+), 195 deletions(-) (limited to 'src/components/expense-form.tsx') diff --git a/src/components/expense-form.tsx b/src/components/expense-form.tsx index 0f677e6..bdee9d0 100644 --- a/src/components/expense-form.tsx +++ b/src/components/expense-form.tsx @@ -1,12 +1,14 @@ 'use client' -import { - createExpenseAction, - deleteExpenseAction, - updateExpenseAction, -} from '@/app/groups/[groupId]/expenses/actions' import { AsyncButton } from '@/components/async-button' import { SubmitButton } from '@/components/submit-button' import { Button } from '@/components/ui/button' +import { + Card, + CardContent, + CardFooter, + CardHeader, + CardTitle, +} from '@/components/ui/card' import { Checkbox } from '@/components/ui/checkbox' import { Form, @@ -28,15 +30,17 @@ import { import { getExpense, getGroup } from '@/lib/api' import { ExpenseFormValues, expenseFormSchema } from '@/lib/schemas' import { zodResolver } from '@hookform/resolvers/zod' -import { useRouter, useSearchParams } from 'next/navigation' +import { useSearchParams } from 'next/navigation' import { useForm } from 'react-hook-form' export type Props = { group: NonNullable>> expense?: NonNullable>> + onSubmit: (values: ExpenseFormValues) => Promise + onDelete?: () => Promise } -export function ExpenseForm({ group, expense }: Props) { +export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) { const isCreate = expense === undefined const searchParams = useSearchParams() const form = useForm({ @@ -61,210 +65,204 @@ export function ExpenseForm({ group, expense }: Props) { } : { title: '', amount: 0, paidFor: [], isReimbursement: false }, }) - const router = useRouter() return (
- { - if (expense) { - await updateExpenseAction(group.id, expense.id, values) - } else { - await createExpenseAction(group.id, values) - } - router.back() - })} - > -
- ( - - Expense title - - - - - Enter a description for the expense. - - - - )} - /> - - ( - - Paid by - - - Select the participant who paid the expense. - - - - )} - /> - - ( - - Amount -
- {group.currency} + onSubmit(values))}> + + + + {isCreate ? <>Create expense : <>Edit expense} + + + + ( + + Expense title -
- - - ( - - - - -
- This is a reimbursement -
-
- )} - /> -
- )} - /> + + Enter a description for the expense. + + + + )} + /> - ( - -
- - Paid for - - + ( + + Paid by + - Select who the expense was paid for. + Select the participant who paid the expense. -
- {group.participants.map(({ id, name }) => ( + +
+ )} + /> + + ( + + Amount +
+ {group.currency} + + + +
+ + { - return ( - - - { - return checked - ? field.onChange([...field.value, id]) - : field.onChange( - field.value?.filter( - (value) => value !== id, - ), - ) - }} - /> - - - {name} - - - ) - }} + name="isReimbursement" + render={({ field }) => ( + + + + +
+ This is a reimbursement +
+
+ )} /> - ))} - -
- )} - /> -
+ + )} + /> -
- Creating… : <>Saving…} - > - {isCreate ? <>Create : <>Save} - - {!isCreate && ( - { - await deleteExpenseAction(group.id, expense.id) - router.back() - }} + ( + +
+ + Paid for + + + + Select who the expense was paid for. + +
+ {group.participants.map(({ id, name }) => ( + { + return ( + + + { + return checked + ? field.onChange([...field.value, id]) + : field.onChange( + field.value?.filter( + (value) => value !== id, + ), + ) + }} + /> + + + {name} + + + ) + }} + /> + ))} + +
+ )} + /> + + + + Creating… : <>Saving…} > - Delete -
- )} -
+ {isCreate ? <>Create : <>Save} + + {!isCreate && onDelete && ( + + Delete + + )} + +
) -- cgit v1.3