Complete "Updating Contacts with FormData"

This commit is contained in:
2025-04-01 17:58:39 +02:00
parent 434b7c8dc8
commit b73c9311f5

View File

@@ -1,8 +1,16 @@
import Form from "next/form";
import { fetchContact } from "@/app/(sidebar)/loaders";
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import { updateContact } from "@/app/data";
async function editContact(contactId: string, formData: FormData) {
"use server";
const updates = Object.fromEntries(formData);
await updateContact(contactId, updates);
redirect(`/contacts/${contactId}`);
}
export default async function EditContactPage({
params
}: {
@@ -13,9 +21,10 @@ export default async function EditContactPage({
if (c === null) notFound();
return c;
});
const editThisContact = editContact.bind(null, contactId);
return (
<Form key={contact.id} id="contact-form" formMethod="post" action=".">
<Form key={contact.id} id="contact-form" action={editThisContact}>
<p>
<span>Name</span>
<input