-
Notifications
You must be signed in to change notification settings - Fork 0
/
[id].tsx
74 lines (65 loc) · 1.71 KB
/
[id].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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React from 'react'
import { GetServerSideProps } from 'next'
import ReactMarkdown from 'react-markdown'
import Layout from '../../components/Layout'
import Router from 'next/router'
import { PostProps } from '../../components/Post'
async function publish(id: number): Promise<void> {
await fetch(`http://localhost:3000/api/publish/${id}`, {
method: 'PUT',
})
await Router.push('/')
}
async function destroy(id: number): Promise<void> {
await fetch(`http://localhost:3000/api/post/${id}`, {
method: 'DELETE',
})
await Router.push('/')
}
const Post: React.FC<PostProps> = props => {
let title = props.title
if (!props.published) {
title = `${title} (Draft)`
}
return (
<Layout>
<div>
<h2>{title}</h2>
<p>By {props?.author?.name || 'Unknown author'}</p>
<ReactMarkdown children={props.content} />
{!props.published && (
<button onClick={() => publish(props.id)}>
Publish
</button>
)}
<button onClick={() => destroy(props.id)}>
Delete
</button>
</div>
<style jsx>{`
.page {
background: white;
padding: 2rem;
}
.actions {
margin-top: 2rem;
}
button {
background: #ececec;
border: 0;
border-radius: 0.125rem;
padding: 1rem 2rem;
}
button + button {
margin-left: 1rem;
}
`}</style>
</Layout>
)
}
export const getServerSideProps: GetServerSideProps = async (context) => {
const res = await fetch(`http://localhost:3000/api/post/${context.params.id}`)
const data = await res.json()
return { props: { ...data } }
}
export default Post