mirror of
https://github.com/EdiFarcas/Giveaway-app.git
synced 2026-06-28 17:00:45 +03:00
Initial Entry logic
This commit is contained in:
+35
-16
@@ -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 (
|
return (
|
||||||
<div className="min-h-screen bg-gradient-to-br from-yellow-50 to-pink-100 py-12 px-4 sm:px-6 lg:px-8 font-sans">
|
<div className="min-h-screen bg-gradient-to-br from-yellow-50 to-pink-100 py-12 px-4 sm:px-6 lg:px-8 font-sans">
|
||||||
<div className="max-w-7xl mx-auto">
|
<div className="max-w-7xl mx-auto">
|
||||||
@@ -33,41 +44,49 @@ export default async function GiveawaysPage() {
|
|||||||
|
|
||||||
{/* Grid of Cards */}
|
{/* Grid of Cards */}
|
||||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8 text-black">
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8 text-black">
|
||||||
{giveaways.map((giveaway) => {
|
{giveaways
|
||||||
const remainingTime = Math.max(
|
.filter((giveaway) => {
|
||||||
|
const remainingTime =
|
||||||
|
new Date(giveaway.endsAt ?? 0).getTime() - Date.now();
|
||||||
|
return remainingTime > 0;
|
||||||
|
})
|
||||||
|
.map((giveaway) => {
|
||||||
|
const remainingTime = Math.max(
|
||||||
0,
|
0,
|
||||||
new Date(giveaway.endsAt ?? 0).getTime() - Date.now()
|
new Date(giveaway.endsAt ?? 0).getTime() - Date.now()
|
||||||
);
|
);
|
||||||
|
|
||||||
const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
|
const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
|
||||||
const hours = Math.floor(
|
const hours = Math.floor(
|
||||||
(remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
|
(remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
|
||||||
);
|
);
|
||||||
const minutes = Math.floor(
|
const minutes = Math.floor(
|
||||||
(remainingTime % (1000 * 60 * 60)) / (1000 * 60)
|
(remainingTime % (1000 * 60 * 60)) / (1000 * 60)
|
||||||
);
|
);
|
||||||
|
|
||||||
const countdownText =
|
const countdownText =
|
||||||
days > 0
|
days > 0
|
||||||
? `${days}d ${hours}h ${minutes}m left`
|
? `${days}d ${hours}h ${minutes}m left`
|
||||||
: hours > 0
|
: hours > 0
|
||||||
? `${hours}h ${minutes}m left`
|
? `${hours}h ${minutes}m left`
|
||||||
: `${minutes}m left`;
|
: `${minutes}m left`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={giveaway.id}
|
key={giveaway.id}
|
||||||
className="bg-white rounded-2xl shadow-lg border-2 border-pink-200 hover:shadow-xl transition-shadow duration-300"
|
className="bg-white rounded-2xl shadow-lg border-2 border-pink-200 hover:shadow-xl transition-shadow duration-300"
|
||||||
>
|
>
|
||||||
<GiveawayCard
|
<GiveawayCard
|
||||||
title={giveaway.title}
|
id={giveaway.id}
|
||||||
description={`${giveaway.description} — 🕒 ${countdownText}`}
|
title={giveaway.title}
|
||||||
imageUrl={giveaway.prize}
|
description={`${giveaway.description} — 🕒 ${countdownText}`}
|
||||||
value={giveaway.value}
|
imageUrl={giveaway.prize}
|
||||||
|
value={giveaway.value}
|
||||||
|
userId={user?.id ?? ""}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
|
import handleEnterGiveawayClick from './GiveawayCardServer';
|
||||||
|
|
||||||
interface GiveawayCardProps {
|
interface GiveawayCardProps {
|
||||||
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
value: number;
|
value: number;
|
||||||
|
userId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const GiveawayCard: React.FC<GiveawayCardProps> = ({ title, description, imageUrl, value }) => {
|
const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, imageUrl, value, userId}) => {
|
||||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
|
|
||||||
const handleEnterClick = () => {
|
const handleEnterClick = () => {
|
||||||
@@ -58,7 +61,16 @@ const GiveawayCard: React.FC<GiveawayCardProps> = ({ title, description, imageUr
|
|||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
|
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
|
||||||
onClick={handleEnterClick}
|
onClick={() => {
|
||||||
|
handleEnterGiveawayClick(id, Math.ceil(value * 0.01), userId)
|
||||||
|
.then(() => {
|
||||||
|
alert(`You have entered the giveaway with ID: ${id} and value: ${Math.ceil(value * 0.01)}`);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error("Error entering giveaway:", error);
|
||||||
|
alert("Failed to enter the giveaway. Please try again.");
|
||||||
|
});
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
Enter: {Math.ceil(value * 0.01)}
|
Enter: {Math.ceil(value * 0.01)}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -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;
|
||||||
Reference in New Issue
Block a user