Fix sale countdown timer to properly update when sale dates change

- Fix SaleCountdown useEffect dependency array to include saleEndDate
- Remove console logs and debug output from page.tsx
- Clean up date parsing logic and remove unnecessary error handling
- Ensure countdown recalculates when sale end date changes
- Fix TypeScript type comparison for sale_active boolean check
This commit is contained in:
DaX
2025-07-11 13:32:27 +02:00
parent 34e9885a29
commit 18a4cd1e34
4 changed files with 22 additions and 20 deletions

View File

@@ -32,21 +32,7 @@ export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5, saleEndDat
if (saleEndDate) {
// Use the sale end date from admin
try {
const saleDate = new Date(saleEndDate);
// Always set to end of day for proper countdown display
targetDate = new Date(saleDate.getFullYear(), saleDate.getMonth(), saleDate.getDate(), 23, 59, 59, 999);
} catch (error) {
console.error('Error parsing sale end date:', error);
// Fallback to next Sunday if date parsing fails
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;
}
targetDate = new Date(saleEndDate);
} else {
// Fallback to next Sunday at 23:59:59
const nextSunday = new Date();
@@ -80,7 +66,7 @@ export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5, saleEndDat
setTimeLeft(calculateTimeLeft());
return () => clearInterval(timer);
}, [mounted]);
}, [mounted, saleEndDate]);
if (!mounted) {
return <div className="h-24" />; // Placeholder to prevent hydration mismatch

View File

@@ -24,6 +24,20 @@ export function SaleManager({ filaments, selectedFilaments, onSaleUpdate }: Sale
setLoading(true);
try {
// First, clear all existing sales
try {
await filamentService.updateBulkSale({
filamentIds: undefined, // Apply to all
salePercentage: 0,
saleStartDate: undefined,
saleEndDate: undefined,
enableSale: false
});
} catch (error: any) {
console.warn('Failed to clear existing sales:', error);
}
// Then apply the new sale to selected filaments
const filamentIds = Array.from(selectedFilaments);
try {