Bill Edit Form: dovršen fetch
This commit is contained in:
18
app/bills/[id]/edit/not-found.tsx
Normal file
18
app/bills/[id]/edit/not-found.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import Link from 'next/link';
|
||||
import { FaceFrownIcon } from '@heroicons/react/24/outline';
|
||||
|
||||
export default function NotFound() {
|
||||
return (
|
||||
<main className="flex h-full flex-col items-center justify-center gap-2">
|
||||
<FaceFrownIcon className="w-10 text-gray-400" />
|
||||
<h2 className="text-xl font-semibold">404 Bill Not Found</h2>
|
||||
<p>Could not find the requested Bill.</p>
|
||||
<Link
|
||||
href="/"
|
||||
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
|
||||
>
|
||||
Go Back
|
||||
</Link>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
32
app/bills/[id]/edit/page.tsx
Normal file
32
app/bills/[id]/edit/page.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Location } from '@/app/lib/db-types';
|
||||
import clientPromise from '@/app/lib/mongodb';
|
||||
import { BillEditForm } from '@/app/ui/BillEditForm';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
const fetchBillById = async (locationID:string, billID:string) => {
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
|
||||
// find a location with the given locationID
|
||||
const billLocation = await db.collection<Location>("lokacije").findOne({ _id: locationID })
|
||||
// find a bill with the given billID
|
||||
return(billLocation?.bills.find(({ _id }) => _id.toString() === billID))
|
||||
}
|
||||
|
||||
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||
|
||||
const [invoiceID, billID] = id.split('-');
|
||||
|
||||
const bill = await fetchBillById(invoiceID, billID);
|
||||
|
||||
if (!bill) {
|
||||
console.log('Bill not found');
|
||||
notFound();
|
||||
}
|
||||
return (
|
||||
<main>
|
||||
<BillEditForm invoiceID={invoiceID} bill={bill} />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user