Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from rushlow-development/feature/time-tracking
add ability to run timers
- Loading branch information
Showing
24 changed files
with
1,261 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import { useDebounce } from 'stimulus-use'; | ||
import axios from 'axios'; | ||
import { DateTime, Duration } from 'luxon'; | ||
|
||
/* stimulusFetch: 'lazy */ | ||
export default class extends Controller { | ||
#intervalId; | ||
#startIcon; | ||
#stopIcon; | ||
|
||
static debounces = ['runTimer', 'startTimer', 'stopTimer'] | ||
|
||
static values = { | ||
timerId: String, | ||
timerRunning: Boolean, | ||
timerStartTime: Number, | ||
timerAccumulatedTime: Number, | ||
} | ||
|
||
static targets = [ | ||
'timerDurationCounter', | ||
'timerStartButton', | ||
'timerStopButton', | ||
] | ||
|
||
connect() { | ||
useDebounce(this); | ||
|
||
this.setButtonVisibility(); | ||
this.#startIcon = this.timerStartButtonTarget.innerHTML; | ||
this.#stopIcon = this.timerStopButtonTarget.innerHTML; | ||
|
||
if (this.timerRunningValue) { | ||
this.initTimer(); | ||
} | ||
} | ||
|
||
setButtonVisibility() { | ||
if (this.timerRunningValue) { | ||
this.timerStartButtonTarget.classList.toggle('hidden', true); | ||
this.timerStopButtonTarget.classList.toggle('hidden', false); | ||
|
||
return; | ||
} | ||
|
||
this.timerStartButtonTarget.classList.toggle('hidden', false); | ||
this.timerStopButtonTarget.classList.toggle('hidden', true); | ||
} | ||
|
||
// Called when page is first loaded to start any timers that are already running. | ||
initTimer() { | ||
let startedAt = DateTime.fromSeconds(this.timerStartTimeValue); | ||
|
||
// Negate the diff, otherwise we'll get a negative int. | ||
let diff = startedAt.diffNow(['seconds']).negate(); | ||
|
||
this.runTimer(diff); | ||
} | ||
|
||
// Called when starting a timer AFTER the initial page load | ||
runTimer(diff) { | ||
// let totalTime = Duration.fromObject({seconds: this.timerAccumulatedTimeValue}); | ||
let totalTime = diff.plus({seconds: this.timerAccumulatedTimeValue}); | ||
|
||
this.#intervalId = setInterval(() => { | ||
totalTime = totalTime.plus({seconds: 1}); | ||
|
||
this.timerDurationCounterTarget.innerHTML = totalTime.toFormat('h:mm:ss'); | ||
}, 1000); | ||
} | ||
|
||
startTimer() { | ||
this.timerStartButtonTarget.innerHTML = '~'; | ||
|
||
this.restartTimer(); | ||
|
||
let diff = Duration.fromObject({seconds: 0}) | ||
|
||
this.runTimer(diff); | ||
} | ||
|
||
stopTimer() { | ||
this.timerStopButtonTarget.innerHTML = '~'; | ||
|
||
clearInterval(this.#intervalId); | ||
|
||
this.timerRunningValue = false; | ||
|
||
this.persistTimer(); | ||
} | ||
|
||
async restartTimer() { | ||
let response = await axios | ||
.post(`/timer/start/${this.timerIdValue}`) | ||
.then((response) => { | ||
return response.data; | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
|
||
return false; | ||
}) | ||
; | ||
|
||
if (response === false) { | ||
// @todo do something | ||
this.timerStartButtonTarget.innerHTML = 'Oops'; | ||
|
||
clearInterval(this.#intervalId); | ||
|
||
return; | ||
} | ||
|
||
this.timerAccumulatedTimeValue = response.accumulatedSeconds; | ||
this.timerStartTimeValue = response.restartedAt; | ||
|
||
this.timerStartButtonTarget.classList.toggle('hidden', true); | ||
this.timerStopButtonTarget.classList.toggle('hidden', false); | ||
this.timerStartButtonTarget.innerHTML = this.#startIcon; | ||
} | ||
|
||
async persistTimer() { | ||
let response = await axios | ||
.post(`/timer/pause/${this.timerIdValue}`) | ||
.then((response) => { | ||
return response.data.accumulatedSeconds; | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
|
||
return false; | ||
}) | ||
; | ||
|
||
if (response === false) { | ||
// @todo do something | ||
this.timerStopButtonTarget.innerHTML = 'Oops'; | ||
|
||
this.runTimer(); | ||
|
||
return; | ||
} | ||
|
||
this.timerAccumulatedTimeValue = response; | ||
|
||
this.timerStartButtonTarget.classList.toggle('hidden', false); | ||
this.timerStopButtonTarget.classList.toggle('hidden', true); | ||
this.timerStopButtonTarget.innerHTML = this.#stopIcon; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import axios from 'axios'; | ||
|
||
/* stimulusFetch: 'lazy */ | ||
export default class extends Controller { | ||
static values = { | ||
// todoListId: String, | ||
} | ||
|
||
static targets = [ | ||
'timerList', | ||
] | ||
|
||
async startTimer() { | ||
let response = await axios | ||
.post('/timer/create') | ||
.then((response) => { | ||
if (response.status !== 200) { | ||
// @TODO Show an alert of something... | ||
|
||
return false; | ||
} | ||
|
||
return response.data; | ||
}) | ||
; | ||
|
||
if (response === false) { | ||
// @TODO Show an alert of something... | ||
|
||
console.log('Ooops, something went wrong....'); | ||
|
||
return; | ||
} | ||
|
||
// console.log(response.html.content); | ||
|
||
const div = document.createElement('div'); | ||
div.classList.add('row'); | ||
div.innerHTML = response.html.content; | ||
|
||
this.timerListTarget.prepend(div); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
:root { | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.