From 8206cac29ec2ce86e0b0a68360ec714b573e13f6 Mon Sep 17 00:00:00 2001 From: David Gay Date: Fri, 4 Jun 2021 10:21:20 -0400 Subject: Timer progress bar --- app/javascript/controllers/timer_controller.js | 32 +++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'app/javascript/controllers') diff --git a/app/javascript/controllers/timer_controller.js b/app/javascript/controllers/timer_controller.js index e85e7ca..b13188a 100644 --- a/app/javascript/controllers/timer_controller.js +++ b/app/javascript/controllers/timer_controller.js @@ -1,47 +1,43 @@ -import { Controller } from "stimulus"; +import {Controller} from "stimulus"; import Rails from "@rails/ujs"; export default class extends Controller { - static targets = [ "timer" ]; + static targets = [ "timer", "progressBar" ]; static values = { - counter: Number, + timeRemaining: Number, + activityDuration: Number, postUrl: String, } connect() { if (!this.timerInterval) this.startUpdating(); - this.timerTarget.textContent = this.counterValue; - this.postFailures = 0; + this.timerTarget.textContent = Math.ceil(this.timeRemainingValue); } startUpdating() { let controller = this; controller.timerInterval = setInterval(() => { - if (controller.postFailures >= 5) { - alert("An error occurred. Please submit a bug report, explaining what you were doing when it occurred."); - clearInterval(controller.timerInterval); - return; - } - if (controller.counterValue > 0) controller.counterValue--; + if (controller.timeRemainingValue > 0) { + controller.timeRemainingValue = controller.timeRemainingValue - 0.01; + } - if (controller.counterValue > 0) { - controller.timerTarget.textContent = controller.counterValue.toString(); + if (controller.timeRemainingValue > 0) { + controller.timerTarget.textContent = Math.ceil(controller.timeRemainingValue).toString(); + controller.progressBarTarget.style.width = `${(1 - (controller.timeRemainingValue / controller.activityDurationValue)) * 100}%`; } else { + clearInterval(controller.timerInterval); + controller.timerInterval = null; Rails.ajax({ type: "POST", url: controller.postUrlValue, success: () => { - clearInterval(controller.timerInterval); - controller.timerInterval = null; - controller.postFailures = 0; }, error: () => { - controller.postFailures++; }, }); } - }, 1000); + }, 10); } } -- cgit v1.2.3