Skip to content

Commit

Permalink
Add ongoing contest countdown timers.
Browse files Browse the repository at this point in the history
  • Loading branch information
davepeck committed Apr 30, 2024
1 parent d6ed6a0 commit 622bcc8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
45 changes: 44 additions & 1 deletion server/vb/templates/components/ongoing_contest.dhtml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
font-weight: 600;
line-height: 100%;
letter-spacing: 4%;
min-width: 30%;
padding: 0.25rem;
text-transform: uppercase;
}
Expand Down Expand Up @@ -92,5 +93,47 @@
{% include "components/button.dhtml" with text="Visit event" href=contest.school.relative_url bg_color="black" color="white" %}
</div>
</div>
<div class="box">Ends in 04:12:45</div>
<div class="box" data-end-at="{{ contest.end_at|date:'c' }}">
<script>
(function(self) {
function countdown(self) {
// compute the deadline
const deadline = new Date(self.dataset.endAt);
const deadlineTime = deadline.getTime();

/** Update the countdown. */
function updateCountdown() {
const now = new Date().getTime();
const diff = deadlineTime - now;

if (diff <= 0) {
clearInterval(interval);
self.innerText = "Just ended!";
return;
}

const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);

const h0digit = Math.floor(hours / 10);
const h1digit = hours % 10;
const m0digit = Math.floor(minutes / 10);
const m1digit = minutes % 10;
const s0digit = Math.floor(seconds / 10);
const s1digit = seconds % 10;

const endsIn = `Ends in ${h0digit}${h1digit}:${m0digit}${m1digit}:${s0digit}${s1digit}`;
self.innerText = endsIn;
}

updateCountdown();
const interval = setInterval(updateCountdown, 1000);
}

onloadAdd(() => countdown(self));
})(me());
</script>
Ends in ...
</div>
</div>
4 changes: 2 additions & 2 deletions server/vb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
@require_GET
def home(request: HttpRequest) -> HttpResponse:
"""Render the voterbowl homepage."""
ongoing_contests = list(Contest.objects.ongoing())
upcoming_contests = list(Contest.objects.upcoming())
ongoing_contests = list(Contest.objects.ongoing().order_by("end_at"))
upcoming_contests = list(Contest.objects.upcoming().order_by("start_at"))
return render(
request,
"home.dhtml",
Expand Down

0 comments on commit 622bcc8

Please sign in to comment.