Skip to content

Commit

Permalink
Added receipts page(broken ui) and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvilmehta committed Apr 3, 2024
1 parent 895bcd3 commit 2c47eb0
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 36 deletions.
8 changes: 5 additions & 3 deletions src/app/api/razorpay/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export async function POST(req: NextRequest) {
currency: 'INR',
receipt: receipt.id,
payment_capture,
// notes: {

// }
notes: {
userId: body.data.userId,
courseId: body.data.courseId,
receipt: receipt.id,
},
};

try {
Expand Down
26 changes: 20 additions & 6 deletions src/app/new-courses/[courseId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ export default async function CourseDetails({
id: Number(params.courseId),
},
});
if (!course) return null;
if (!course) return <div>Course does not exist</div>;

const ifPurchasedByUser =
(await db.userPurchases.count({
where: {
userId: session.user.id,
courseId: course.id,
},
})) > 0
? true
: false;

return (
<Card className="w-500" key={course.id}>
Expand All @@ -41,11 +51,15 @@ export default async function CourseDetails({
</CardHeader>
<CardFooter className="flex justify-between">
<Button variant="outline">INR {course.price}</Button>
<RazorPayComponent
userId={session.user.id}
courseId={course.id}
key={course.id}
/>
{ifPurchasedByUser ? (
<button>View Receipt</button>
) : (
<RazorPayComponent
userId={session.user.id}
courseId={course.id}
key={course.id}
/>
)}
</CardFooter>
</Card>
);
Expand Down
39 changes: 21 additions & 18 deletions src/app/new-courses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,27 @@ export default async function PurchasePage() {
return (
<>
<main className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 place-items-center w-full pt-5 gap-5">
{/* TODO: Improve this UI */}
{courses.map((course) => (
<Card className="w-500" key={course.id}>
<CardContent className="flex justify-center">
<Image src={'/harkirat.png'} alt="" width={200} height={100} />
</CardContent>
<CardHeader>
<CardTitle>{course.title}</CardTitle>
<CardDescription>{course.description}</CardDescription>
</CardHeader>
<CardFooter className="flex justify-between">
<Button variant="outline">INR {course.price}</Button>
<Link href={`/new-courses/${course.id}`}>
<Button>View Details</Button>
</Link>
</CardFooter>
</Card>
))}
{courses.length === 0 ? (
<div>No New Courses to Buy</div>
) : (
courses.map((course) => (
<Card className="w-500" key={course.id}>
<CardContent className="flex justify-center">
<Image src={'/harkirat.png'} alt="" width={200} height={100} />
</CardContent>
<CardHeader>
<CardTitle>{course.title}</CardTitle>
<CardDescription>{course.description}</CardDescription>
</CardHeader>
<CardFooter className="flex justify-between">
<Button variant="outline">INR {course.price}</Button>
<Link href={`/new-courses/${course.id}`}>
<Button>View Details</Button>
</Link>
</CardFooter>
</Card>
))
)}
</main>
</>
);
Expand Down
57 changes: 57 additions & 0 deletions src/app/receipts/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import db from '@/db';
import { authOptions } from '@/lib/auth';
import { getServerSession } from 'next-auth';
import Image from 'next/image';
import { redirect } from 'next/navigation';

export default async function Receipts() {
const session = await getServerSession(authOptions);
if (!session?.user) {
redirect('/signin');
}

const courses = await db.purchase.findMany({
where: {
purchasedById: session.user.id,
},
select: {
receiptId: true,
purchasedCourse: true,
},
});

return (
<>
<main className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 place-items-center w-full pt-5 gap-5">
{courses.length === 0 ? (
<div>No Receipts found</div>
) : (
courses.map((course) => (
<Card className="w-500" key={course.purchasedCourse.id}>
<CardContent className="flex justify-center">
<Image src={'/harkirat.png'} alt="" width={200} height={100} />
</CardContent>
<CardHeader>
<CardTitle>{course.purchasedCourse.title}</CardTitle>
<CardDescription>
{course.purchasedCourse.description}
</CardDescription>
</CardHeader>
<CardFooter className="flex justify-between">
Receipt Id: {course.receiptId}
</CardFooter>
</Card>
))
)}
</main>
</>
);
}
20 changes: 11 additions & 9 deletions src/components/Appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ export const Appbar = () => {
<Button size={'sm'} variant={'link'} asChild>
<Link href={'/history'}>Watch History</Link>
</Button>
<Button size={'sm'} variant={'link'} asChild>
<Link href={'/receipts'}>Receipts</Link>
</Button>
<Button
size={'sm'}
variant={'link'}
asChild
onClick={() => router.push('/new-courses')}
>
<button>Buy Course</button>
</Button>
</div>

<Button
size={'sm'}
variant={'link'}
asChild
onClick={() => router.push('/new-courses')}
>
<button>Buy Course</button>
</Button>

<AppbarAuth />
</div>

Expand Down

0 comments on commit 2c47eb0

Please sign in to comment.