Skip to content

Commit b8bc1ac

Browse files
committed
Create Single Article Page
1 parent ba8317a commit b8bc1ac

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/components/Blog/Main.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ const Main = () => {
2222
{data.articles.map((article: Article, index) => {
2323
if (index % 3 == 0) {
2424
return (
25-
<Link href={`article/?article=${article._id}`}>
25+
<Link href={`article/${article._id}`}>
2626
<ArticlePreview article={article} width={[`auto`, `auto`, `50%`, `50%`]}/>
2727
</Link>
2828
)
2929
}
3030

3131
if (index % 3 == 1) {
3232
return (
33-
<ArticlePreview article={article} width={[`auto`, `auto`, `50%`, `50%`]}/>
33+
<Link href={`article/${article._id}`}>
34+
<ArticlePreview article={article} width={[`auto`, `auto`, `50%`, `50%`]}/>
35+
</Link>
3436
)
3537
}
3638
})}

src/pages/api/article/get-all.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,10 @@ export default async (request: NowRequest, response: NowResponse) => {
1010
const db = await connectToDatabase(process.env.MONGODB_URI)
1111
const collection = db.collection('articles')
1212

13-
let articles = []
14-
15-
if (page) {
16-
articles = await collection.find({})
13+
const articles = await collection.find({})
1714
.limit(numberOfArticlesPerPage * currentPage)
1815
.sort({sentAt: -1})
1916
.toArray()
20-
}
21-
22-
else {
23-
articles = await collection.find({})
24-
.toArray()
25-
}
2617

2718
const numberOfArticles = await collection.countDocuments()
2819

src/pages/article/[article].tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useRouter } from 'next/router'
2+
import {useFetch} from "../../services/swr";
3+
import Loading from "../../components/message/Loading";
4+
5+
const Article = () => {
6+
const router = useRouter()
7+
const { article } = router.query
8+
9+
const {data} = useFetch(`api/article/find-one/?article=${article}`)
10+
11+
if (!data) return <Loading/>
12+
13+
return <p>Post: {data.articles.title}</p>
14+
}
15+
16+
export default Article

0 commit comments

Comments
 (0)