Skip to content

Commit e5b216e

Browse files
committed
Add contact route
1 parent bc80dc3 commit e5b216e

File tree

3 files changed

+104
-15
lines changed

3 files changed

+104
-15
lines changed
File renamed without changes.

src/main.jsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom/client'
3-
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
4-
import Root from './routes/root'
5-
import ErrorPage from './error-page'
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
4+
import Root from './routes/root';
5+
import ErrorPage from './error-page';
6+
import Contact from './routes/contact';
67
// import App from './App.jsx'
7-
import './index.css'
8+
import './index.css';
89

910
const router = createBrowserRouter([
10-
{ path: '/',
11-
element: <Root/>,
12-
errorElement: <ErrorPage />,
13-
}
14-
])
11+
{ path: '/', element: <Root />, errorElement: <ErrorPage /> },
12+
{ path: 'contacts/:contactId', element: <Contact /> },
13+
]);
1514

1615
ReactDOM.createRoot(document.getElementById('root')).render(
17-
<React.StrictMode>
18-
<RouterProvider router={router} />
19-
</React.StrictMode>,
20-
)
16+
<React.StrictMode>
17+
<RouterProvider router={router} />
18+
</React.StrictMode>
19+
);

src/routes/contact.jsx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { Form } from "react-router-dom";
2+
3+
export default function Contact() {
4+
const contact = {
5+
first: "Your",
6+
last: "Name",
7+
avatar: "https://placekitten.com/g/200/200",
8+
twitter: "your_handle",
9+
notes: "Some notes",
10+
favorite: true,
11+
};
12+
13+
return (
14+
<div id="contact">
15+
<div>
16+
<img
17+
key={contact.avatar}
18+
src={contact.avatar || null}
19+
/>
20+
</div>
21+
22+
<div>
23+
<h1>
24+
{contact.first || contact.last ? (
25+
<>
26+
{contact.first} {contact.last}
27+
</>
28+
) : (
29+
<i>No Name</i>
30+
)}{" "}
31+
<Favorite contact={contact} />
32+
</h1>
33+
34+
{contact.twitter && (
35+
<p>
36+
<a
37+
target="_blank"
38+
href={`https://twitter.com/${contact.twitter}`}
39+
>
40+
{contact.twitter}
41+
</a>
42+
</p>
43+
)}
44+
45+
{contact.notes && <p>{contact.notes}</p>}
46+
47+
<div>
48+
<Form action="edit">
49+
<button type="submit">Edit</button>
50+
</Form>
51+
<Form
52+
method="post"
53+
action="destroy"
54+
onSubmit={(event) => {
55+
if (
56+
!confirm(
57+
"Please confirm you want to delete this record."
58+
)
59+
) {
60+
event.preventDefault();
61+
}
62+
}}
63+
>
64+
<button type="submit">Delete</button>
65+
</Form>
66+
</div>
67+
</div>
68+
</div>
69+
);
70+
}
71+
72+
function Favorite({ contact }) {
73+
// yes, this is a `let` for later
74+
let favorite = contact.favorite;
75+
return (
76+
<Form method="post">
77+
<button
78+
name="favorite"
79+
value={favorite ? "false" : "true"}
80+
aria-label={
81+
favorite
82+
? "Remove from favorites"
83+
: "Add to favorites"
84+
}
85+
>
86+
{favorite ? "★" : "☆"}
87+
</button>
88+
</Form>
89+
);
90+
}

0 commit comments

Comments
 (0)