From edb7ef2fc403ca317e362fa2e60aea49367d0dd8 Mon Sep 17 00:00:00 2001 From: Hollgy Date: Wed, 6 Mar 2024 00:48:19 +0100 Subject: [PATCH] Initial draft of comments --- client-solid/src/Components/Comment.tsx | 47 +++++++++++++ .../src/Components/CommentSection.tsx | 21 ++++++ client-solid/src/Components/NewComment.tsx | 69 +++++++++++++++++++ client-solid/src/Components/SinglePost.tsx | 19 ++--- client-solid/src/Util/api.ts | 21 ++++++ 5 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 client-solid/src/Components/Comment.tsx create mode 100644 client-solid/src/Components/CommentSection.tsx create mode 100644 client-solid/src/Components/NewComment.tsx diff --git a/client-solid/src/Components/Comment.tsx b/client-solid/src/Components/Comment.tsx new file mode 100644 index 0000000..c3ce3c0 --- /dev/null +++ b/client-solid/src/Components/Comment.tsx @@ -0,0 +1,47 @@ +import { useNavigate } from "@solidjs/router"; +import { For, JSXElement, Show, createSignal } from "solid-js"; + +import { CheckMark, loadSpinner } from "../Util/Icons"; +import { PublicComment, getComments } from "../Util/api"; + +//exported into primary as a Route +export function Comment({ postId }: { postId: string }): JSXElement { + const [comments, setComments] = createSignal([] as PublicComment[]); + const [loading, setLoading] = createSignal(true); + + getComments(postId, 10, 0).then((comment) => { + setComments(comment); + setLoading(false); + }); + + return ( + + + {(comment): JSXElement => } + + + ); +} + +export function CommentSegment(props: { comment: PublicComment }): JSXElement { + const nav = useNavigate(); + return ( +
+
+

+ {props.comment?.content} +

+
+ +
+
+
+ ); +} diff --git a/client-solid/src/Components/CommentSection.tsx b/client-solid/src/Components/CommentSection.tsx new file mode 100644 index 0000000..7046465 --- /dev/null +++ b/client-solid/src/Components/CommentSection.tsx @@ -0,0 +1,21 @@ +import { + For, + JSXElement, + Show, + createResource, + createSignal, + useContext, +} from "solid-js"; + +import { getComments } from "../Util/api"; +import { CommentSegment } from "./Comment"; + +export function CommentSection({ postId }: { postId: string }): JSXElement { + const [comments] = createResource(postId, () => getComments(postId, 0, 10)); + + return ( + + {(comment) => } + + ); +} diff --git a/client-solid/src/Components/NewComment.tsx b/client-solid/src/Components/NewComment.tsx new file mode 100644 index 0000000..5d64272 --- /dev/null +++ b/client-solid/src/Components/NewComment.tsx @@ -0,0 +1,69 @@ +import { useNavigate } from "@solidjs/router"; +import { JSXElement, Show, createSignal, onMount, useContext } from "solid-js"; + +import { LoginContext } from "../Context/GlobalState"; +import { NewComment, createComment } from "../Util/api"; + +export function NewCommentInputArea({ + parentPostId, +}: { + parentPostId: number; +}): JSXElement { + const [content, setContent] = createSignal(""); + const [waiting, setWaiting] = createSignal(false); + + // We assumte this context is always available + const login_ctx = useContext(LoginContext)!; + + const nav = useNavigate(); + + const sendComment = (): void => { + setWaiting(true); + + const response = createComment({ + content: content(), + user_token: login_ctx.token(), + parent_post_id: parentPostId, + } as NewComment); + + if (response) { + response.then(() => { + setWaiting(false); + setContent(""); + nav("/"); + }); + } + }; + + // Bail out if not logged in + onMount(() => { + if (!login_ctx.loggedIn()) nav("/"); + }); + + return ( + } + > +
+