From 7c24d821a50c376d09f359d9737566997ab75785 Mon Sep 17 00:00:00 2001 From: Nemo Godebski-Pedersen Date: Wed, 21 May 2025 19:21:02 +0700 Subject: [PATCH] inactivity timeout refactor for Vue --- components.d.ts | 1 + src/assets/inactivity-timer.js | 36 --------- src/components/AutoLogout.vue | 142 +++++++++++++++++++++++++++++++++ src/components/HeaderNav.vue | 11 +-- src/views/BodyView.vue | 16 +++- 5 files changed, 163 insertions(+), 43 deletions(-) delete mode 100644 src/assets/inactivity-timer.js create mode 100644 src/components/AutoLogout.vue diff --git a/components.d.ts b/components.d.ts index 3aa6149..1813313 100644 --- a/components.d.ts +++ b/components.d.ts @@ -7,6 +7,7 @@ export {} /* prettier-ignore */ declare module 'vue' { export interface GlobalComponents { + AutoLogout: typeof import('./src/components/AutoLogout.vue')['default'] ChatMessage: typeof import('./src/components/ChatMessage.vue')['default'] ChatWidget: typeof import('./src/components/ChatWidget.vue')['default'] ChatWidgetOld: typeof import('./src/components/ChatWidgetOld.vue')['default'] diff --git a/src/assets/inactivity-timer.js b/src/assets/inactivity-timer.js deleted file mode 100644 index 93b4882..0000000 --- a/src/assets/inactivity-timer.js +++ /dev/null @@ -1,36 +0,0 @@ -function addSeconds(date, seconds) { - date.setSeconds(date.getSeconds() + seconds); - return date; -} - -export function showCountdownTimer() { - - // Get current date and time - var now = new Date().getTime(); - let distance = countDownDate - now; - - // Output the result in an element with id="countdown-timer-span" - let elementId = ("countdown-timer-span"); - document.getElementById(elementId).innerHTML = "in " + Math.floor(distance / 1000) + "s"; - - // If the count down is over release resources - if (distance < 0) { - destroyCountdownTimer(); - } -} - - -// Set the date we're counting down to -let countDownDate = addSeconds(new Date(), 5); - -let showTimerInterval = null; - -export function destroyCountdownTimer() { - clearInterval(showTimerInterval); -} - -export function resetCountdownTimer(seconds) { - destroyCountdownTimer(); // Destroy previous timer if any - countDownDate = addSeconds(new Date(), seconds); // Set the date we're counting down to - showTimerInterval = setInterval(showCountdownTimer, 1000); // Update the count down every 1 second -} \ No newline at end of file diff --git a/src/components/AutoLogout.vue b/src/components/AutoLogout.vue new file mode 100644 index 0000000..5461002 --- /dev/null +++ b/src/components/AutoLogout.vue @@ -0,0 +1,142 @@ + + + + + + \ No newline at end of file diff --git a/src/components/HeaderNav.vue b/src/components/HeaderNav.vue index 48c9997..49a86d3 100644 --- a/src/components/HeaderNav.vue +++ b/src/components/HeaderNav.vue @@ -60,7 +60,8 @@ const headerLinksBackgroundColor = ref(headerLinksBackgroundColorSetting) const clearActiveTab = () => { const activeLinks = document.querySelectorAll('.router-link') for (const active of activeLinks) { - if (active.id) { + // Skip login and logoff buttons + if (active.id && active.id !== 'login' && active.id !== 'logoff') { active.style.backgroundColor = 'transparent' active.style.color = '#39455f' } @@ -163,11 +164,11 @@ const getCurrentPath = () => { --> - - + {{ $t('header.logoff') }} @@ -233,8 +234,8 @@ nav { cursor: pointer; } -.login-button, -.logoff-button { +a.login-button, +a.logoff-button { margin: 5px; color: #ffffff; background-color: #32b9ce; diff --git a/src/views/BodyView.vue b/src/views/BodyView.vue index c213ffe..da2ae49 100644 --- a/src/views/BodyView.vue +++ b/src/views/BodyView.vue @@ -28,14 +28,26 @@