From ea25fa9489690f38d52a50721d6c9b80cd30fbea Mon Sep 17 00:00:00 2001 From: Hollgy Date: Fri, 22 Mar 2024 22:38:18 +0100 Subject: [PATCH] Handlers for GET and POST engages added, renders numbers of unique engagements per post --- .../src/Components/Buttons/Engegament.tsx | 63 ++++++++++++++----- client-solid/src/Components/PostSegment.tsx | 16 ++--- client-solid/src/Util/api.ts | 10 ++- server/src/routes/post.rs | 13 +++- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/client-solid/src/Components/Buttons/Engegament.tsx b/client-solid/src/Components/Buttons/Engegament.tsx index c768418..de87ae5 100644 --- a/client-solid/src/Components/Buttons/Engegament.tsx +++ b/client-solid/src/Components/Buttons/Engegament.tsx @@ -1,21 +1,54 @@ -import { JSXElement } from "solid-js"; +import { JSXElement, createSignal,onMount,useContext } from "solid-js"; import { EngagementIcon } from "../../Util/Icons"; +import { engage } from "../../Util/api"; +import { LoginContext } from "../../Context/GlobalState"; +import { getEngagementCount } from "../../Util/api"; + +export default function EngagementButton({ + postId, +}: { + postId: string; +}): JSXElement { + const [engagementCount, setEngagementCount] = createSignal(0); + const login_ctx = useContext(LoginContext)!; + + onMount((): void => { + void setUp() + }) + + const setUp = async () => { + const r = await getEngagementCount(postId) + setEngagementCount(r) + } + + // Function to handle engagement + const handleEngagement = async () => { + try { + const response = await engage(postId, login_ctx.token()) ; + if (response.ok) { + // Update engagement count if the request is successful + setEngagementCount(await response.json()); + } else { + console.error("Failed to engage:", response.statusText); + } + } catch (error) { + console.error("Error engaging:", error); + } + }; -export default function EngagementButton(): JSXElement { return ( - <> -
- - -

-
-
- +
+ + +

+
+
); } diff --git a/client-solid/src/Components/PostSegment.tsx b/client-solid/src/Components/PostSegment.tsx index 0992c83..756076a 100644 --- a/client-solid/src/Components/PostSegment.tsx +++ b/client-solid/src/Components/PostSegment.tsx @@ -1,15 +1,15 @@ import { JSXElement, Show } from "solid-js"; - import { Post } from "../Util/api"; -import CommentsBUtton from "./Buttons/CommentsButton"; +import CommentsButton from "./Buttons/CommentsButton"; import EngagementButton from "./Buttons/Engegament"; import RemovePostButton from "./Buttons/RemovePostButton"; import ReportButton from "./Buttons/Report"; import ToPostButton from "./Buttons/ToPost"; export function PostSegment(props: { post: Post }): JSXElement { - const dateOfCreation = new Date(props.post.createdAt).toDateString(); - const isEdited = !(props.post.createdAt == props.post.updatedAt); + const { post } = props; // Destructuring the post object from props + const dateOfCreation = new Date(post.createdAt).toDateString(); + const isEdited = !(post.createdAt == post.updatedAt); return (
@@ -31,13 +31,13 @@ export function PostSegment(props: { post: Post }): JSXElement {

This post has been edited

-

{props.post.content}

+

{post.content}

- - + +
- +
diff --git a/client-solid/src/Util/api.ts b/client-solid/src/Util/api.ts index 7dcd384..b1d01aa 100644 --- a/client-solid/src/Util/api.ts +++ b/client-solid/src/Util/api.ts @@ -90,9 +90,13 @@ export async function getComments( return data; } -/** Incomplete */ -export async function engagePost(postId: string): Promise { - const res = await fetch(`/api/engage_post?post_id=${postId}`); +/** + * Gets the Engagement counts for a post by postId + * @param postId The id of the post + * @returns {Promise} A promise that contains number of post engages + */ +export async function getEngagementCount(postId: string): Promise { + const res = await fetch(`/api/posts/${postId}/engage`); const data = await res.json(); return data; } diff --git a/server/src/routes/post.rs b/server/src/routes/post.rs index fa97264..3728f0c 100755 --- a/server/src/routes/post.rs +++ b/server/src/routes/post.rs @@ -101,7 +101,18 @@ pub async fn engage_post( } } - return Ok(HttpResponse::Ok().json("Engaged")); + // Get engagement count + let q = sqlx::query!("SELECT COUNT(*) FROM engagements WHERE post_id = $1", post_id) + .fetch_one(&state.pool) + .await; + + match q { + Ok(count) => Ok(HttpResponse::Ok().json(count.count)), + Err(e) => { + info!("Error getting engagements: {}", e); + Ok(HttpResponse::InternalServerError().json("Error")) + } + } } #[get("/posts/{id}/engage")]