Skip to content

Commit

Permalink
added credits count in settings. added 5 initial credit logic in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dokmy committed Jan 13, 2024
1 parent f7a87b3 commit e3eb328
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/app/(root)/(routes)/settings/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import React from "react";
import SubscriptionButton from "@/components/subcription-button";
import { checkSubscription } from "@/lib/subscriptions";
import { getSearchCreditCount } from "@/lib/searchCredits";
import { auth } from "@clerk/nextjs";

const SettingsPage = async () => {
const { userId } = auth();
const hasSubscription = await checkSubscription();

if (!userId) {
return <div>No user Id. Please login again.</div>;
}

const creditCount = await getSearchCreditCount(userId);

return (
<div className="h-full p-4 space-y-2">
<h3 className="text-lg font-medium">Settings</h3>
<div className="text-muted-foreground text-sm">
{hasSubscription
? "You are currently on a pro plan."
: "You are currently on a free plan."}
? `You are currently on a pro plan. Search credits left: ${creditCount}`
: `You are currently on a free plan. Search credits left: ${creditCount}`}
</div>
<div>
<SubscriptionButton hasSubscription={hasSubscription} />
Expand Down
15 changes: 15 additions & 0 deletions src/app/(root)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import Navbar from "@/components/navbar";
import { Sidebar } from "@/components/sidebar";
import { checkSubscription } from "@/lib/subscriptions";
import { incrementSearchCredit, checkSearchCredits } from "@/lib/searchCredits";
import { auth } from "@clerk/nextjs";
import { redirect } from "next/navigation";

const RootLayout = async ({ children }: { children: React.ReactNode }) => {
const { userId } = auth();
const hasSubscription = await checkSubscription();

if (!userId) {
return redirect("/");
}
const inSearchCreditsDb = await checkSearchCredits(userId);

if (!inSearchCreditsDb) {
await incrementSearchCredit(userId, 5);
console.log("First time logging in. 5 credits are given.");
}

return (
<div className="flex flex-col h-screen">
<div className="fixed inset-y-0 w-full h-16 z-20">
Expand Down
8 changes: 3 additions & 5 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dayjs from "dayjs";
import prismadb from '../../lib/prismadb';
import { auth, currentUser } from "@clerk/nextjs";
import { NextResponse } from 'next/server';
import { checkSearchCredits, deductSearchCredit, getSearchCreditCount, incrementSearchCredit } from '@/lib/searchCredits';
import { checkSearchCredits, deductSearchCredit, getSearchCreditCount } from '@/lib/searchCredits';



Expand Down Expand Up @@ -52,8 +52,7 @@ export async function POST(req: Request) {
const inSearchCreditsDb = await checkSearchCredits(userId)

if (!inSearchCreditsDb) {
await incrementSearchCredit(userId, 5)
console.log("First time searching. Added 5 credits.")
return new NextResponse("Not inside credits database", {status: 401})
}

const creditsLeft = await getSearchCreditCount(userId)
Expand All @@ -63,10 +62,9 @@ export async function POST(req: Request) {
}

if (creditsLeft == false) {
return new NextResponse("Not inside credits database", {status: 401})
return new NextResponse("Credits left is null.", {status: 401})
}



if (creditsLeft > 0) {
await deductSearchCredit(userId)
Expand Down

0 comments on commit e3eb328

Please sign in to comment.