Skip to content

Commit 3b9a594

Browse files
committed
Add destroy feature.
1 parent 7ff25fa commit 3b9a594

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

src/main.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { loader as contactLoader } from './routes/contact';
1212
import './index.css';
1313
import EditContact from './routes/edit';
1414
import {action as editAction} from './routes/edit'
15+
// import DeleteContact from './routes/destroy'
16+
import {action as deleteAction} from './routes/destroy'
1517

1618
const router = createBrowserRouter([
1719
{
@@ -32,6 +34,12 @@ const router = createBrowserRouter([
3234
loader: contactLoader,
3335
action: editAction
3436
},
37+
{
38+
path: 'contacts/:contactId/destroy',
39+
// element: <DeleteContact />,
40+
action: deleteAction,
41+
errorElement: <div>Oops! There was an error.</div>,
42+
}
3543
],
3644
},
3745
]);

src/routes/destroy.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { redirect } from 'react-router-dom'
2+
import { deleteContact } from '../contacts'
3+
4+
export async function action ({params}) {
5+
throw new Error('Ohh dang')
6+
await deleteContact(params.contactId)
7+
return redirect("/")
8+
}
9+

src/routes/root.jsx

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Outlet, NavLink, useLoaderData, Form } from 'react-router-dom';
1+
import {
2+
Outlet,
3+
NavLink,
4+
useLoaderData,
5+
Form,
6+
useNavigation,
7+
} from 'react-router-dom';
28
import { getContacts, createContact } from '../contacts';
39

410
export const loader = async () => {
@@ -13,6 +19,7 @@ export const action = async () => {
1319

1420
export default function Root() {
1521
const { contacts } = useLoaderData();
22+
const navigation = useNavigation();
1623
return (
1724
<>
1825
<div id='sidebar'>
@@ -33,35 +40,40 @@ export default function Root() {
3340
<button type='submit'>New</button>
3441
</Form>
3542
</div>
36-
{contacts.length ? (
37-
<ul>
38-
{contacts.map((contact) => (
39-
<li key={contact.id}>
40-
<NavLink
41-
to={`contacts/${contact.id}`}
42-
className={({ isActive, isPending }) =>
43-
isActive ? 'active' : isPending ? 'peding' : ''
44-
}
45-
>
46-
{contact.first || contact.last ? (
47-
<>
48-
{contact.first} {contact.last}
49-
</>
50-
) : (
51-
<i>No Name</i>
52-
)}{' '}
53-
{contact.favorite && <span></span>}
54-
</NavLink>
55-
</li>
56-
))}
57-
</ul>
58-
) : (
59-
<p>
60-
<i>No contacts</i>
61-
</p>
62-
)}
43+
<nav>
44+
{contacts.length ? (
45+
<ul>
46+
{contacts.map((contact) => (
47+
<li key={contact.id}>
48+
<NavLink
49+
to={`contacts/${contact.id}`}
50+
className={({ isActive, isPending }) =>
51+
isActive ? 'active' : isPending ? 'peding' : ''
52+
}
53+
>
54+
{contact.first || contact.last ? (
55+
<>
56+
{contact.first} {contact.last}
57+
</>
58+
) : (
59+
<i>No Name</i>
60+
)}{' '}
61+
{contact.favorite && <span></span>}
62+
</NavLink>
63+
</li>
64+
))}
65+
</ul>
66+
) : (
67+
<p>
68+
<i>No contacts</i>
69+
</p>
70+
)}
71+
</nav>
6372
</div>
64-
<div id='detail'>
73+
<div
74+
id='detail'
75+
className={navigation.state === 'loading' ? 'loading' : ''}
76+
>
6577
<Outlet />
6678
</div>
6779
</>

0 commit comments

Comments
 (0)