Skip to content

Commit

Permalink
🎨 Instant frontend update
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasGruber committed Feb 22, 2025
1 parent 904a459 commit bc1b27a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
23 changes: 13 additions & 10 deletions app/src/app/jutsus/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export default function MyJutsu() {
undefined,
{ enabled: !!userData },
);
const { data: recentTransfers } = api.jutsu.getRecentTransfers.useQuery(undefined, {
enabled: !!userData,
});

const userJutsuCounts = userJutsus?.map((userJutsu) => {
return {
Expand All @@ -75,6 +78,10 @@ export default function MyJutsu() {
};
});

// Transfer costs
const freeTransfers = getFreeTransfers(userData?.federalStatus || "NONE");
const usedTransfers = recentTransfers?.length || 0;

const onSettled = () => {
document.body.style.cursor = "default";
setIsOpen(false);
Expand Down Expand Up @@ -146,8 +153,13 @@ export default function MyJutsu() {
api.jutsu.transferLevel.useMutation({
onSuccess: async (data) => {
showMutationToast(data);
if (data.success) {
if (data.success && userData) {
await utils.jutsu.getUserJutsus.invalidate();
if (usedTransfers >= freeTransfers) {
await updateUser({
reputationPoints: userData.reputationPoints - JUTSU_TRANSFER_COST,
});
}
}
},
onSettled,
Expand Down Expand Up @@ -216,15 +228,6 @@ export default function MyJutsu() {
// Ryo from forgetting
const forgetRyo = 0;

// Transfer costs
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - JUTSU_TRANSFER_DAYS);
const { data: recentTransfers } = api.jutsu.getRecentTransfers.useQuery<
RouterOutputs["jutsu"]["getRecentTransfers"]
>({ cutoffDate }, { enabled: !!userData });
const freeTransfers = getFreeTransfers(userData?.federalStatus || "NONE");
const usedTransfers = recentTransfers?.length || 0;

// Loaders
if (!userData) return <Loader explanation="Loading userdata" />;

Expand Down
21 changes: 11 additions & 10 deletions app/src/server/api/routers/jutsu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ import type { DrizzleClient } from "@/server/db";
import { TRPCError } from "@trpc/server";

export const jutsuRouter = createTRPCRouter({
getRecentTransfers: protectedProcedure
.input(z.object({ cutoffDate: z.date() }))
.query(async ({ ctx, input }) => {
return await ctx.drizzle.query.actionLog.findMany({
where: and(
eq(actionLog.userId, ctx.userId),
eq(actionLog.relatedMsg, "JutsuLevelTransfer"),
gte(actionLog.createdAt, input.cutoffDate),
getRecentTransfers: protectedProcedure.query(async ({ ctx }) => {
return await ctx.drizzle.query.actionLog.findMany({
where: and(
eq(actionLog.userId, ctx.userId),
eq(actionLog.relatedMsg, "JutsuLevelTransfer"),
gte(
actionLog.createdAt,
secondsFromDate(-JUTSU_TRANSFER_DAYS * DAY_S, new Date()),
),
});
}),
),
});
}),
transferLevel: protectedProcedure
.input(
z.object({
Expand Down

0 comments on commit bc1b27a

Please sign in to comment.