Add voting deadline function

This commit is contained in:
2025-10-13 22:30:15 +08:00
parent afd5ecd89a
commit 6a50c9fe90
35 changed files with 3489 additions and 103 deletions
+79
View File
@@ -32,6 +32,19 @@
</div>
</div>
</div>
<!-- Voting Deadline Countdown -->
{% if votingDeadline %}
<div class="mt-4">
<div id="countdown-container" class="alert alert-info">
<h5 class="mb-2"><i class="fas fa-clock me-2"></i>Voting Closes In:</h5>
<div id="countdown-timer" class="display-6 fw-bold"></div>
</div>
<div id="voting-closed" class="alert alert-danger" style="display: none;">
<h5 class="mb-0"><i class="fas fa-lock me-2"></i>Voting has closed</h5>
</div>
</div>
{% endif %}
</div>
</div>
</div>
@@ -178,11 +191,77 @@
{% block extra_scripts %}
<script>
$(document).ready(function() {
{% if votingDeadline %}
// Countdown timer functionality
const votingDeadline = new Date('{{ votingDeadline }}');
const countdownTimer = $('#countdown-timer');
const countdownContainer = $('#countdown-container');
const votingClosedDiv = $('#voting-closed');
const votingForm = $('#motmForm');
const submitButton = votingForm.find('button[type="submit"]');
function updateCountdown() {
const now = new Date();
const timeRemaining = votingDeadline - now;
if (timeRemaining <= 0) {
// Voting has closed
countdownContainer.hide();
votingClosedDiv.show();
votingForm.find('select, textarea, button').prop('disabled', true);
submitButton.html('<i class="fas fa-lock me-2"></i>Voting Closed');
return;
}
// Calculate time components
const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
// Format and display countdown
let countdownText = '';
if (days > 0) {
countdownText = `${days}d ${hours}h ${minutes}m ${seconds}s`;
} else if (hours > 0) {
countdownText = `${hours}h ${minutes}m ${seconds}s`;
} else if (minutes > 0) {
countdownText = `${minutes}m ${seconds}s`;
} else {
countdownText = `${seconds}s`;
}
countdownTimer.text(countdownText);
// Update alert color based on time remaining
if (timeRemaining < 60000) { // Less than 1 minute
countdownContainer.removeClass('alert-info alert-warning').addClass('alert-danger');
} else if (timeRemaining < 300000) { // Less than 5 minutes
countdownContainer.removeClass('alert-info alert-danger').addClass('alert-warning');
}
}
// Update countdown every second
updateCountdown();
setInterval(updateCountdown, 1000);
{% endif %}
// Form validation
$('#motmForm').on('submit', function(e) {
const motmSelect = $('#motmSelect');
const dotdSelect = $('#dotdSelect');
{% if votingDeadline %}
// Check if voting is still open
const now = new Date();
const votingDeadline = new Date('{{ votingDeadline }}');
if (now >= votingDeadline) {
e.preventDefault();
alert('Sorry, voting has closed.');
return false;
}
{% endif %}
if (!motmSelect.val()) {
e.preventDefault();
alert('Please select a player for Man of the Match.');