Skip to content

Commit

Permalink
Make current date and time overlay reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 committed Sep 27, 2024
1 parent 082a628 commit 2483dfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
19 changes: 13 additions & 6 deletions website/src/components/TimetableDisplay.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script setup lang="ts">
import { useIntervalFn } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { computed } from 'vue'
import { computed, ref } from 'vue'
import TimetableEmptyClassrooms from '@/components/TimetableEmptyClassrooms.vue'
import TimetableLesson from '@/components/TimetableLesson.vue'
import { useSessionStore } from '@/stores/session'
import { EntityType, useSettingsStore } from '@/stores/settings'
import { type MergedLesson, useTimetableStore } from '@/stores/timetable'
import { getCurrentDay } from '@/utils/days'
import { getCurrentDay, getIsWeekend } from '@/utils/days'
import { localizedWeekdays } from '@/utils/localization'
import { getCurrentTime, lessonTimes } from '@/utils/times'
Expand All @@ -25,9 +26,15 @@ const { lessons } = storeToRefs(useTimetableStore())
const { showHoursInTimetable, highlightCurrentTime, enableLessonDetails } =
storeToRefs(useSettingsStore())
const isWeekend = [0, 6].includes(new Date().getDay())
const currentDay = getCurrentDay()
const currentTime = getCurrentTime()
const isWeekend = ref(getIsWeekend())
const currentDay = ref(getCurrentDay())
const currentTime = ref(getCurrentTime())
useIntervalFn(() => {
isWeekend.value = getIsWeekend()
currentDay.value = getCurrentDay()
currentTime.value = getCurrentTime()
}, 30000)
/**
* Determines the range of lesson times that need to be displayed in the timetable.
Expand All @@ -48,7 +55,7 @@ function lessonStyles(dayIndex: number, timeIndex: number) {
// prettier-ignore
return {
'bg-surface-highlighted': lessons.value[timeIndex][dayIndex]?.find(lesson => lesson.isSubstitution),
'current-time': highlightCurrentTime.value && !isWeekend && dayIndex === currentDay && timeIndex === currentTime,
'current-time': highlightCurrentTime.value && !isWeekend.value && dayIndex === currentDay.value && timeIndex === currentTime.value,
}
}
Expand Down
11 changes: 11 additions & 0 deletions website/src/utils/days.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export function getCurrentDay(): number {
return currentDay - 1
}

/**
* Returns whether it is currently a weekend.
*/
export function getIsWeekend(): boolean {
return [0, 6].includes(new Date().getDay())
}

/**
* Returns the weekdays of the week of the given date.
*
Expand Down Expand Up @@ -54,5 +61,9 @@ export function getWeekdays(date: Date): Date[] {
* Returns the ISO date string of the given date.
*/
export function getISODate(date: Date): string {
// Make sure the date is UTC midnight, otherwise, conversion to ISO may be wrong
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))

// Convert the date into ISO format and return the date part
return date.toISOString().split('T')[0]
}

0 comments on commit 2483dfd

Please sign in to comment.