File size: 6,149 Bytes
24d40b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import { useState, useEffect } from "react";
import { useParams, useNavigate } from "react-router-dom";
import { Trash, Edit, ArrowDownRight, ArrowUpRight } from "lucide-react";
import AppLayout from "@/components/layout/AppLayout";
import Header from "@/components/shared/Header";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { Transaction } from "@/components/dashboard/RecentTransactions";
import { toast } from "sonner";
import database from "@/services/database";
const TransactionDetail = () => {
const { id } = useParams<{ id: string }>();
const navigate = useNavigate();
const [transaction, setTransaction] = useState<Transaction | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const loadTransaction = async () => {
if (!id) return;
try {
await database.initialize();
// Fetch the transaction from SQLite
const results = database.exec('SELECT * FROM transactions WHERE id = ?', [id]);
if (results.length === 0) {
setTransaction(null);
} else {
const row = results[0];
// Convert to our Transaction type
setTransaction({
id: row.id,
title: row.title,
amount: parseFloat(row.amount),
type: row.type as 'income' | 'expense',
category: row.category,
date: new Date(row.date),
note: row.note || undefined
});
}
} catch (error) {
console.error('Error loading transaction:', error);
toast.error('Failed to load transaction');
} finally {
setIsLoading(false);
}
};
loadTransaction();
}, [id]);
if (isLoading) {
return (
<AppLayout>
<div className="max-w-md mx-auto p-4 flex justify-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
</div>
</AppLayout>
);
}
if (!transaction) {
return (
<AppLayout>
<div className="max-w-md mx-auto p-4 text-center">
<h2 className="text-xl font-semibold">Transaction not found</h2>
<Button
onClick={() => navigate('/transactions')}
className="mt-4"
>
Go Back
</Button>
</div>
</AppLayout>
);
}
const formatDate = (date: Date) => {
return new Intl.DateTimeFormat('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
}).format(date);
};
const formatCurrency = (amount: number) => {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol',
}).format(Math.abs(amount));
};
const handleDelete = async () => {
if (!id) return;
try {
await database.initialize();
// Delete the transaction from SQLite
database.exec('DELETE FROM transactions WHERE id = ?', [id]);
toast.success('Transaction deleted successfully');
navigate('/transactions');
} catch (error) {
console.error('Error deleting transaction:', error);
toast.error('Failed to delete transaction');
}
};
return (
<AppLayout>
<div className="max-w-md mx-auto">
<Header
title="Transaction Details"
showBackButton
rightElement={
<Button
variant="ghost"
size="icon"
onClick={handleDelete}
className="text-red-500 hover:text-red-600 hover:bg-red-50"
>
<Trash size={18} />
</Button>
}
/>
<div className="p-4 space-y-6 animate-fade-in">
<div className="flex justify-center py-6">
<div
className={cn(
"w-16 h-16 rounded-full flex items-center justify-center",
transaction.type === "expense"
? "bg-red-100 text-red-600"
: "bg-green-100 text-green-600"
)}
>
{transaction.type === "expense"
? <ArrowUpRight size={28} />
: <ArrowDownRight size={28} />
}
</div>
</div>
<div className="text-center">
<h2 className="text-2xl font-semibold">{transaction.title}</h2>
<p className={cn(
"text-3xl font-bold mt-2",
transaction.type === "expense" ? "text-red-600" : "text-green-600"
)}>
{transaction.type === "expense" ? "- " : "+ "}
{formatCurrency(transaction.amount)}
</p>
<p className="text-muted-foreground mt-1">{formatDate(transaction.date)}</p>
</div>
<div className="bg-card border border-border rounded-xl p-4 space-y-4">
<div>
<p className="text-sm text-muted-foreground">Type</p>
<p className="font-medium capitalize">{transaction.type}</p>
</div>
<div>
<p className="text-sm text-muted-foreground">Category</p>
<p className="font-medium capitalize">{transaction.category}</p>
</div>
<div>
<p className="text-sm text-muted-foreground">Note</p>
<p className="font-medium">{transaction.note || "No notes"}</p>
</div>
</div>
<Button
onClick={() => navigate(`/edit-transaction/${transaction.id}`)}
className="w-full flex items-center justify-center py-6"
>
<Edit size={18} className="mr-2" />
Edit Transaction
</Button>
</div>
</div>
</AppLayout>
);
};
export default TransactionDetail;
|