From 36831785effd90c7bcf1c97ea39c715410b2383c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:31:33 +0100 Subject: [PATCH 1/6] fix: upvotes issue --- src/AppwriteService.ts | 1 - src/components/blocks/Upvote.tsx | 29 +++++++++++------------ src/components/layout/Project.tsx | 2 +- src/components/layout/ProjectFeatured.tsx | 6 +---- src/routes/projects/[projectId]/index.tsx | 2 +- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/AppwriteService.ts b/src/AppwriteService.ts index d39a8b5..e89b88f 100644 --- a/src/AppwriteService.ts +++ b/src/AppwriteService.ts @@ -190,7 +190,6 @@ export const AppwriteService = { listUserUpvotes: async (userId: string) => { return ( await databases.listDocuments("main", "projectUpvotes", [ - Query.limit(100), Query.equal("userId", userId), Query.orderDesc("$createdAt"), ]) diff --git a/src/components/blocks/Upvote.tsx b/src/components/blocks/Upvote.tsx index 1728a7f..1b1827f 100644 --- a/src/components/blocks/Upvote.tsx +++ b/src/components/blocks/Upvote.tsx @@ -13,17 +13,16 @@ import { AccountContext, UpvotesContext } from "~/routes/layout"; type Props = { projectId: string; votes: number; - inline?: boolean; }; export default component$((props: Props) => { - const upvoteContext = useContext(UpvotesContext); - const accountContext = useContext(AccountContext); + const upvotes = useContext(UpvotesContext); + const account = useContext(AccountContext); const isLoading = useSignal(false); const isUpvotedServer = useComputed$(() => { - return !!upvoteContext.value.find( + return !!upvotes.value.find( (upvote) => upvote.projectId === props.projectId ); }); @@ -32,14 +31,14 @@ export default component$((props: Props) => { return isLoading.value ? isUpvotedClient.value : isUpvotedServer.value; }); - const votesServer = useSignal(props.votes); - const votes = useComputed$(() => { + const countServer = useSignal(props.votes); + const count = useComputed$(() => { if (isLoading.value) { return isUpvotedClient.value - ? votesServer.value + 1 - : votesServer.value - 1; + ? countServer.value + 1 + : countServer.value - 1; } - return votesServer.value; + return countServer.value; }); const upvoteProject = $(async (e: QwikMouseEvent) => { @@ -50,12 +49,12 @@ export default component$((props: Props) => { try { const res = await AppwriteService.upvoteProject(props.projectId); - if (accountContext.value) { - upvoteContext.value = await AppwriteService.listUserUpvotes( - accountContext.value.$id + if (account.value) { + upvotes.value = await AppwriteService.listUserUpvotes( + account.value.$id ); } - votesServer.value = res.votes; + countServer.value = res.votes; } catch (error: unknown) { if (error instanceof AppwriteException && error.code === 401) { alert("Please sign in first."); @@ -72,14 +71,14 @@ export default component$((props: Props) => { ); }); diff --git a/src/components/layout/Project.tsx b/src/components/layout/Project.tsx index ecfe49f..d0c2ac1 100644 --- a/src/components/layout/Project.tsx +++ b/src/components/layout/Project.tsx @@ -49,7 +49,7 @@ export default component$((props: { project: Project | null }) => { {project.name}

- +

{ > {project.name} - +

diff --git a/src/routes/projects/[projectId]/index.tsx b/src/routes/projects/[projectId]/index.tsx index 564b5bb..6560fb4 100644 --- a/src/routes/projects/[projectId]/index.tsx +++ b/src/routes/projects/[projectId]/index.tsx @@ -117,7 +117,7 @@ export default component$(() => {

{project.name}

- +

{project.tagline}

From a617d082b4e5643d097127f61b7aa668dc8c65c8 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 16 Jun 2023 16:25:56 +0100 Subject: [PATCH 2/6] feat: selective upvote fetching --- src/AppwriteService.ts | 18 ++++++++---- src/components/blocks/Upvote.tsx | 29 ++++++++---------- src/components/hooks/useUpvotes.ts | 36 +++++++++++++++++++++++ src/routes/index.tsx | 32 +++++++++++++++++--- src/routes/layout.tsx | 27 +++++++++-------- src/routes/projects/[projectId]/index.tsx | 6 +++- src/routes/search/index.tsx | 30 +++++++++++++++++-- 7 files changed, 137 insertions(+), 41 deletions(-) create mode 100644 src/components/hooks/useUpvotes.ts diff --git a/src/AppwriteService.ts b/src/AppwriteService.ts index e89b88f..e049a9d 100644 --- a/src/AppwriteService.ts +++ b/src/AppwriteService.ts @@ -187,12 +187,20 @@ export const AppwriteService = { ) ).documents; }, - listUserUpvotes: async (userId: string) => { + listUserUpvotes: async (userId: string, queries: string[] = []) => { + const defaultQueries = [ + Query.equal("userId", userId), + Query.orderDesc("$createdAt"), + ]; + + queries = [...queries, ...defaultQueries]; + return ( - await databases.listDocuments("main", "projectUpvotes", [ - Query.equal("userId", userId), - Query.orderDesc("$createdAt"), - ]) + await databases.listDocuments( + "main", + "projectUpvotes", + queries + ) ).documents; }, uploadThumbnail: async (file: File) => { diff --git a/src/components/blocks/Upvote.tsx b/src/components/blocks/Upvote.tsx index 1b1827f..18fff16 100644 --- a/src/components/blocks/Upvote.tsx +++ b/src/components/blocks/Upvote.tsx @@ -6,7 +6,6 @@ import { useContext, useSignal, } from "@builder.io/qwik"; -import { AppwriteException } from "appwrite"; import { AppwriteService } from "~/AppwriteService"; import { AccountContext, UpvotesContext } from "~/routes/layout"; @@ -22,9 +21,8 @@ export default component$((props: Props) => { const isLoading = useSignal(false); const isUpvotedServer = useComputed$(() => { - return !!upvotes.value.find( - (upvote) => upvote.projectId === props.projectId - ); + console.log("isUpvotedServer", props.projectId, upvotes[props.projectId]); + return upvotes[props.projectId]; }); const isUpvotedClient = useSignal(isUpvotedServer.value); const isUpvoted = useComputed$(() => { @@ -44,23 +42,20 @@ export default component$((props: Props) => { const upvoteProject = $(async (e: QwikMouseEvent) => { e.stopPropagation(); + if (!account.value) { + alert("Please sign in first."); + return; + } + isUpvotedClient.value = !isUpvotedServer.value; isLoading.value = true; try { - const res = await AppwriteService.upvoteProject(props.projectId); - if (account.value) { - upvotes.value = await AppwriteService.listUserUpvotes( - account.value.$id - ); - } - countServer.value = res.votes; + const response = await AppwriteService.upvoteProject(props.projectId); + upvotes[props.projectId] = response.isUpvoted; + countServer.value = response.votes; } catch (error: unknown) { - if (error instanceof AppwriteException && error.code === 401) { - alert("Please sign in first."); - } else { - alert("An unexpected error occurred."); - } + alert("An unexpected error occurred."); } finally { isUpvotedClient.value = isUpvotedServer.value; isLoading.value = false; @@ -71,7 +66,7 @@ export default component$((props: Props) => {