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
giveaway Giveaway @relation(fields: [giveawayId], references: [id])
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"
>
<GiveawayCard
id={giveaway.id}
giveawayId={giveaway.id}
title={giveaway.title}
description={`${giveaway.description} — 🕒 ${countdownText}`}
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"
import React, { useState } from 'react';
import handleEnterGiveawayClick from './GiveawayCardServer';
import entrycount from './EntryCount';
import React from 'react';
import { handleEnterGiveawayClick } from './GiveawayCardServer';
import { entrycount, usercoins } from './GiveawayCardServer';
import { useEffect, useState } from 'react';
interface GiveawayCardProps {
id: string;
giveawayId: string;
title: string;
description: string;
imageUrl: string;
@@ -12,13 +13,42 @@ interface GiveawayCardProps {
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 [entrynumber, setEntrynumber] = useState<number>(0);
const [activeUserCoins, setActiveUserCoins] = useState<number>(0);
const entrienumber = entrycount(userId, id);
const handleEnterClick = () => {
const activeUserCoins = 300; // Replace with actual logic to fetch active user's coins (TO DO LATER)
const fetchEntryCount = async () => {
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) {
setIsModalOpen(true);
} else {
@@ -37,7 +67,7 @@ const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, ima
<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 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 className="p-4 flex items-center">
<button
@@ -66,9 +96,9 @@ const GiveawayCard: React.FC<GiveawayCardProps> = ({ id, title, description, ima
<button
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
onClick={() => {
handleEnterGiveawayClick(id, Math.ceil(value * 0.01), userId)
handleEnterGiveawayClick(giveawayId, Math.ceil(value * 0.01), activeUserCoins, userId)
.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) => {
console.error("Error entering giveaway:", error);
+41 -3
View File
@@ -1,13 +1,13 @@
"use server"
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({
data: {
userId: userId,
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;
}