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
This commit is contained in:
@@ -178,6 +178,15 @@ export default function Home() {
|
|||||||
<SaleCountdown
|
<SaleCountdown
|
||||||
hasActiveSale={filaments.some(f => f.sale_active === true)}
|
hasActiveSale={filaments.some(f => f.sale_active === true)}
|
||||||
maxSalePercentage={Math.max(...filaments.filter(f => f.sale_active).map(f => f.sale_percentage || 0), 0)}
|
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;
|
||||||
|
})()}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FilamentTableV2
|
<FilamentTableV2
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ interface TimeLeft {
|
|||||||
interface SaleCountdownProps {
|
interface SaleCountdownProps {
|
||||||
hasActiveSale: boolean;
|
hasActiveSale: boolean;
|
||||||
maxSalePercentage?: number;
|
maxSalePercentage?: number;
|
||||||
|
saleEndDate?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5 }: SaleCountdownProps) {
|
export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5, saleEndDate }: SaleCountdownProps) {
|
||||||
const [timeLeft, setTimeLeft] = useState<TimeLeft>({ days: 0, hours: 0, minutes: 0, seconds: 0 });
|
const [timeLeft, setTimeLeft] = useState<TimeLeft>({ days: 0, hours: 0, minutes: 0, seconds: 0 });
|
||||||
const [mounted, setMounted] = useState(false);
|
const [mounted, setMounted] = useState(false);
|
||||||
|
|
||||||
@@ -26,16 +27,24 @@ export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5 }: SaleCoun
|
|||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
|
||||||
const calculateTimeLeft = (): TimeLeft => {
|
const calculateTimeLeft = (): TimeLeft => {
|
||||||
// Get next Sunday at 23:59:59
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
let targetDate: Date;
|
||||||
|
|
||||||
|
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 nextSunday = new Date();
|
||||||
const daysUntilSunday = (7 - now.getDay()) % 7;
|
const daysUntilSunday = (7 - now.getDay()) % 7;
|
||||||
const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday; // If today is Sunday, target next Sunday
|
const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday;
|
||||||
|
|
||||||
nextSunday.setDate(now.getDate() + targetDay);
|
nextSunday.setDate(now.getDate() + targetDay);
|
||||||
nextSunday.setHours(23, 59, 59, 999);
|
nextSunday.setHours(23, 59, 59, 999);
|
||||||
|
targetDate = nextSunday;
|
||||||
|
}
|
||||||
|
|
||||||
const difference = nextSunday.getTime() - now.getTime();
|
const difference = targetDate.getTime() - now.getTime();
|
||||||
|
|
||||||
if (difference > 0) {
|
if (difference > 0) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user