diff --git a/public/pause.svg b/public/pause.svg new file mode 100644 index 0000000..38302a0 --- /dev/null +++ b/public/pause.svg @@ -0,0 +1,2 @@ + + diff --git a/src/timer/Timer.svelte b/src/timer/Timer.svelte index d1cf945..ef9f6e0 100644 --- a/src/timer/Timer.svelte +++ b/src/timer/Timer.svelte @@ -1,10 +1,11 @@ + -{#if showReset} +
+ {#if showReset} updateSession(sessionData)}> reset the timer -
+ {:else} + {/if} + + + + > + + + + + {displayTime} + + diff --git a/src/utils/handleSession.js b/src/utils/handleSession.js index c9d5fe6..7320bd3 100644 --- a/src/utils/handleSession.js +++ b/src/utils/handleSession.js @@ -29,3 +29,24 @@ export async function newSession(duration) { }); return await response.json(); } + +export async function pauseSession(sessionID, pauseTime){ + await fetch(`${process.env.ADDR}/session/pause`, { + method: 'POST', + body: JSON.stringify( { + sessionId: sessionID, + pauseTime: pauseTime, + }), + }); +} + +export async function unpauseSession(sessionID, unpauseTime){ + console.log(sessionID) + await fetch(`${process.env.ADDR}/session/unpause`, { + method: 'POST', + body: JSON.stringify( { + sessionId: sessionID, + unpauseTime: unpauseTime, + }), + }); +} \ No newline at end of file diff --git a/src/utils/timer-utils.js b/src/utils/timer-utils.js new file mode 100644 index 0000000..0e658d0 --- /dev/null +++ b/src/utils/timer-utils.js @@ -0,0 +1,47 @@ + import { + updateSession, + } from './handleSession.js'; + +import { + sendDriverNotification, + sendNotification, + newDriverNotification, +} from './notification.js'; + +const notifySound = new Audio('/deduction.mp3'); + +/** + * Calculate and display remaining time MS + * for a pre-existing session + * + * @param {object} existingSessionData + */ +export function calculateRemainingTime(existingSessionData) { + const endTime = existingSessionData.EndTime; + const remainingTimeMillis = endTime - Date.now(); + displayRemainingTime(remainingTimeMillis); +} + +/** + * Handle when the timer has finished: + * update the server with session data, + * show relevant notification + */ +export function timesUp(uuid, sessionData) { + if ( + 'CurrentDriver' in sessionData && + 'UUID' in sessionData.CurrentDriver + ) { + if (uuid === sessionData.CurrentDriver.UUID && !(Number.isInteger(displayTime))) { + showReset = true; + const notification = sendDriverNotification(notifySound); + notification.onclick = () => { + updateSession(sessionData); + notification.close(); + }; + } else { + sendNotification(notifySound); + } + } + return 'Time\'s up' +}