mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-21 23:16:13 +01:00
Use modal dialogs for expense creation & edition (#10)
* First attemps at using route interception and modals * Remove route interception * Make it work * Use Vaul on small screens * Improve vaul
This commit is contained in:
committed by
GitHub
parent
66ab0ff82b
commit
1e66efe516
3
src/app/groups/[groupId]/@modal/default.tsx
Normal file
3
src/app/groups/[groupId]/@modal/default.tsx
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function Default() {
|
||||
return null
|
||||
}
|
||||
83
src/app/groups/[groupId]/@modal/expense-modal.tsx
Normal file
83
src/app/groups/[groupId]/@modal/expense-modal.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
'use client'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { ReactNode, useEffect, useState } from 'react'
|
||||
import { Drawer } from 'vaul'
|
||||
|
||||
type Props = {
|
||||
children: ReactNode
|
||||
title: ReactNode
|
||||
}
|
||||
|
||||
export function ExpenseModal(props: Props) {
|
||||
const size = useTailwindBreakpoint()
|
||||
if (size === 'xs') {
|
||||
return <ExpenseVaul {...props} />
|
||||
} else {
|
||||
return <ExpenseDialog {...props} />
|
||||
}
|
||||
}
|
||||
|
||||
export function ExpenseDialog({ children, title }: Props) {
|
||||
const router = useRouter()
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={() => router.back()}>
|
||||
<DialogContent className="w-full max-w-screen-sm">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{title}</DialogTitle>
|
||||
</DialogHeader>
|
||||
{children}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
export function ExpenseVaul({ children, title }: Props) {
|
||||
const router = useRouter()
|
||||
return (
|
||||
<Drawer.Root open onClose={() => router.back()}>
|
||||
<Drawer.Portal>
|
||||
<Drawer.Title>{title}</Drawer.Title>
|
||||
<Drawer.Overlay className="fixed inset-0 bg-background/80 backdrop-blur-sm" />
|
||||
<Drawer.Content className="bg-background border flex flex-col rounded-t-[10px] max-h-[90dvh] mt-24 fixed bottom-0 left-0 right-0 z-50">
|
||||
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 dark:bg-gray-700 mt-4"></div>
|
||||
<div className="text-xl font-bold p-4">{title}</div>
|
||||
<div className="flex-1 overflow-y-auto p-4 pt-0">{children}</div>
|
||||
</Drawer.Content>
|
||||
</Drawer.Portal>
|
||||
</Drawer.Root>
|
||||
)
|
||||
}
|
||||
|
||||
export function useTailwindBreakpoint() {
|
||||
const [size, setSize] = useState<'xs' | 'sm' | 'md' | 'lg'>('xs')
|
||||
|
||||
useEffect(() => {
|
||||
const handleBreakpointChange = () => {
|
||||
if (window.innerWidth >= 1200) {
|
||||
setSize('lg')
|
||||
} else if (window.innerWidth >= 768) {
|
||||
setSize('md')
|
||||
} else if (window.innerWidth >= 640) {
|
||||
setSize('sm')
|
||||
} else {
|
||||
setSize('xs')
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('resize', handleBreakpointChange)
|
||||
handleBreakpointChange()
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', handleBreakpointChange)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return size
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { ExpenseModal } from '@/app/groups/[groupId]/@modal/expense-modal'
|
||||
import { ExpenseForm } from '@/components/expense-form'
|
||||
import { getExpense, getGroup } from '@/lib/api'
|
||||
import { Metadata } from 'next'
|
||||
import { notFound } from 'next/navigation'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Edit expense',
|
||||
}
|
||||
|
||||
export default async function EditExpensePage({
|
||||
params: { groupId, expenseId },
|
||||
}: {
|
||||
params: { groupId: string; expenseId: string }
|
||||
}) {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) notFound()
|
||||
const expense = await getExpense(groupId, expenseId)
|
||||
if (!expense) notFound()
|
||||
|
||||
return (
|
||||
<ExpenseModal title="Edit expense">
|
||||
<ExpenseForm group={group} expense={expense} />
|
||||
</ExpenseModal>
|
||||
)
|
||||
}
|
||||
24
src/app/groups/[groupId]/@modal/expenses/create/page.tsx
Normal file
24
src/app/groups/[groupId]/@modal/expenses/create/page.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import { ExpenseModal } from '@/app/groups/[groupId]/@modal/expense-modal'
|
||||
import { ExpenseForm } from '@/components/expense-form'
|
||||
import { getGroup } from '@/lib/api'
|
||||
import { Metadata } from 'next'
|
||||
import { notFound } from 'next/navigation'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Create expense',
|
||||
}
|
||||
|
||||
export default async function ExpensePage({
|
||||
params: { groupId },
|
||||
}: {
|
||||
params: { groupId: string }
|
||||
}) {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) notFound()
|
||||
|
||||
return (
|
||||
<ExpenseModal title="Create expense">
|
||||
<ExpenseForm group={group} />
|
||||
</ExpenseModal>
|
||||
)
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { ExpenseForm } from '@/components/expense-form'
|
||||
import { deleteExpense, getExpense, getGroup, updateExpense } from '@/lib/api'
|
||||
import { expenseFormSchema } from '@/lib/schemas'
|
||||
import { Metadata } from 'next'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Edit expense',
|
||||
}
|
||||
|
||||
export default async function EditExpensePage({
|
||||
params: { groupId, expenseId },
|
||||
}: {
|
||||
params: { groupId: string; expenseId: string }
|
||||
}) {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) notFound()
|
||||
const expense = await getExpense(groupId, expenseId)
|
||||
if (!expense) notFound()
|
||||
|
||||
async function updateExpenseAction(values: unknown) {
|
||||
'use server'
|
||||
const expenseFormValues = expenseFormSchema.parse(values)
|
||||
await updateExpense(groupId, expenseId, expenseFormValues)
|
||||
redirect(`/groups/${groupId}`)
|
||||
}
|
||||
|
||||
async function deleteExpenseAction() {
|
||||
'use server'
|
||||
await deleteExpense(expenseId)
|
||||
redirect(`/groups/${groupId}`)
|
||||
}
|
||||
|
||||
return (
|
||||
<ExpenseForm
|
||||
group={group}
|
||||
expense={expense}
|
||||
onSubmit={updateExpenseAction}
|
||||
onDelete={deleteExpenseAction}
|
||||
/>
|
||||
)
|
||||
}
|
||||
28
src/app/groups/[groupId]/expenses/actions.ts
Normal file
28
src/app/groups/[groupId]/expenses/actions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
'use server'
|
||||
import { createExpense, deleteExpense, updateExpense } from '@/lib/api'
|
||||
import { expenseFormSchema } from '@/lib/schemas'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
|
||||
export async function createExpenseAction(groupId: string, values: unknown) {
|
||||
'use server'
|
||||
const expenseFormValues = expenseFormSchema.parse(values)
|
||||
await createExpense(expenseFormValues, groupId)
|
||||
revalidatePath(`/groups/${groupId}`, 'layout')
|
||||
}
|
||||
|
||||
export async function updateExpenseAction(
|
||||
groupId: string,
|
||||
expenseId: string,
|
||||
values: unknown,
|
||||
) {
|
||||
'use server'
|
||||
const expenseFormValues = expenseFormSchema.parse(values)
|
||||
await updateExpense(groupId, expenseId, expenseFormValues)
|
||||
revalidatePath(`/groups/${groupId}`, 'layout')
|
||||
}
|
||||
|
||||
export async function deleteExpenseAction(groupId: string, expenseId: string) {
|
||||
'use server'
|
||||
await deleteExpense(expenseId)
|
||||
revalidatePath(`/groups/${groupId}`, 'layout')
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import { ExpenseForm } from '@/components/expense-form'
|
||||
import { createExpense, getGroup } from '@/lib/api'
|
||||
import { expenseFormSchema } from '@/lib/schemas'
|
||||
import { Metadata } from 'next'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Create expense',
|
||||
}
|
||||
|
||||
export default async function ExpensePage({
|
||||
params: { groupId },
|
||||
}: {
|
||||
params: { groupId: string }
|
||||
}) {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) notFound()
|
||||
|
||||
async function createExpenseAction(values: unknown) {
|
||||
'use server'
|
||||
const expenseFormValues = expenseFormSchema.parse(values)
|
||||
await createExpense(expenseFormValues, groupId)
|
||||
redirect(`/groups/${groupId}`)
|
||||
}
|
||||
|
||||
return <ExpenseForm group={group} onSubmit={createExpenseAction} />
|
||||
}
|
||||
@@ -33,7 +33,9 @@ export function ExpenseList({
|
||||
expense.isReimbursement && 'italic',
|
||||
)}
|
||||
onClick={() => {
|
||||
router.push(`/groups/${groupId}/expenses/${expense.id}/edit`)
|
||||
router.push(`/groups/${groupId}/expenses/${expense.id}/edit`, {
|
||||
scroll: false,
|
||||
})
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
@@ -66,7 +68,10 @@ export function ExpenseList({
|
||||
{currency} {(expense.amount / 100).toFixed(2)}
|
||||
</div>
|
||||
<Button size="icon" variant="link" className="-my-2" asChild>
|
||||
<Link href={`/groups/${groupId}/expenses/${expense.id}/edit`}>
|
||||
<Link
|
||||
href={`/groups/${groupId}/expenses/${expense.id}/edit`}
|
||||
scroll={false}
|
||||
>
|
||||
<ChevronRight className="w-4 h-4" />
|
||||
</Link>
|
||||
</Button>
|
||||
|
||||
19
src/app/groups/[groupId]/expenses/expense-page.tsx
Normal file
19
src/app/groups/[groupId]/expenses/expense-page.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { ReactNode } from 'react'
|
||||
|
||||
export function ExpensePage({
|
||||
children,
|
||||
title,
|
||||
}: {
|
||||
children: ReactNode
|
||||
title: ReactNode
|
||||
}) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>{title}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>{children}</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
9
src/app/groups/[groupId]/expenses/layout.tsx
Normal file
9
src/app/groups/[groupId]/expenses/layout.tsx
Normal file
@@ -0,0 +1,9 @@
|
||||
import { ReactNode } from 'react'
|
||||
|
||||
export default function GroupExpensesLayout({
|
||||
children,
|
||||
}: {
|
||||
children: ReactNode
|
||||
}) {
|
||||
return <>{children}</>
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export default async function GroupExpensesPage({
|
||||
</CardHeader>
|
||||
<CardHeader>
|
||||
<Button asChild size="icon">
|
||||
<Link href={`/groups/${groupId}/expenses/create`}>
|
||||
<Link href={`/groups/${groupId}/expenses/create`} scroll={false}>
|
||||
<Plus />
|
||||
</Link>
|
||||
</Button>
|
||||
|
||||
@@ -5,12 +5,13 @@ import { getGroup } from '@/lib/api'
|
||||
import { Metadata } from 'next'
|
||||
import Link from 'next/link'
|
||||
import { notFound } from 'next/navigation'
|
||||
import { PropsWithChildren } from 'react'
|
||||
import { PropsWithChildren, ReactNode } from 'react'
|
||||
|
||||
type Props = {
|
||||
params: {
|
||||
groupId: string
|
||||
}
|
||||
modal: ReactNode
|
||||
}
|
||||
|
||||
export async function generateMetadata({
|
||||
@@ -28,6 +29,7 @@ export async function generateMetadata({
|
||||
|
||||
export default async function GroupLayout({
|
||||
children,
|
||||
modal,
|
||||
params: { groupId },
|
||||
}: PropsWithChildren<Props>) {
|
||||
const group = await getGroup(groupId)
|
||||
@@ -47,6 +49,7 @@ export default async function GroupLayout({
|
||||
</div>
|
||||
|
||||
{children}
|
||||
{modal}
|
||||
|
||||
<SaveGroupLocally group={{ id: group.id, name: group.name }} />
|
||||
</>
|
||||
|
||||
5
src/app/groups/[groupId]/not-found.tsx
Normal file
5
src/app/groups/[groupId]/not-found.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
'use client'
|
||||
|
||||
export default function NotFound() {
|
||||
return null
|
||||
}
|
||||
@@ -37,6 +37,7 @@ export function ReimbursementList({
|
||||
<Button variant="link" asChild className="-mx-4 -my-3">
|
||||
<Link
|
||||
href={`/groups/${groupId}/expenses/create?reimbursement=yes&from=${reimbursement.from}&to=${reimbursement.to}&amount=${reimbursement.amount}`}
|
||||
scroll={false}
|
||||
>
|
||||
Mark as paid
|
||||
</Link>
|
||||
|
||||
Reference in New Issue
Block a user