Draft implementation of comments
This commit is contained in:
parent
62f3e0d72f
commit
fafce18907
2 changed files with 38 additions and 2 deletions
|
@ -1,18 +1,29 @@
|
||||||
import { useParams } from "@solidjs/router";
|
import { useParams } from "@solidjs/router";
|
||||||
import { JSXElement, Show, Suspense, createResource } from "solid-js";
|
import { For, JSXElement, Show, Suspense, createResource } from "solid-js";
|
||||||
|
|
||||||
import { loadSpinner } from "../Util/Icons";
|
import { loadSpinner } from "../Util/Icons";
|
||||||
import { getPost } from "../Util/api";
|
import { getComments, getPost } from "../Util/api";
|
||||||
import { PostSegment } from "./Posts";
|
import { PostSegment } from "./Posts";
|
||||||
|
|
||||||
export function SinglePost(): JSXElement {
|
export function SinglePost(): JSXElement {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const [post] = createResource(params.postid, getPost);
|
const [post] = createResource(params.postid, getPost);
|
||||||
|
const [comments] = createResource(params.postid, () =>
|
||||||
|
getComments(params.postid, 0, 10)
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Suspense fallback={loadSpinner()}>
|
<Suspense fallback={loadSpinner()}>
|
||||||
<Show when={post()}>
|
<Show when={post()}>
|
||||||
<PostSegment post={post()!} />
|
<PostSegment post={post()!} />
|
||||||
|
<For each={comments()!}>
|
||||||
|
{(comment) => (
|
||||||
|
// TODO: This should be a separate component
|
||||||
|
<div class="comment">
|
||||||
|
<p>{comment.content}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</For>
|
||||||
</Show>
|
</Show>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
);
|
);
|
||||||
|
|
|
@ -22,6 +22,18 @@ export interface AuthResponse {
|
||||||
token: string;
|
token: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is what a public comment looks like, as it arrives from the server
|
||||||
|
export interface PublicComment {
|
||||||
|
id: number;
|
||||||
|
parent_post_id: number;
|
||||||
|
parent_comment_id: number | null;
|
||||||
|
upvotes: number;
|
||||||
|
downvotes: number;
|
||||||
|
content: string;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getPosts(): Promise<Post[]> {
|
export async function getPosts(): Promise<Post[]> {
|
||||||
const res = await fetch("/api/posts");
|
const res = await fetch("/api/posts");
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
@ -44,6 +56,19 @@ export async function createPost(post: NewPost): Promise<void> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets the comments for a specific post
|
||||||
|
export async function getComments(
|
||||||
|
postId: string,
|
||||||
|
limit: number,
|
||||||
|
offset: number
|
||||||
|
): Promise<PublicComment[]> {
|
||||||
|
const res = await fetch(
|
||||||
|
`/api/comments?post_id=${postId}&limit=${limit}&offset=${offset}`
|
||||||
|
);
|
||||||
|
const data = await res.json();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
// Send the registration request to the server
|
// Send the registration request to the server
|
||||||
export async function submitRegistration(
|
export async function submitRegistration(
|
||||||
username: string,
|
username: string,
|
||||||
|
|
Loading…
Reference in a new issue