| 1 |
/** |
| 2 |
* Downloads List JavaScript |
| 3 |
* Handles download functionality for the list view with visibility checks |
| 4 |
*/ |
| 5 |
|
| 6 |
document.addEventListener('DOMContentLoaded', function() { |
| 7 |
// Use centralized API helper |
| 8 |
if (typeof window.YatraApiHelper === 'undefined') { |
| 9 |
console.error('YatraApiHelper not loaded'); |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
// Handle download button clicks |
| 14 |
const downloadButtons = document.querySelectorAll('.yatra-download-btn:not(.yatra-download-btn-disabled)'); |
| 15 |
|
| 16 |
downloadButtons.forEach(button => { |
| 17 |
button.addEventListener('click', function(e) { |
| 18 |
e.preventDefault(); |
| 19 |
|
| 20 |
const downloadId = this.getAttribute('data-download-id'); |
| 21 |
const visibility = this.getAttribute('data-visibility'); |
| 22 |
|
| 23 |
if (!downloadId) { |
| 24 |
console.error('No download ID found'); |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// Show loading state |
| 29 |
const originalContent = this.innerHTML; |
| 30 |
this.innerHTML = '<span class="yatra-download-spinner"></span> Getting download...'; |
| 31 |
this.disabled = true; |
| 32 |
|
| 33 |
// For booked_only downloads, we need to get booking ID |
| 34 |
// In a real implementation, you'd get this from user's bookings |
| 35 |
if (visibility === 'booked_only') { |
| 36 |
// For demo purposes, we'll try without booking ID first |
| 37 |
// The API will tell us if booking is required |
| 38 |
} |
| 39 |
|
| 40 |
// Make API request to get download URL using centralized helper |
| 41 |
window.YatraApiHelper.getDownloadUrl(downloadId) |
| 42 |
.then(response => { |
| 43 |
if (!response.ok) { |
| 44 |
return response.json().then(err => { |
| 45 |
throw new Error(err.message || 'Download failed'); |
| 46 |
}); |
| 47 |
} |
| 48 |
return response.json(); |
| 49 |
}) |
| 50 |
.then(data => { |
| 51 |
if (data.download_url) { |
| 52 |
// Create temporary link and trigger download |
| 53 |
const link = document.createElement('a'); |
| 54 |
link.href = data.download_url; |
| 55 |
link.download = data.filename || 'download'; |
| 56 |
link.style.display = 'none'; |
| 57 |
document.body.appendChild(link); |
| 58 |
link.click(); |
| 59 |
document.body.removeChild(link); |
| 60 |
} else { |
| 61 |
throw new Error('No download URL provided'); |
| 62 |
} |
| 63 |
}) |
| 64 |
.catch(error => { |
| 65 |
console.error('Download error:', error); |
| 66 |
|
| 67 |
// Show user-friendly error message |
| 68 |
let errorMessage = 'Download failed. Please try again.'; |
| 69 |
if (error.message.includes('booking')) { |
| 70 |
errorMessage = 'This download requires a booking. Please book this trip first.'; |
| 71 |
} else if (error.message.includes('login')) { |
| 72 |
errorMessage = 'Please log in to download this file.'; |
| 73 |
} else if (error.message.includes('permission')) { |
| 74 |
errorMessage = 'You do not have permission to download this file.'; |
| 75 |
} |
| 76 |
|
| 77 |
alert(errorMessage); |
| 78 |
}) |
| 79 |
.finally(() => { |
| 80 |
// Restore button state |
| 81 |
this.innerHTML = originalContent; |
| 82 |
this.disabled = false; |
| 83 |
}); |
| 84 |
}); |
| 85 |
}); |
| 86 |
|
| 87 |
// Handle disabled button clicks - show appropriate message |
| 88 |
const disabledButtons = document.querySelectorAll('.yatra-download-btn-disabled'); |
| 89 |
|
| 90 |
disabledButtons.forEach(button => { |
| 91 |
button.addEventListener('click', function(e) { |
| 92 |
e.preventDefault(); |
| 93 |
|
| 94 |
const visibility = this.getAttribute('data-visibility'); |
| 95 |
let message = 'Download not available.'; |
| 96 |
|
| 97 |
if (visibility === 'logged_in') { |
| 98 |
message = 'Please log in to download this file.'; |
| 99 |
} else if (visibility === 'booked_only') { |
| 100 |
message = 'This download requires a booking for this trip.'; |
| 101 |
} |
| 102 |
|
| 103 |
alert(message); |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
// Add CSS for spinner if not already present |
| 108 |
if (!document.querySelector('#yatra-downloads-spinner-styles')) { |
| 109 |
const style = document.createElement('style'); |
| 110 |
style.id = 'yatra-downloads-spinner-styles'; |
| 111 |
style.textContent = ` |
| 112 |
.yatra-download-spinner { |
| 113 |
display: inline-block; |
| 114 |
width: 16px; |
| 115 |
height: 16px; |
| 116 |
border: 2px solid rgba(255, 255, 255, 0.3); |
| 117 |
border-top: 2px solid white; |
| 118 |
border-radius: 50%; |
| 119 |
animation: yatra-spin 1s linear infinite; |
| 120 |
} |
| 121 |
|
| 122 |
@keyframes yatra-spin { |
| 123 |
0% { transform: rotate(0deg); } |
| 124 |
100% { transform: rotate(360deg); } |
| 125 |
} |
| 126 |
`; |
| 127 |
document.head.appendChild(style); |
| 128 |
} |
| 129 |
}); |
| 130 |
|