File size: 8,069 Bytes
627a319 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | // Amazon Deals Page Functionality
document.addEventListener('DOMContentLoaded', function() {
// Simulate loading deals from Amazon API
loadDeals();
// Countdown timer for deal refresh
startCountdown();
// Initialize tooltips
feather.replace();
});
function loadDeals() {
// In a real implementation, this would fetch from an API endpoint
// that pulls Amazon deals data with our affiliate tag
const dealsContainer = document.getElementById('deals-container');
dealsContainer.innerHTML = ''; // Clear existing
// Sample data - in real app this would come from API
const sampleDeals = [
{
id: 'B0BYSWG9RH',
title: 'Wireless Noise Cancelling Headphones',
image: 'http://static.photos/technology/640x360/1',
price: 89.99,
originalPrice: 149.99,
discount: 40,
rating: 4.5,
reviewCount: 1280,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
},
{
id: 'B08L5V9RV7',
title: 'Smart Watch with Fitness Tracker',
image: 'http://static.photos/technology/640x360/2',
price: 129.99,
originalPrice: 199.99,
discount: 35,
rating: 4.2,
reviewCount: 876,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
},
{
id: 'B07VGRJDFY',
title: '4K Smart TV 55"',
image: 'http://static.photos/technology/640x360/3',
price: 399.99,
originalPrice: 599.99,
discount: 33,
rating: 4.7,
reviewCount: 2543,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
},
{
id: 'B08N5KWB9H',
title: 'Robot Vacuum Cleaner',
image: 'http://static.photos/technology/640x360/4',
price: 199.99,
originalPrice: 349.99,
discount: 43,
rating: 4.3,
reviewCount: 1120,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
},
{
id: 'B08L5V9RV7',
title: 'Air Fryer 5.8QT',
image: 'http://static.photos/home/640x360/5',
price: 79.99,
originalPrice: 129.99,
discount: 38,
rating: 4.6,
reviewCount: 4321,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
},
{
id: 'B08N5KWB9H',
title: 'Bluetooth Speaker Waterproof',
image: 'http://static.photos/technology/640x360/6',
price: 39.99,
originalPrice: 69.99,
discount: 43,
rating: 4.4,
reviewCount: 876,
isPrime: true,
shipping: 'FREE delivery',
dealEnds: '2023-12-31T23:59:59'
}
];
// Render deals
dealsContainer.innerHTML = sampleDeals.map(deal => `
<div class="deal-card bg-white rounded-lg shadow-sm overflow-hidden hover:shadow-md transition-shadow border border-gray-200" data-product-id="${deal.id}">
<div class="relative">
<a href="/product/${deal.id}?StoreID=tinder0001-20">
<img src="${deal.image}" alt="${deal.title}" class="w-full h-48 object-contain p-4">
</a>
<div class="absolute top-2 left-2 bg-red-600 text-white text-xs font-bold px-2 py-1 rounded">
${deal.discount}% off
</div>
${deal.isPrime ?
'<div class="absolute top-2 right-2"><span class="bg-amazon text-black text-xs font-bold px-2 py-1 rounded">Prime</span></div>'
: ''}
<div class="absolute bottom-2 right-2">
<button class="w-10 h-10 bg-white rounded-full shadow flex items-center justify-center hover:bg-gray-100">
<i data-feather="heart" class="w-4 h-4 text-gray-600"></i>
</button>
</div>
</div>
<div class="p-4">
<a href="/product/${deal.id}?StoreID=tinder0001-20" class="block">
<h3 class="text-sm font-medium text-gray-900 mb-1 line-clamp-2">${deal.title}</h3>
<div class="flex items-center mb-1">
<div class="flex text-yellow-400">
${Array(Math.floor(deal.rating)).fill().map(() =>
'<i data-feather="star" class="w-3 h-3 fill-current"></i>'
).join('')}
${deal.rating % 1 >= 0.5 ?
'<i data-feather="star" class="w-3 h-3 fill-current"></i>' : ''}
</div>
<span class="text-xs text-gray-600 ml-1">(${deal.reviewCount})</span>
</div>
<div class="flex items-end mb-1">
<span class="text-lg font-bold text-primary">$${deal.price.toFixed(2)}</span>
<span class="text-xs text-gray-500 line-through ml-2">$${deal.originalPrice.toFixed(2)}</span>
</div>
<div class="text-xs text-green-600">${deal.shipping}</div>
</a>
<button class="mt-3 w-full bg-primary hover:bg-primary-dark text-white text-sm font-medium py-2 px-4 rounded">
Add to Cart
</button>
</div>
</div>
`).join('');
// Initialize tracking for new elements
initTracking();
}
function startCountdown() {
let seconds = 60 * 60; // 1 hour countdown
const countdownElement = document.getElementById('countdown');
const timer = setInterval(() => {
seconds--;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
countdownElement.textContent = `${minutes}:${remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds}`;
if (seconds <= 0) {
clearInterval(timer);
loadDeals(); // Refresh deals
startCountdown(); // Restart timer
}
}, 1000);
}
// In a real implementation, this would be an API call to get fresh deals
function refreshDeals() {
console.log('Refreshing deals from Amazon API...');
// This would make a fetch request to your backend API
// which would then make requests to Amazon's API with your affiliate tag
// and return the data formatted for your frontend
// Example of what the API call might look like:
/*
fetch('/api/amazon-deals?StoreID=tinder0001-20')
.then(response => response.json())
.then(data => {
renderDeals(data);
})
.catch(error => {
console.error('Error fetching deals:', error);
});
*/
}
// Initialize tracking for all links with StoreID
function initTracking() {
document.querySelectorAll('a[href*="StoreID=tinder0001-20"]').forEach(link => {
link.addEventListener('click', trackClick);
});
}
function trackClick(e) {
// In a real implementation, this would send to your analytics
const productId = this.closest('[data-product-id]')?.getAttribute('data-product-id');
const url = this.href;
console.log(`Tracking click: ${url} for product ${productId}`);
// Example tracking payload
const trackingData = {
storeId: 'tinder0001-20',
productId: productId || null,
url: url,
timestamp: new Date().toISOString(),
eventType: 'deal_click'
};
// In a real app, this would be sent to your backend
localStorage.setItem('lastTracking', JSON.stringify(trackingData));
} |