Skip to content

Commit

Permalink
fix: resolve build issues with server actions
Browse files Browse the repository at this point in the history
- Move server action to separate file with 'use server' directive
- Fix syntax error in page component
- Update imports to use new actions file

Co-Authored-By: ben <ben@prologe.io>
  • Loading branch information
devin-ai-integration[bot] and benjaminshafii committed Dec 25, 2024
1 parent d569ef4 commit 11604c7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
62 changes: 62 additions & 0 deletions packages/web/app/claim-offer/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use server';

import { db, UserUsageTable, createEmptyUserUsage } from "@/drizzle/schema";
import { eq, sql } from "drizzle-orm";
import { revalidatePath } from 'next/cache';
import { handleAuthorizationV2 } from "@/lib/handleAuthorization";
import { headers } from 'next/headers';
import { NextRequest } from 'next/server';

export async function claimTokens() {
try {
// Check if after deadline
const deadline = new Date('2025-01-01');
if (new Date() > deadline) {
return { error: "This offer has expired" };
}

// Get authenticated user
const headersList = headers();
const request = new NextRequest('https://dummy.url', {
headers: headersList,
});

const { userId } = await handleAuthorizationV2(request);
if (!userId) {
return { error: "You must be logged in to claim tokens" };
}

// Get or create user usage record
let [usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId));

if (!usage) {
await createEmptyUserUsage(userId);
[usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId));
}

// Check if already claimed
if (usage.maxTokenUsage >= 5_000_000) {
return { error: "You have already claimed this offer" };
}

// Increase maxTokenUsage by 5M
const result = await db
.update(UserUsageTable)
.set({
maxTokenUsage: sql`GREATEST(${UserUsageTable.maxTokenUsage}, 5000000)`,
subscriptionStatus: "active",
paymentStatus: "succeeded"
})
.where(eq(UserUsageTable.userId, userId))
.returning({ newMaxTokens: UserUsageTable.maxTokenUsage });

revalidatePath('/claim-offer');
return {
success: true,
maxTokens: result[0].newMaxTokens
};
} catch (error: any) {
console.error("Error claiming tokens:", error);
return { error: error.message || "Failed to claim tokens" };
}
}
2 changes: 1 addition & 1 deletion packages/web/app/claim-offer/claim-button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { useState } from "react";
import { claimTokens } from "./page";
import { claimTokens } from "./actions";

export function ClaimButton() {
const [loading, setLoading] = useState(false);
Expand Down
62 changes: 0 additions & 62 deletions packages/web/app/claim-offer/page.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,6 @@
import Image from 'next/image';
import { db, UserUsageTable, createEmptyUserUsage } from "@/drizzle/schema";
import { eq, sql } from "drizzle-orm";
import { revalidatePath } from 'next/cache';
import { handleAuthorizationV2 } from "@/lib/handleAuthorization";
import { headers } from 'next/headers';
import { NextRequest } from 'next/server';
import { ClaimButton } from './claim-button';

export async function claimTokens() {
'use server'

try {
// Check if after deadline
const deadline = new Date('2025-01-01');
if (new Date() > deadline) {
return { error: "This offer has expired" };
}

// Get authenticated user
const headersList = headers();
const request = new NextRequest('https://dummy.url', {
headers: headersList,
});

const { userId } = await handleAuthorizationV2(request);
if (!userId) {
return { error: "You must be logged in to claim tokens" };
}

// Get or create user usage record
let [usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId));

if (!usage) {
await createEmptyUserUsage(userId);
[usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId));
}

// Check if already claimed
if (usage.maxTokenUsage >= 5_000_000) {
return { error: "You have already claimed this offer" };
}

// Increase maxTokenUsage by 5M
const result = await db
.update(UserUsageTable)
.set({
maxTokenUsage: sql`GREATEST(${UserUsageTable.maxTokenUsage}, 5000000)`,
subscriptionStatus: "active",
paymentStatus: "succeeded"
})
.where(eq(UserUsageTable.userId, userId))
.returning({ newMaxTokens: UserUsageTable.maxTokenUsage });

revalidatePath('/claim-offer');
return {
success: true,
maxTokens: result[0].newMaxTokens
};
} catch (error: any) {
console.error("Error claiming tokens:", error);
return { error: error.message || "Failed to claim tokens" };
}
}

export default function ClaimOfferPage() {
return (
<div className="max-w-md mx-auto p-6 mt-8 text-center space-y-6">
Expand Down

0 comments on commit 11604c7

Please sign in to comment.