From 1e276c8dc93934547380d449b84edf0e85b52c44 Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Mon, 18 Dec 2023 09:00:03 +0100 Subject: [PATCH] Pagination --- apps/dashboard/src/actions/schema.ts | 5 ++++ .../src/actions/update-team-action.ts | 2 +- apps/dashboard/src/actions/vote-action.ts | 24 ++++++++++++++++--- apps/dashboard/src/components/vote-button.tsx | 18 ++++---------- apps/dashboard/src/components/vote.tsx | 2 +- .../supabase/src/queries/cached-queries.ts | 8 +++++++ 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/apps/dashboard/src/actions/schema.ts b/apps/dashboard/src/actions/schema.ts index 670228b421..18027262a3 100644 --- a/apps/dashboard/src/actions/schema.ts +++ b/apps/dashboard/src/actions/schema.ts @@ -98,3 +98,8 @@ export const shareFileSchema = z.object({ filepath: z.string(), expireIn: z.number(), }); + +export const voteSchema = z.object({ + revalidatePath: z.string(), + id: z.string(), +}); diff --git a/apps/dashboard/src/actions/update-team-action.ts b/apps/dashboard/src/actions/update-team-action.ts index c047006bfd..85297b255a 100644 --- a/apps/dashboard/src/actions/update-team-action.ts +++ b/apps/dashboard/src/actions/update-team-action.ts @@ -15,5 +15,5 @@ export const updateTeamAction = action( revalidatePathFunc(revalidatePath); return team; - }, + } ); diff --git a/apps/dashboard/src/actions/vote-action.ts b/apps/dashboard/src/actions/vote-action.ts index 12b188caac..51538ee0c4 100644 --- a/apps/dashboard/src/actions/vote-action.ts +++ b/apps/dashboard/src/actions/vote-action.ts @@ -1,9 +1,27 @@ "use server"; import { client } from "@midday/kv"; +import { createClient } from "@midday/supabase/server"; import { revalidatePath as revalidatePathFunc } from "next/cache"; +import { action } from "./safe-action"; +import { voteSchema } from "./schema"; + +export const voteAction = action(voteSchema, async ({ revalidatePath, id }) => { + const supabase = createClient(); + const { + data: { session }, + } = await supabase.auth.getSession(); + + const hasUser = await client.sadd( + `apps:v2:${id}:user:${session.user.id}`, + true + ); + + if (!hasUser) { + throw new Error("You have already voted"); + } + + await client.incr(`apps:v2:${id}`); -export const voteAction = async (id: string, revalidatePath: string) => { - await client.incr(id); revalidatePathFunc(revalidatePath); -}; +}); diff --git a/apps/dashboard/src/components/vote-button.tsx b/apps/dashboard/src/components/vote-button.tsx index 2081166658..8171170f6f 100644 --- a/apps/dashboard/src/components/vote-button.tsx +++ b/apps/dashboard/src/components/vote-button.tsx @@ -3,34 +3,26 @@ import { voteAction } from "@/actions/vote-action"; import { Button } from "@midday/ui/button"; import { ChevronUp } from "lucide-react"; -import { useOptimistic, useTransition } from "react"; +import { useOptimisticAction } from "next-safe-action/hook"; export function VoteButton({ count, id }) { - const [_, startTransition] = useTransition(); - - const [optimisticCount, addOptimisticCount] = useOptimistic( + const { execute, optimisticData } = useOptimisticAction( + voteAction, count, (prevCount) => { return +prevCount + 1; } ); - const handleVote = () => { - startTransition(() => { - addOptimisticCount(); - voteAction(id, "/apps"); - }); - }; - return ( ); diff --git a/apps/dashboard/src/components/vote.tsx b/apps/dashboard/src/components/vote.tsx index e93f911746..6353dadb3c 100644 --- a/apps/dashboard/src/components/vote.tsx +++ b/apps/dashboard/src/components/vote.tsx @@ -2,7 +2,7 @@ import { VoteButton } from "@/components/vote-button"; import { client } from "@midday/kv"; export async function Vote({ id }) { - const count = await client.mget(id); + const count = await client.mget(`apps:v2:${id}`); return ; } diff --git a/packages/supabase/src/queries/cached-queries.ts b/packages/supabase/src/queries/cached-queries.ts index 2f06e1001f..ab2b0d328b 100644 --- a/packages/supabase/src/queries/cached-queries.ts +++ b/packages/supabase/src/queries/cached-queries.ts @@ -26,6 +26,7 @@ export const getTransactions = async (params) => { }, ["transactions", teamId], { + revalidate: 180, tags: [`transactions_${teamId}`], } )(params); @@ -47,6 +48,7 @@ export const getUser = async () => { ["user", userId], { tags: [`user_${userId}`], + revalidate: 3600, } )(userId); }; @@ -67,6 +69,7 @@ export const getBankConnectionsByTeamId = async () => { ["bank_connections", teamId], { tags: [`bank_connections_${teamId}`], + revalidate: 3600, } )(teamId); }; @@ -88,6 +91,7 @@ export const getTeamBankAccounts = async () => { ["bank_accounts", teamId], { tags: [`bank_accounts_${teamId}`], + revalidate: 180, } )(teamId); }; @@ -109,6 +113,7 @@ export const getTeamMembers = async () => { ["team_members", teamId], { tags: [`bank_members_${teamId}`], + revalidate: 180, } )(teamId); }; @@ -129,6 +134,7 @@ export const getSpending = async (params) => { ["spending", teamId], { tags: [`spending_${teamId}`], + revalidate: 180, } )(params); }; @@ -150,6 +156,7 @@ export const getMetrics = async (params) => { ["metrics", teamId], { tags: [`metrics_${teamId}`], + revalidate: 180, } )(params); }; @@ -171,6 +178,7 @@ export const getVault = async (params) => { ["vault", teamId], { tags: [`vault_${teamId}`], + revalidate: 3600, } )(params); };