Handlers for GET and POST engages added, renders numbers of unique engagements per post

This commit is contained in:
Hollgy 2024-03-22 22:38:18 +01:00
parent efcdaf0cd2
commit ea25fa9489
4 changed files with 75 additions and 27 deletions

View file

@ -1,21 +1,54 @@
import { JSXElement } from "solid-js"; import { JSXElement, createSignal,onMount,useContext } from "solid-js";
import { EngagementIcon } from "../../Util/Icons"; 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 ( return (
<>
<div class="flex p-1"> <div class="flex p-1">
<button <button
class="rounded-base btn btn-xs hover:border-primary" class="rounded-base btn btn-xs hover:border-primary"
aria-label="Show sign of engagement" aria-label="Show sign of engagement"
onClick={handleEngagement} // Call handleEngagement function on button click
> >
<EngagementIcon /> <EngagementIcon />
</button> </button>
<span class="text-1xl countdown px-1.5 pt-1.5 text-center"> <span class="text-1xl countdown px-1.5 pt-1.5 text-center">
<p style={{ "--value": 46 }}></p> <p style={{ "--value": engagementCount() }}></p>
</span> </span>
</div> </div>
</>
); );
} }

View file

@ -1,15 +1,15 @@
import { JSXElement, Show } from "solid-js"; import { JSXElement, Show } from "solid-js";
import { Post } from "../Util/api"; import { Post } from "../Util/api";
import CommentsBUtton from "./Buttons/CommentsButton"; import CommentsButton from "./Buttons/CommentsButton";
import EngagementButton from "./Buttons/Engegament"; import EngagementButton from "./Buttons/Engegament";
import RemovePostButton from "./Buttons/RemovePostButton"; import RemovePostButton from "./Buttons/RemovePostButton";
import ReportButton from "./Buttons/Report"; import ReportButton from "./Buttons/Report";
import ToPostButton from "./Buttons/ToPost"; import ToPostButton from "./Buttons/ToPost";
export function PostSegment(props: { post: Post }): JSXElement { export function PostSegment(props: { post: Post }): JSXElement {
const dateOfCreation = new Date(props.post.createdAt).toDateString(); const { post } = props; // Destructuring the post object from props
const isEdited = !(props.post.createdAt == props.post.updatedAt); const dateOfCreation = new Date(post.createdAt).toDateString();
const isEdited = !(post.createdAt == post.updatedAt);
return ( return (
<div class="card compact w-full flex-grow border-b-2 border-b-primary bg-base-200 text-base-content transition-all hover:bg-base-300"> <div class="card compact w-full flex-grow border-b-2 border-b-primary bg-base-200 text-base-content transition-all hover:bg-base-300">
@ -31,13 +31,13 @@ export function PostSegment(props: { post: Post }): JSXElement {
<Show when={isEdited}> <Show when={isEdited}>
<p>This post has been edited</p> <p>This post has been edited</p>
</Show> </Show>
<p class="my-5 text-base">{props.post.content}</p> <p class="my-5 text-base">{post.content}</p>
<div class="card-actions justify-between"> <div class="card-actions justify-between">
<div class="flex"> <div class="flex">
<EngagementButton /> <EngagementButton postId={post.id} />
<CommentsBUtton /> <CommentsButton />
</div> </div>
<ToPostButton post={props.post} /> <ToPostButton post={post} />
</div> </div>
</div> </div>
</div> </div>

View file

@ -90,9 +90,13 @@ export async function getComments(
return data; return data;
} }
/** Incomplete */ /**
export async function engagePost(postId: string): Promise<void> { * Gets the Engagement counts for a post by postId
const res = await fetch(`/api/engage_post?post_id=${postId}`); * @param postId The id of the post
* @returns {Promise<number>} A promise that contains number of post engages
*/
export async function getEngagementCount(postId: string): Promise<number> {
const res = await fetch(`/api/posts/${postId}/engage`);
const data = await res.json(); const data = await res.json();
return data; return data;
} }

View file

@ -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")] #[get("/posts/{id}/engage")]