From cb37fd773d9163a772c6ad1f84e6641334cf75f3 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Tue, 4 Jan 2022 17:54:00 +0100 Subject: [PATCH] feat: convert to composable useDateTimeSalutation --- .../useDateTimeSalutation.test.ts} | 7 ++----- .../useDateTimeSalutation.ts} | 11 ++++++++--- src/views/Home.vue | 11 +++++------ 3 files changed, 15 insertions(+), 14 deletions(-) rename src/{helpers/hourToSalutation.test.ts => composables/useDateTimeSalutation.test.ts} (94%) rename src/{helpers/hourToSalutation.ts => composables/useDateTimeSalutation.ts} (63%) diff --git a/src/helpers/hourToSalutation.test.ts b/src/composables/useDateTimeSalutation.test.ts similarity index 94% rename from src/helpers/hourToSalutation.test.ts rename to src/composables/useDateTimeSalutation.test.ts index 3297f71bc..ecd48024f 100644 --- a/src/helpers/hourToSalutation.test.ts +++ b/src/composables/useDateTimeSalutation.test.ts @@ -1,6 +1,5 @@ import {describe, it, expect} from 'vitest' - -import {hourToSalutation} from './hourToSalutation' +import {hourToSalutation} from './useDateTimeSalutation' const dateWithHour = (hours: number): Date => { const date = new Date() @@ -29,6 +28,4 @@ describe('Salutation', () => { const salutation = hourToSalutation(dateWithHour(23)) expect(salutation).toBe('home.welcomeNight') }) -}) - - +}) \ No newline at end of file diff --git a/src/helpers/hourToSalutation.ts b/src/composables/useDateTimeSalutation.ts similarity index 63% rename from src/helpers/hourToSalutation.ts rename to src/composables/useDateTimeSalutation.ts index 68a866f67..001c5d80c 100644 --- a/src/helpers/hourToSalutation.ts +++ b/src/composables/useDateTimeSalutation.ts @@ -1,10 +1,10 @@ +import {computed} from 'vue' import {useNow} from '@vueuse/core' -import {Ref} from 'vue' const TRANSLATION_KEY_PREFIX = 'home.welcome' -export function hourToSalutation(now: Date | Ref = useNow()): String { - const hours = now instanceof Date ? new Date(now).getHours() : new Date(now.value).getHours() +export function hourToSalutation(now: Date) { + const hours = now.getHours() if (hours < 5) { return `${TRANSLATION_KEY_PREFIX}Night` @@ -24,3 +24,8 @@ export function hourToSalutation(now: Date | Ref = useNow()): String { return `${TRANSLATION_KEY_PREFIX}Night` } + +export function useDateTimeSalutation() { + const now = useNow() + return computed(() => hourToSalutation(now.value)) +} \ No newline at end of file diff --git a/src/views/Home.vue b/src/views/Home.vue index bb1449221..f3d2b3ecb 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -66,16 +66,15 @@ import AddTask from '@/components/tasks/add-task.vue' import {getHistory} from '@/modules/listHistory' import {parseDateOrNull} from '@/helpers/parseDateOrNull' import {formatDateShort, formatDateSince} from '@/helpers/time/formatDate' -import {hourToSalutation} from '@/helpers/hourToSalutation' +import {useDateTimeSalutation} from '@/composables/useDateTimeSalutation' -const welcome = computed(hourToSalutation) +const welcome = useDateTimeSalutation() const store = useStore() const listHistory = computed(() => { - const history = getHistory() - return history.map(l => { - return store.getters['lists/getListById'](l.id) - }).filter(l => l !== null) + return getHistory() + .map(l => store.getters['lists/getListById'](l.id)) + .filter(l => l !== null) })