Skip to content

Commit

Permalink
Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 18, 2023
1 parent 8cacc21 commit 1e276c8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
2 changes: 1 addition & 1 deletion apps/dashboard/src/actions/update-team-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const updateTeamAction = action(
revalidatePathFunc(revalidatePath);

return team;
},
}
);
24 changes: 21 additions & 3 deletions apps/dashboard/src/actions/vote-action.ts
Original file line number Diff line number Diff line change
@@ -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);
};
});
18 changes: 5 additions & 13 deletions apps/dashboard/src/components/vote-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Button
variant="outline"
className="p-6 flex-col w-14 h-16"
onClick={handleVote}
onClick={() => execute({ revalidatePath: "/apps", id })}
>
<div className="flex space-x-2 items-center flex-col">
<ChevronUp size={16} />
{optimisticCount}
{optimisticData}
</div>
</Button>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/vote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <VoteButton count={count} id={id} />;
}
8 changes: 8 additions & 0 deletions packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getTransactions = async (params) => {
},
["transactions", teamId],
{
revalidate: 180,
tags: [`transactions_${teamId}`],
}
)(params);
Expand All @@ -47,6 +48,7 @@ export const getUser = async () => {
["user", userId],
{
tags: [`user_${userId}`],
revalidate: 3600,
}
)(userId);
};
Expand All @@ -67,6 +69,7 @@ export const getBankConnectionsByTeamId = async () => {
["bank_connections", teamId],
{
tags: [`bank_connections_${teamId}`],
revalidate: 3600,
}
)(teamId);
};
Expand All @@ -88,6 +91,7 @@ export const getTeamBankAccounts = async () => {
["bank_accounts", teamId],
{
tags: [`bank_accounts_${teamId}`],
revalidate: 180,
}
)(teamId);
};
Expand All @@ -109,6 +113,7 @@ export const getTeamMembers = async () => {
["team_members", teamId],
{
tags: [`bank_members_${teamId}`],
revalidate: 180,
}
)(teamId);
};
Expand All @@ -129,6 +134,7 @@ export const getSpending = async (params) => {
["spending", teamId],
{
tags: [`spending_${teamId}`],
revalidate: 180,
}
)(params);
};
Expand All @@ -150,6 +156,7 @@ export const getMetrics = async (params) => {
["metrics", teamId],
{
tags: [`metrics_${teamId}`],
revalidate: 180,
}
)(params);
};
Expand All @@ -171,6 +178,7 @@ export const getVault = async (params) => {
["vault", teamId],
{
tags: [`vault_${teamId}`],
revalidate: 3600,
}
)(params);
};

0 comments on commit 1e276c8

Please sign in to comment.