first build commit
This commit is contained in:
10
src/components/accounts/AccountBadge.tsx
Normal file
10
src/components/accounts/AccountBadge.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { AccountType } from '@/generated/prisma/client'
|
||||
|
||||
export function AccountBadge({ type }: { type: AccountType }) {
|
||||
return type === 'BANK' ? (
|
||||
<Badge variant="secondary">Bank</Badge>
|
||||
) : (
|
||||
<Badge variant="outline">Credit Card</Badge>
|
||||
)
|
||||
}
|
||||
97
src/components/accounts/AccountCard.tsx
Normal file
97
src/components/accounts/AccountCard.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { Account } from '@/generated/prisma/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import {
|
||||
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import { MoreHorizontal, Pencil, Trash2, EyeOff, Eye } from 'lucide-react'
|
||||
import { AccountBadge } from './AccountBadge'
|
||||
import { CreateAccountDialog } from './CreateAccountDialog'
|
||||
import { formatCents } from '@/lib/utils/currency'
|
||||
|
||||
export function AccountCard({ account }: { account: Account }) {
|
||||
const router = useRouter()
|
||||
const [editOpen, setEditOpen] = useState(false)
|
||||
|
||||
async function handleToggleActive() {
|
||||
await fetch(`/api/accounts/${account.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ isActive: !account.isActive }),
|
||||
})
|
||||
router.refresh()
|
||||
}
|
||||
|
||||
async function handleDelete() {
|
||||
if (!confirm(`Delete "${account.name}"? This cannot be undone.`)) return
|
||||
await fetch(`/api/accounts/${account.id}`, { method: 'DELETE' })
|
||||
router.refresh()
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card className={account.isActive ? '' : 'opacity-60'}>
|
||||
<CardHeader className="flex flex-row items-start justify-between space-y-0 pb-2">
|
||||
<div className="space-y-1 min-w-0">
|
||||
<CardTitle className="text-base truncate">
|
||||
<Link href={`/accounts/${account.id}`} className="hover:underline">
|
||||
{account.name}
|
||||
</Link>
|
||||
</CardTitle>
|
||||
{account.institution && (
|
||||
<p className="text-xs text-muted-foreground truncate">{account.institution}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1 shrink-0 ml-2">
|
||||
<AccountBadge type={account.type} />
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger className="inline-flex h-7 w-7 items-center justify-center rounded-md hover:bg-accent transition-colors">
|
||||
<MoreHorizontal className="h-4 w-4" />
|
||||
<span className="sr-only">Account actions</span>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setEditOpen(true)}>
|
||||
<Pencil className="h-4 w-4 mr-2" />
|
||||
Edit
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={handleToggleActive}>
|
||||
{account.isActive
|
||||
? <><EyeOff className="h-4 w-4 mr-2" />Deactivate</>
|
||||
: <><Eye className="h-4 w-4 mr-2" />Activate</>
|
||||
}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={handleDelete}
|
||||
className="text-destructive focus:text-destructive"
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
Delete
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-2xl font-bold tabular-nums">
|
||||
{formatCents(account.currentBalanceCents)}
|
||||
</p>
|
||||
{!account.isActive && (
|
||||
<p className="text-xs text-muted-foreground mt-1">Inactive</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<CreateAccountDialog
|
||||
open={editOpen}
|
||||
onOpenChange={setEditOpen}
|
||||
account={account}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
47
src/components/accounts/AccountList.tsx
Normal file
47
src/components/accounts/AccountList.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Account } from '@/generated/prisma/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Plus } from 'lucide-react'
|
||||
import { AccountCard } from './AccountCard'
|
||||
import { CreateAccountDialog } from './CreateAccountDialog'
|
||||
|
||||
export function AccountList({ accounts }: { accounts: Account[] }) {
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold">Accounts</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{accounts.length} account{accounts.length !== 1 ? 's' : ''}
|
||||
</p>
|
||||
</div>
|
||||
<Button onClick={() => setDialogOpen(true)}>
|
||||
<Plus className="h-4 w-4 mr-2" />
|
||||
Add account
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{accounts.length === 0 ? (
|
||||
<div className="rounded-lg border border-dashed p-12 text-center">
|
||||
<p className="text-muted-foreground">No accounts yet.</p>
|
||||
<Button variant="outline" className="mt-4" onClick={() => setDialogOpen(true)}>
|
||||
<Plus className="h-4 w-4 mr-2" />
|
||||
Add your first account
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
{accounts.map((account) => (
|
||||
<AccountCard key={account.id} account={account} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<CreateAccountDialog open={dialogOpen} onOpenChange={setDialogOpen} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
112
src/components/accounts/CreateAccountDialog.tsx
Normal file
112
src/components/accounts/CreateAccountDialog.tsx
Normal file
@@ -0,0 +1,112 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Account } from '@/generated/prisma/client'
|
||||
import {
|
||||
Dialog, DialogContent, DialogHeader, DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
account?: Account
|
||||
}
|
||||
|
||||
export function CreateAccountDialog({ open, onOpenChange, account }: Props) {
|
||||
const router = useRouter()
|
||||
const isEdit = !!account
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
setLoading(true)
|
||||
|
||||
const form = new FormData(e.currentTarget)
|
||||
const body = {
|
||||
name: form.get('name') as string,
|
||||
institution: (form.get('institution') as string) || undefined,
|
||||
type: form.get('type') as string,
|
||||
currency: 'USD',
|
||||
}
|
||||
|
||||
const res = await fetch(
|
||||
isEdit ? `/api/accounts/${account.id}` : '/api/accounts',
|
||||
{
|
||||
method: isEdit ? 'PATCH' : 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
)
|
||||
|
||||
if (!res.ok) {
|
||||
setError('Something went wrong. Please try again.')
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
router.refresh()
|
||||
onOpenChange(false)
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{isEdit ? 'Edit account' : 'Add account'}</DialogTitle>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit} className="space-y-4 pt-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="name">Name</Label>
|
||||
<Input
|
||||
id="name"
|
||||
name="name"
|
||||
defaultValue={account?.name}
|
||||
placeholder="e.g. Checking Account"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="institution">Institution <span className="text-muted-foreground">(optional)</span></Label>
|
||||
<Input
|
||||
id="institution"
|
||||
name="institution"
|
||||
defaultValue={account?.institution ?? ''}
|
||||
placeholder="e.g. Chase"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="type">Type</Label>
|
||||
<Select name="type" defaultValue={account?.type ?? 'BANK'} required>
|
||||
<SelectTrigger id="type">
|
||||
<SelectValue placeholder="Select type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="BANK">Bank</SelectItem>
|
||||
<SelectItem value="CREDIT_CARD">Credit Card</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{error && <p className="text-sm text-destructive">{error}</p>}
|
||||
<div className="flex justify-end gap-2 pt-2">
|
||||
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={loading}>
|
||||
{loading ? 'Saving…' : isEdit ? 'Save changes' : 'Add account'}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user