fix(frontend/blog/post) fix compatibility with new backend

- switch to axios
This commit is contained in:
lumither committed 2024-05-04 22:54:38 +08:00
1 parent 4f09648a34
commit a4d0b7140a
4 files changed
+24 -11

No files matched your search

+3 -1
View File
@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Card, CardBody, CardHeader } from '@nextui-org/card'; import { Card, CardBody, CardHeader } from '@nextui-org/card';
import { Divider } from '@nextui-org/react'; import { Divider } from '@nextui-org/react';
import Link from 'next/link';
type Props = { type Props = {
post: JSON post: JSON
@@ -10,13 +11,14 @@ function BlogPostCard({ post }: Props) {
let title = (post as any)['title']; let title = (post as any)['title'];
let summary = (post as any)['summary']; let summary = (post as any)['summary'];
let last_update = (post as any)['last_update']; let last_update = (post as any)['last_update'];
let post_id = (post as any)['post_id'];
return ( return (
<div> <div>
{/*todo: to be update*/ } {/*todo: to be update*/ }
{/*<Link href={ `/blog/${ name }` }>*/ } {/*<Link href={ `/blog/${ name }` }>*/ }
<Card> <Card as={ Link } href={ `/blog/post/${ post_id }` }>
<CardHeader> <CardHeader>
<p className={ 'font-bold' }>{ title }</p> <p className={ 'font-bold' }>{ title }</p>
</CardHeader> </CardHeader>
+5 -3
View File
@@ -1,9 +1,11 @@
import axios from 'axios';
export default async function fetch_posts_list(): Promise<any> { export default async function fetch_posts_list(): Promise<any> {
const response = await fetch(`http://localhost:${ process.env.BACKEND_PORT }/api/get/post_list`); const response = await axios(`http://localhost:${ process.env.BACKEND_PORT }/api/get/post_list`);
if (!response.ok) { if (response.status !== 200) {
throw new Error(response.statusText); throw new Error(response.statusText);
} }
return await response.json(); return response.data;
} }
+14 -7
View File
@@ -8,19 +8,26 @@ import rehypeHighlight from 'rehype-highlight';
import rehypeSanitize from 'rehype-sanitize'; import rehypeSanitize from 'rehype-sanitize';
import rehypeRaw from 'rehype-raw'; import rehypeRaw from 'rehype-raw';
import remarkToc from 'remark-toc'; import remarkToc from 'remark-toc';
import remarkFrontmatter from 'remark-frontmatter';
import axios from 'axios';
type Props = { type Props = {
id: string id: string
} }
async function Generate(props: Props) { async function Generate(props: Props) {
const response = await fetch(`${ process.env.BLOG_ENDPOINT }/posts/${ props.id }`); const response = await axios(`http://localhost:${ process.env.BACKEND_PORT }/api/get/post/${ props.id }`);
if (!response.ok) { if (response.status !== 200) {
throw new Error(response.statusText); return (
<Card className={ `w-full max-w-[1024px]` }>
<CardBody>
Fatal: Failed to load the post.
</CardBody>
</Card>
);
} }
const post_md = await response.text();
console.log(post_md); let post_data = response.data;
return ( return (
<div> <div>
@@ -28,9 +35,9 @@ async function Generate(props: Props) {
<CardBody> <CardBody>
<Markdown <Markdown
className={ `mx-5 my-5 max-w-none prose dark:prose-invert sm:prose-sm md:prose-sm lg:prose-lg` } className={ `mx-5 my-5 max-w-none prose dark:prose-invert sm:prose-sm md:prose-sm lg:prose-lg` }
remarkPlugins={ [ remarkGfm, remarkToc ] } remarkPlugins={ [ remarkGfm, remarkToc, remarkFrontmatter ] }
rehypePlugins={ [ rehypeHighlight, rehypeRaw, rehypeSanitize ] } rehypePlugins={ [ rehypeHighlight, rehypeRaw, rehypeSanitize ] }
>{ post_md }</Markdown> >{ (post_data as any)['content'] }</Markdown>
</CardBody> </CardBody>
</Card> </Card>
+2
View File
@@ -10,6 +10,7 @@
}, },
"dependencies": { "dependencies": {
"@nextui-org/react": "^2.3.5", "@nextui-org/react": "^2.3.5",
"axios": "^1.6.8",
"framer-motion": "^11.1.7", "framer-motion": "^11.1.7",
"next": "14.2.2", "next": "14.2.2",
"next-themes": "^0.3.0", "next-themes": "^0.3.0",
@@ -20,6 +21,7 @@
"rehype-highlight": "^7.0.0", "rehype-highlight": "^7.0.0",
"rehype-raw": "^7.0.0", "rehype-raw": "^7.0.0",
"rehype-sanitize": "^6.0.0", "rehype-sanitize": "^6.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0", "remark-gfm": "^4.0.0",
"remark-toc": "^9.0.0" "remark-toc": "^9.0.0"
}, },