diff --git a/src/app/giveaways/page.tsx b/src/app/giveaways/page.tsx
index 00439c0..914334a 100644
--- a/src/app/giveaways/page.tsx
+++ b/src/app/giveaways/page.tsx
@@ -23,6 +23,17 @@ export default async function GiveawaysPage() {
},
});
+ const user = await db.user.findUnique({
+ where: { email: session.user.email },
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ coins: true,
+ youtubeHandle: true,
+ },
+ });
+
return (
@@ -33,41 +44,49 @@ export default async function GiveawaysPage() {
{/* Grid of Cards */}
- {giveaways.map((giveaway) => {
- const remainingTime = Math.max(
+ {giveaways
+ .filter((giveaway) => {
+ const remainingTime =
+ new Date(giveaway.endsAt ?? 0).getTime() - Date.now();
+ return remainingTime > 0;
+ })
+ .map((giveaway) => {
+ const remainingTime = Math.max(
0,
new Date(giveaway.endsAt ?? 0).getTime() - Date.now()
- );
-
- const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
- const hours = Math.floor(
+ );
+
+ const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
+ const hours = Math.floor(
(remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
- );
- const minutes = Math.floor(
+ );
+ const minutes = Math.floor(
(remainingTime % (1000 * 60 * 60)) / (1000 * 60)
- );
-
- const countdownText =
+ );
+
+ const countdownText =
days > 0
? `${days}d ${hours}h ${minutes}m left`
: hours > 0
? `${hours}h ${minutes}m left`
: `${minutes}m left`;
-
- return (
+
+ return (
- );
- })}
+ );
+ })}
diff --git a/src/components/GiveawayCard.tsx b/src/components/GiveawayCard.tsx
index 02744db..43976b0 100644
--- a/src/components/GiveawayCard.tsx
+++ b/src/components/GiveawayCard.tsx
@@ -1,14 +1,17 @@
"use client"
import React, { useState } from 'react';
+import handleEnterGiveawayClick from './GiveawayCardServer';
interface GiveawayCardProps {
+ id: string;
title: string;
description: string;
imageUrl: string;
value: number;
+ userId: string;
}
-const GiveawayCard: React.FC = ({ title, description, imageUrl, value }) => {
+const GiveawayCard: React.FC = ({ id, title, description, imageUrl, value, userId}) => {
const [isModalOpen, setIsModalOpen] = useState(false);
const handleEnterClick = () => {
@@ -58,7 +61,16 @@ const GiveawayCard: React.FC = ({ title, description, imageUr
diff --git a/src/components/GiveawayCardServer.tsx b/src/components/GiveawayCardServer.tsx
new file mode 100644
index 0000000..5594383
--- /dev/null
+++ b/src/components/GiveawayCardServer.tsx
@@ -0,0 +1,22 @@
+"use server"
+import { db } from "@/lib/db";
+
+async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, userId: string) {
+
+ await db.entry.create({
+ data: {
+ userId: userId,
+ giveawayId: giveawayId,
+ weight: 1,
+ },
+ });
+
+ await db.user.update({
+ where: { id: userId },
+ data: {
+ coins: { decrement: entryValue },
+ },
+ });
+}
+
+export default handleEnterGiveawayClick;
\ No newline at end of file