Open History to see the last commit
1import * as React from 'react'
2import { Form, useLoaderData, redirect, useNavigate } from 'react-router-dom'
3β
4import { updateContact } from '../contacts'
5β
6export const action =
7 (queryClient) =>
8 async ({ request, params }) => {
9 const formData = await request.formData()
10 const updates = Object.fromEntries(formData)
11 await updateContact(params.contactId, updates)
12 queryClient.invalidateQueries({ queryKey: ['contacts'] })
13 return redirect(`/contacts/${params.contactId}`)
14 }
15β
16export default function Edit() {
17 const contact = useLoaderData()
18 const navigate = useNavigate()
19β
20 return (
21 <Form method="post" id="contact-form">
22 <p>
23 <span>Name</span>
24 <input
25 placeholder="First"
26 aria-label="First name"
27 type="text"
28 name="first"
29 defaultValue={contact.first}
30 />
31 <input
32 placeholder="Last"
33 aria-label="Last name"
34 type="text"
35 name="last"
36 defaultValue={contact.last}
37 />
38 </p>
39 <label>
40 <span>Twitter</span>
41 <input
42 type="text"
43 name="twitter"
44 placeholder="@jack"
45 defaultValue={contact.twitter}
46 />
47 </label>
48 <label>
49 <span>Avatar URL</span>
50 <input
51 placeholder="https://example.com/avatar.jpg"
52 aria-label="Avatar URL"
53 type="text"
54 name="avatar"
55 defaultValue={contact.avatar}
56 />
57 </label>
58 <label>
59 <span>Notes</span>
60 <textarea name="notes" defaultValue={contact.notes} rows={6} />
61 </label>
62 <p>
63 <button type="submit">Save</button>
64 <button
65 type="button"
66 onClick={() => {
67 navigate(-1)
68 }}
69 >
70 Cancel
71 </button>
72 </p>
73 </Form>
74 )
75}