init: add code
This commit is contained in:
parent
475596444a
commit
1e09c02cb9
20 changed files with 2432 additions and 147 deletions
58
app/components/post.tsx
Normal file
58
app/components/post.tsx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
import StaticPost from "./staticPost";
|
||||
|
||||
export interface PostContent {
|
||||
id: number,
|
||||
title: string,
|
||||
content: string,
|
||||
userId: number,
|
||||
parentId: number,
|
||||
createdAt: string,
|
||||
author: {
|
||||
name: string,
|
||||
display_name: string,
|
||||
id: number
|
||||
},
|
||||
replies: any[];
|
||||
_count: {
|
||||
replies: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface PostProps {
|
||||
postId: number;
|
||||
}
|
||||
|
||||
|
||||
export default function Post({ postId }: PostProps) {
|
||||
const [postContent, setPostContent] = useState<PostContent>();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchPost = async () => {
|
||||
const response = await fetch(`http://localhost:8787/posts/${postId}/view`);
|
||||
const jsonContent: Array<PostContent> = await response.json();
|
||||
|
||||
let newPostContent: PostContent = jsonContent[0] as PostContent;
|
||||
|
||||
if (newPostContent) {
|
||||
setPostContent(newPostContent);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
fetchPost();
|
||||
}, []);
|
||||
|
||||
if (isLoading) {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
if (!postContent) {
|
||||
return <p>Failed to load!</p>;
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<StaticPost postContent={postContent} />
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue