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