-
Notifications
You must be signed in to change notification settings - Fork 26k
/
index.tsx
46 lines (42 loc) · 1.2 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { useState } from 'react'
import Link from 'next/link'
import { mutate } from 'swr'
import ButtonLink from '@/components/button-link'
import Button from '@/components/button'
function Entry({ id, title, content }) {
const [deleting, setDeleting] = useState(false)
async function deleteEntry() {
setDeleting(true)
let res = await fetch(`/api/delete-entry?id=${id}`, { method: 'DELETE' })
let json = await res.json()
if (!res.ok) throw Error(json.message)
mutate('/api/get-entries')
setDeleting(false)
}
return (
<div>
<div className="flex items-center">
<Link href={`/entry/${id}`}>
<a className="font-bold py-2">{title}</a>
</Link>
<div className="flex ml-4">
<ButtonLink
href={`/entry/edit/${id}?title=${title}&content=${content}`}
className="h-5 py-0 mx-1"
>
Edit
</ButtonLink>
<Button
disabled={deleting}
onClick={deleteEntry}
className="h-5 py-0 mx-1"
>
{deleting ? 'Deleting ...' : 'Delete'}
</Button>
</div>
</div>
<p>{content}</p>
</div>
)
}
export default Entry