From dbb536e702d49706ccc23c5a925de7660cf8a5b1 Mon Sep 17 00:00:00 2001 From: singharaj usai Date: Mon, 30 Sep 2024 23:49:31 -0400 Subject: [PATCH] claiming currencies fixed --- client/js/auth/auth.js | 123 ++++++++++++++++------------ server/functions/api/routes/auth.js | 15 ++-- 2 files changed, 80 insertions(+), 58 deletions(-) diff --git a/client/js/auth/auth.js b/client/js/auth/auth.js index 2d6afdf..a4b70f5 100644 --- a/client/js/auth/auth.js +++ b/client/js/auth/auth.js @@ -218,10 +218,10 @@ const App = { const username = localStorage.getItem("username"); const token = localStorage.getItem("token"); const authContainer = $("#auth-container"); - + if (username && token) { $('#profile-link').attr('href', `/user-profile?username=${encodeURIComponent(username)}`); - + $.ajax({ url: "/api/user-info", method: "GET", @@ -230,15 +230,22 @@ const App = { authContainer.html(` Welcome, ${this.escapeHtml(username)} - - ${response.currency} - + + + ${response.currency} + `); this.updateCurrencyTooltip(response.lastCurrencyClaimDate); this.initLogout(); $('#user-submenu').show(); + + // Initialize the tooltip + $('#currency-container').tooltip({ + trigger: 'hover', + html: true + }); }, error: (xhr, status, error) => { console.error("Error fetching user info:", error); @@ -255,59 +262,73 @@ const App = { if (typeof updateAnnouncementPosition === 'function') updateAnnouncementPosition(); }, -updateCurrencyTooltip: function (lastClaimDate) { - const countdownElement = $('#currency-countdown'); - - const updateCountdown = () => { - const now = new Date(); - const lastClaim = new Date(lastClaimDate); - const nextClaimTime = new Date(lastClaim.getTime() + 24 * 60 * 60 * 1000); - let timeUntilNextClaim = nextClaimTime - now; + // Update the currency tooltip with countdown + updateCurrencyTooltip: function (lastClaimDate) { + const tooltipElement = $('#currency-container'); - if (timeUntilNextClaim < 0) { - timeUntilNextClaim = 0; - } + const updateCountdown = () => { + const now = new Date(); + const lastClaim = new Date(lastClaimDate); + const nextClaimTime = new Date(lastClaim.getTime() + 24 * 60 * 60 * 1000); + let timeUntilNextClaim = nextClaimTime - now; - const hours = Math.floor(timeUntilNextClaim / (60 * 60 * 1000)); - const minutes = Math.floor((timeUntilNextClaim % (60 * 60 * 1000)) / (60 * 1000)); - const seconds = Math.floor((timeUntilNextClaim % (60 * 1000)) / 1000); + if (timeUntilNextClaim <= 0) { + tooltipElement.attr('data-original-title', 'Currency available! Click to claim.'); + tooltipElement.tooltip('fixTitle'); + this.claimCurrency(); + clearInterval(this.currencyInterval); + return; + } - const countdownText = timeUntilNextClaim > 0 - ? `Next currency in ${hours}h ${minutes}m ${seconds}s` - : 'Currency available!'; + const hours = Math.floor(timeUntilNextClaim / (60 * 60 * 1000)); + const minutes = Math.floor((timeUntilNextClaim % (60 * 60 * 1000)) / (60 * 1000)); + const seconds = Math.floor((timeUntilNextClaim % (60 * 1000)) / 1000); - countdownElement.text(countdownText); + const tooltipText = `Next currency in ${hours}h ${minutes}m ${seconds}s`; + tooltipElement.attr('data-original-title', tooltipText); + tooltipElement.tooltip('fixTitle'); + }; - // Automatically claim currency when countdown reaches zero - if (timeUntilNextClaim <= 0) { - this.claimCurrency(); + // Clear any existing interval + if (this.currencyInterval) { clearInterval(this.currencyInterval); - countdownElement.text('Currency available!'); } - }; - - // Initial countdown update - updateCountdown(); - - // Update countdown every second - this.currencyInterval = setInterval(updateCountdown, 1000); -}, - -claimCurrency: function () { - const token = localStorage.getItem("token"); - $.ajax({ - url: "/api/claim-daily-currency", - method: "POST", - headers: { "Authorization": `Bearer ${token}` }, - success: (response) => { - $("#currency-amount").text(response.newBalance); - this.updateCurrencyTooltip(response.lastClaimDate); - }, - error: (xhr) => { - console.error("Error claiming currency:", xhr.responseText); - }, - }); -}, + + // Initial countdown update + updateCountdown(); + + // Update countdown every second + this.currencyInterval = setInterval(updateCountdown, 1000); + }, + + // Claim currency when available + claimCurrency: function () { + const token = localStorage.getItem("token"); + if (!token) { + console.error("No token found. User might not be logged in."); + return; + } + $.ajax({ + url: "/api/claim-daily-currency", + method: "POST", + headers: { "Authorization": `Bearer ${token}` }, + success: (response) => { + $("#currency-amount").text(response.newBalance); + this.updateCurrencyTooltip(response.lastClaimDate); + $('#currency-container').tooltip('show'); + }, + error: (xhr) => { + console.error("Error claiming currency:", xhr.responseText); + if (xhr.status === 400) { + // If currency was already claimed, update the tooltip with the correct time + const errorData = JSON.parse(xhr.responseText); + if (errorData.lastClaimDate) { + this.updateCurrencyTooltip(errorData.lastClaimDate); + } + } + }, + }); + }, // Handle user logout logout: function () { diff --git a/server/functions/api/routes/auth.js b/server/functions/api/routes/auth.js index 17b7049..55f9007 100644 --- a/server/functions/api/routes/auth.js +++ b/server/functions/api/routes/auth.js @@ -451,13 +451,9 @@ router.get("/user-count", async (req, res) => { } }); -router.post("/claim-daily-currency", async (req, res) => { +router.post("/claim-daily-currency", authenticateToken, async (req, res) => { try { - const userId = req.session.userId; - if (!userId) { - return res.status(401).json({ error: "Unauthorized" }); - } - + const userId = req.user.userId; const user = await User.findById(userId); if (!user) { return res.status(404).json({ error: "User not found" }); @@ -472,7 +468,12 @@ router.post("/claim-daily-currency", async (req, res) => { await user.save(); res.json({ success: true, newBalance: user.currency, lastClaimDate: user.lastCurrencyClaimDate }); } else { - res.status(400).json({ error: "Currency can only be claimed once per day" }); + const nextClaimTime = lastClaim.clone().add(24, 'hours'); + const timeUntilNextClaim = nextClaimTime.diff(now); + res.status(400).json({ + error: "Currency can only be claimed once per day", + lastClaimDate: user.lastCurrencyClaimDate + }); } } catch (error) { console.error("Error claiming daily currency:", error);