From b75849e285dd0c66ab2848bab89d353f4a94c24a Mon Sep 17 00:00:00 2001 From: DaX Date: Fri, 11 Jul 2025 11:50:27 +0200 Subject: [PATCH] Use admin-set sale end dates in countdown banner - Countdown now uses actual sale_end_date from database instead of hardcoded Sunday - Shows time until earliest sale end date when multiple sales are active - Falls back to next Sunday if no end dates are set - Banner now reflects real deadlines set in admin panel --- app/page.tsx | 9 +++++++++ src/components/SaleCountdown.tsx | 27 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index a5595a3..5e0b024 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -178,6 +178,15 @@ export default function Home() { f.sale_active === true)} maxSalePercentage={Math.max(...filaments.filter(f => f.sale_active).map(f => f.sale_percentage || 0), 0)} + saleEndDate={(() => { + const activeSales = filaments.filter(f => f.sale_active && f.sale_end_date); + if (activeSales.length === 0) return null; + return activeSales.reduce((earliest, current) => { + if (!earliest.sale_end_date) return current; + if (!current.sale_end_date) return earliest; + return new Date(current.sale_end_date) < new Date(earliest.sale_end_date) ? current : earliest; + }).sale_end_date; + })()} /> ({ days: 0, hours: 0, minutes: 0, seconds: 0 }); const [mounted, setMounted] = useState(false); @@ -26,16 +27,24 @@ export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5 }: SaleCoun if (!mounted) return; const calculateTimeLeft = (): TimeLeft => { - // Get next Sunday at 23:59:59 const now = new Date(); - const nextSunday = new Date(); - const daysUntilSunday = (7 - now.getDay()) % 7; - const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday; // If today is Sunday, target next Sunday - - nextSunday.setDate(now.getDate() + targetDay); - nextSunday.setHours(23, 59, 59, 999); + let targetDate: Date; - const difference = nextSunday.getTime() - now.getTime(); + if (saleEndDate) { + // Use the sale end date from admin + targetDate = new Date(saleEndDate); + } else { + // Fallback to next Sunday at 23:59:59 + const nextSunday = new Date(); + const daysUntilSunday = (7 - now.getDay()) % 7; + const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday; + + nextSunday.setDate(now.getDate() + targetDay); + nextSunday.setHours(23, 59, 59, 999); + targetDate = nextSunday; + } + + const difference = targetDate.getTime() - now.getTime(); if (difference > 0) { return {