Files
ausgaben-next/app/page.tsx

90 lines
2.7 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import AusgabenForm from '@/components/AusgabenForm';
import AusgabenList from '@/components/AusgabenList';
import { AusgabenEntry } from '@/types/ausgaben';
import packageJson from '@/package.json';
export default function Home() {
const [entries, setEntries] = useState<AusgabenEntry[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [selectedEntry, setSelectedEntry] = useState<AusgabenEntry | null>(null);
const version = packageJson.version;
useEffect(() => {
fetchRecentEntries();
}, []);
const fetchRecentEntries = async () => {
setIsLoading(true);
try {
const response = await fetch('/api/ausgaben?limit=10', {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
},
});
const data = await response.json();
if (data.success) {
setEntries(data.data);
}
} catch (error) {
console.error('Error fetching entries:', error);
} finally {
setIsLoading(false);
}
};
const handleSuccess = () => {
setSelectedEntry(null);
setTimeout(() => {
fetchRecentEntries();
}, 100);
};
const handleDelete = (id: number) => {
setEntries(entries.filter(entry => entry.ID !== id));
};
const handleEdit = (entry: AusgabenEntry) => {
setSelectedEntry(entry);
window.scrollTo({ top: 0, behavior: 'smooth' });
};
return (
<div className="min-h-screen bg-white py-4 px-4">
<main className="max-w-7xl mx-auto border-2 border-black rounded-lg p-6 bg-[#FFFFDD]">
<h1 className="text-3xl font-bold text-center mb-6">Ausgaben - Log</h1>
<div>
<h2 className="text-xl font-semibold mb-4">Eingabe</h2>
<AusgabenForm onSuccess={handleSuccess} selectedEntry={selectedEntry} />
<div className="mt-6 bg-white border border-black rounded-lg shadow-md p-6">
<h3 className="text-xl font-semibold mb-4">Letzte 10 Einträge</h3>
{isLoading ? (
<div className="text-center py-4">Lade Daten...</div>
) : (
<AusgabenList entries={entries} onDelete={handleDelete} onEdit={handleEdit} />
)}
</div>
</div>
{/* Footer */}
<footer className="mt-8 flex justify-between items-center text-sm text-gray-600 px-4 border-t-2 border-black pt-4">
<div>
<a href="mailto:rxf@gmx.de" className="text-blue-600 hover:underline">
mailto:rxf@gmx.de
</a>
</div>
<div className="text-right">
Version {version}
</div>
</footer>
</main>
</div>
);
}