summaryrefslogtreecommitdiff
path: root/app/javascript/controllers/timer_controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/javascript/controllers/timer_controller.js')
-rw-r--r--app/javascript/controllers/timer_controller.js32
1 files changed, 14 insertions, 18 deletions
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);
}
}