Complete "Updating Contacts with FormData"
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user