Complete "Updating Contacts with FormData"
This commit is contained in:
@@ -1,8 +1,16 @@
|
|||||||
import Form from "next/form";
|
import Form from "next/form";
|
||||||
import { fetchContact } from "@/app/(sidebar)/loaders";
|
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({
|
export default async function EditContactPage({
|
||||||
params
|
params
|
||||||
}: {
|
}: {
|
||||||
@@ -13,9 +21,10 @@ export default async function EditContactPage({
|
|||||||
if (c === null) notFound();
|
if (c === null) notFound();
|
||||||
return c;
|
return c;
|
||||||
});
|
});
|
||||||
|
const editThisContact = editContact.bind(null, contactId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form key={contact.id} id="contact-form" formMethod="post" action=".">
|
<Form key={contact.id} id="contact-form" action={editThisContact}>
|
||||||
<p>
|
<p>
|
||||||
<span>Name</span>
|
<span>Name</span>
|
||||||
<input
|
<input
|
||||||
|
|||||||
Reference in New Issue
Block a user