From 75337be90c997f0d93c901f652eeadf641647a4c Mon Sep 17 00:00:00 2001 From: "Federico Pasqua (eisterman)" Date: Tue, 1 Apr 2025 14:10:20 +0200 Subject: [PATCH] Complete "URL Params in Loaders" and "Throwing Responses". --- .../contacts/[contactId]/Contact.tsx | 96 ++++++++++++++++ .../contacts/[contactId]/loading.tsx | 5 + .../contacts/[contactId]/not-found.tsx | 8 ++ .../(sidebar)/contacts/[contactId]/page.tsx | 107 +++--------------- 4 files changed, 123 insertions(+), 93 deletions(-) create mode 100644 src/app/(sidebar)/contacts/[contactId]/Contact.tsx create mode 100644 src/app/(sidebar)/contacts/[contactId]/loading.tsx create mode 100644 src/app/(sidebar)/contacts/[contactId]/not-found.tsx diff --git a/src/app/(sidebar)/contacts/[contactId]/Contact.tsx b/src/app/(sidebar)/contacts/[contactId]/Contact.tsx new file mode 100644 index 0000000..e54c418 --- /dev/null +++ b/src/app/(sidebar)/contacts/[contactId]/Contact.tsx @@ -0,0 +1,96 @@ +"use client"; + +import { ContactRecord } from "@/app/data"; +import Form from "next/form"; +import { use } from "react"; + +export default function Contact({ + contact +}: { + contact: Promise +}) { + const c = use(contact); + + return ( +
+
+ {/* eslint-disable-next-line @next/next/no-img-element */} + {`${c.first} +
+ +
+

+ {c.first || c.last ? ( + <> + {c.first} {c.last} + + ) : ( + No Name + )} + +

+ + {c.twitter ? ( +

+ + {c.twitter} + +

+ ) : null} + + {c.notes ?

{c.notes}

: null} + +
+
+ +
+ +
{ + const response = confirm( + "Please confirm you want to delete this record." + ); + if (!response) { + event.preventDefault(); + } + }} + > + +
+
+
+
+ ); +} + +function Favorite({ + contact, +}: { + contact: Pick; +}) { + const favorite = contact.favorite; + + return ( +
+ +
+ ); +} \ No newline at end of file diff --git a/src/app/(sidebar)/contacts/[contactId]/loading.tsx b/src/app/(sidebar)/contacts/[contactId]/loading.tsx new file mode 100644 index 0000000..0822a97 --- /dev/null +++ b/src/app/(sidebar)/contacts/[contactId]/loading.tsx @@ -0,0 +1,5 @@ +export default function ContactLoading() { + return ( +
Loading Contact...
+ ); +} \ No newline at end of file diff --git a/src/app/(sidebar)/contacts/[contactId]/not-found.tsx b/src/app/(sidebar)/contacts/[contactId]/not-found.tsx new file mode 100644 index 0000000..d648d02 --- /dev/null +++ b/src/app/(sidebar)/contacts/[contactId]/not-found.tsx @@ -0,0 +1,8 @@ +export default function NotFound() { + return ( +
+

404

+

The requested contact could not be found.

+
+ ); +} \ No newline at end of file diff --git a/src/app/(sidebar)/contacts/[contactId]/page.tsx b/src/app/(sidebar)/contacts/[contactId]/page.tsx index 274e324..fbf41fe 100644 --- a/src/app/(sidebar)/contacts/[contactId]/page.tsx +++ b/src/app/(sidebar)/contacts/[contactId]/page.tsx @@ -1,98 +1,19 @@ -"use client"; +import { getContact } from "@/app/data"; +import { notFound } from "next/navigation"; +import Contact from "@/app/(sidebar)/contacts/[contactId]/Contact"; -import Form from "next/form"; -import { ContactRecord } from "@/app/data"; - -export default function Contact() { - const contact = { - first: "Your", - last: "Name", - avatar: "https://placecats.com/200/200", - twitter: "your_handle", - notes: "Some notes", - favorite: true, - }; +export default async function ContactPage({ + params +}: { + params: Promise<{ contactId: string }> +}) { + const { contactId } = await params; + const contact = getContact(contactId).then((c) => { + if (c === null) notFound(); + return c; + }); return ( -
-
- {/* eslint-disable-next-line @next/next/no-img-element */} - {`${contact.first} -
- -
-

- {contact.first || contact.last ? ( - <> - {contact.first} {contact.last} - - ) : ( - No Name - )} - -

- - {contact.twitter ? ( -

- - {contact.twitter} - -

- ) : null} - - {contact.notes ?

{contact.notes}

: null} - -
-
- -
- -
{ - const response = confirm( - "Please confirm you want to delete this record." - ); - if (!response) { - event.preventDefault(); - } - }} - > - -
-
-
-
+ ); } - -function Favorite({ - contact, -}: { - contact: Pick; -}) { - const favorite = contact.favorite; - - return ( -
- -
- ); -} \ No newline at end of file