Entry Update 2

This commit is contained in:
EdiFarcas
2025-05-05 21:42:29 +03:00
parent bcc11ddbc1
commit ad25daea04
6 changed files with 95 additions and 29 deletions
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `weight` on the `Entry` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "Entry" DROP COLUMN "weight",
ADD COLUMN "acive" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN "pastValue" INTEGER NOT NULL DEFAULT 0;
+2 -1
View File
@@ -78,5 +78,6 @@ model Entry {
userId String userId String
giveaway Giveaway @relation(fields: [giveawayId], references: [id]) giveaway Giveaway @relation(fields: [giveawayId], references: [id])
giveawayId String giveawayId String
weight Float acive Boolean @default(true)
pastValue Int @default(0)
} }
+1 -1
View File
@@ -77,7 +77,7 @@ export default async function GiveawaysPage() {
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
id={giveaway.id} giveawayId={giveaway.id}
title={giveaway.title} title={giveaway.title}
description={`${giveaway.description} — 🕒 ${countdownText}`} description={`${giveaway.description} — 🕒 ${countdownText}`}
imageUrl={giveaway.prize} imageUrl={giveaway.prize}
-13
View File
@@ -1,13 +0,0 @@
import { db } from "@/lib/db";
async function entrycount(userId: string, giveawayId: string) {
const entryCount = await db.entry.count({
where: {
userId: userId,
giveawayId: giveawayId,
},
});
return entryCount;
}
export default entrycount;
+41 -11
View File
@@ -1,10 +1,11 @@
"use client" "use client"
import React, { useState } from 'react'; import React from 'react';
import handleEnterGiveawayClick from './GiveawayCardServer'; import { handleEnterGiveawayClick } from './GiveawayCardServer';
import entrycount from './EntryCount'; import { entrycount, usercoins } from './GiveawayCardServer';
import { useEffect, useState } from 'react';
interface GiveawayCardProps { interface GiveawayCardProps {
id: string; giveawayId: string;
title: string; title: string;
description: string; description: string;
imageUrl: string; imageUrl: string;
@@ -12,13 +13,42 @@ interface GiveawayCardProps {
userId: string; userId: string;
} }
const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, imageUrl, value, userId}) => { const GiveawayCard: React.FC<GiveawayCardProps> = ({ giveawayId, title, description, imageUrl, value, userId}) => {
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const [entrynumber, setEntrynumber] = useState<number>(0);
const [activeUserCoins, setActiveUserCoins] = useState<number>(0);
const entrienumber = entrycount(userId, id);
const handleEnterClick = () => { const fetchEntryCount = async () => {
const activeUserCoins = 300; // Replace with actual logic to fetch active user's coins (TO DO LATER) try {
const count = await entrycount(userId, giveawayId);
setEntrynumber(count);
} catch (error) {
console.error("Error fetching entry count:", error);
}
};
useEffect(() => {
fetchEntryCount();
}, [userId, giveawayId]);
const fetchActiveUserCoins = async () => {
try {
const coins = await usercoins(userId, giveawayId);
setActiveUserCoins(coins);
} catch (error) {
console.error("Error fetching active user coins:", error);
}
};
useEffect(() => {
fetchActiveUserCoins();
}, [userId, giveawayId]);
const handleEnterClick = async () => {
await fetchActiveUserCoins();
if (activeUserCoins >= value) { if (activeUserCoins >= value) {
setIsModalOpen(true); setIsModalOpen(true);
} else { } else {
@@ -37,7 +67,7 @@ const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, ima
<h3 className="text-lg font-bold mb-2">{title}</h3> <h3 className="text-lg font-bold mb-2">{title}</h3>
<p className="text-gray-600 text-sm">{description}</p> <p className="text-gray-600 text-sm">{description}</p>
<p className="text-gray-600 text-sm mt-2">Value: {value} coins</p> <p className="text-gray-600 text-sm mt-2">Value: {value} coins</p>
{/* <p className="text-gray-600 text-sm mt-2">Your entries: {entrycount(userId, id)}</p> */} <p className="text-gray-600 text-sm mt-2">Your entries: {entrynumber}</p>
</div> </div>
<div className="p-4 flex items-center"> <div className="p-4 flex items-center">
<button <button
@@ -66,9 +96,9 @@ const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, ima
<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={() => { onClick={() => {
handleEnterGiveawayClick(id, Math.ceil(value * 0.01), userId) handleEnterGiveawayClick(giveawayId, Math.ceil(value * 0.01), activeUserCoins, userId)
.then(() => { .then(() => {
alert(`You have entered the giveaway with ID: ${id} and value: ${Math.ceil(value * 0.01)}`); alert(`You have entered the giveaway with ID: ${giveawayId} and value: ${Math.ceil(value * 0.01)}`);
}) })
.catch((error) => { .catch((error) => {
console.error("Error entering giveaway:", error); console.error("Error entering giveaway:", error);
+41 -3
View File
@@ -1,13 +1,13 @@
"use server" "use server"
import { db } from "@/lib/db"; import { db } from "@/lib/db";
async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, userId: string) { export async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, activeUserCoins: number, userId: string) {
await db.entry.create({ await db.entry.create({
data: { data: {
userId: userId, userId: userId,
giveawayId: giveawayId, giveawayId: giveawayId,
weight: 1, pastValue: activeUserCoins,
}, },
}); });
@@ -19,4 +19,42 @@ async function handleEnterGiveawayClick(giveawayId: string, entryValue: number,
}); });
} }
export default handleEnterGiveawayClick; export async function entrycount(userId: string, giveawayId: string) {
const entryCount = db.entry.count({
where: {
userId: userId,
giveawayId: giveawayId,
},
});
return entryCount;
}
export async function usercoins(userId: string, giveawayId: string) {
const user = await db.user.findUnique({
where: { id: userId },
select: { coins: true },
});
const entry = await db.entry.findFirst({
where: {
userId: userId,
giveawayId: giveawayId,
},
orderBy: {
pastValue: 'desc',
},
select: { pastValue: true },
});
if (!user) {
throw new Error("User not found");
}
if (entry) {
if ((entry?.pastValue ?? 0) > user.coins) {
return entry.pastValue;
}
}
return user.coins;
}