feat: get username from store getter

This commit is contained in:
Dominik Pschenitschni 2022-10-16 19:36:04 +02:00
parent c20de51a3c
commit c4d7f6fdfa
Signed by untrusted user: dpschen
GPG Key ID: B257AC0149F43A77
3 changed files with 17 additions and 14 deletions

View File

@ -1,6 +1,8 @@
import {computed, unref} from 'vue' import {computed} from 'vue'
import {useI18n} from 'vue-i18n' import {useI18n} from 'vue-i18n'
import {useNow, type MaybeRef} from '@vueuse/core' import {useNow} from '@vueuse/core'
import {useAuthStore} from '@/stores/auth'
type Daytime = 'night' | 'morning' | 'day' | 'evening' type Daytime = 'night' | 'morning' | 'day' | 'evening'
@ -12,17 +14,18 @@ export function hourToDaytime(now: Date): Daytime {
morning: hours < 11, morning: hours < 11,
day: hours < 18, day: hours < 18,
evening: hours < 23, evening: hours < 23,
} as Record<Daytime, boolean> } as Record<Daytime, boolean>
return (Object.keys(daytimeMap) as Daytime[]).find((daytime) => daytimeMap[daytime]) || 'night' return (Object.keys(daytimeMap) as Daytime[]).find((daytime) => daytimeMap[daytime]) || 'night'
} }
export function useDateTimeSalutation(username: MaybeRef<string>) { export function useDateTimeSalutation() {
const {t} = useI18n({useScope: 'global'}) const {t} = useI18n({useScope: 'global'})
const now = useNow() const now = useNow()
const authStore = useAuthStore()
const name = computed(() => authStore.userDisplayName)
const daytime = computed(() => hourToDaytime(now.value)) const daytime = computed(() => hourToDaytime(now.value))
const name = computed(() => unref(username))
const salutations = { const salutations = {
'night': () => t('home.welcomeNight', {username: name.value}), 'night': () => t('home.welcomeNight', {username: name.value}),
@ -31,5 +34,5 @@ export function useDateTimeSalutation(username: MaybeRef<string>) {
'evening': () => t('home.welcomeEvening', {username: name.value}), 'evening': () => t('home.welcomeEvening', {username: name.value}),
} as Record<Daytime, () => string> } as Record<Daytime, () => string>
return computed(() => salutations[daytime.value]()) return computed(() => name.value ? salutations[daytime.value]() : undefined)
} }

View File

@ -3,7 +3,7 @@ import {defineStore, acceptHMRUpdate} from 'pinia'
import {HTTPFactory, AuthenticatedHTTPFactory} from '@/http-common' import {HTTPFactory, AuthenticatedHTTPFactory} from '@/http-common'
import {i18n, getCurrentLanguage, saveLanguage} from '@/i18n' import {i18n, getCurrentLanguage, saveLanguage} from '@/i18n'
import {objectToSnakeCase} from '@/helpers/case' import {objectToSnakeCase} from '@/helpers/case'
import UserModel, { getAvatarUrl } from '@/models/user' import UserModel, { getAvatarUrl, getDisplayName } from '@/models/user'
import UserSettingsService from '@/services/userSettings' import UserSettingsService from '@/services/userSettings'
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth' import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
import {setModuleLoading} from '@/stores/helper' import {setModuleLoading} from '@/stores/helper'
@ -54,6 +54,9 @@ export const useAuthStore = defineStore('auth', {
state.info.type === AUTH_TYPES.LINK_SHARE state.info.type === AUTH_TYPES.LINK_SHARE
) )
}, },
userDisplayName(state) {
return state.info ? getDisplayName(state.info) : undefined
},
}, },
actions: { actions: {
setIsLoading(isLoading: boolean) { setIsLoading(isLoading: boolean) {

View File

@ -1,8 +1,7 @@
<template> <template>
<div class="content has-text-centered"> <div class="content has-text-centered">
<h2 v-if="userInfo"> <h2 v-if="salutation">{{ salutation }}</h2>
{{ salutation }}
</h2>
<message variant="danger" v-if="deletionScheduledAt !== null" class="mb-4"> <message variant="danger" v-if="deletionScheduledAt !== null" class="mb-4">
{{ {{
$t('user.deletion.scheduled', { $t('user.deletion.scheduled', {
@ -78,8 +77,7 @@ import {useNamespaceStore} from '@/stores/namespaces'
import {useAuthStore} from '@/stores/auth' import {useAuthStore} from '@/stores/auth'
import {useTaskStore} from '@/stores/tasks' import {useTaskStore} from '@/stores/tasks'
const username = computed(() => userInfo.name !== '' ? userInfo.name : userInfo.username) const salutation = useDateTimeSalutation()
const salutation = useDateTimeSalutation(username)
const baseStore = useBaseStore() const baseStore = useBaseStore()
const authStore = useAuthStore() const authStore = useAuthStore()
@ -100,7 +98,6 @@ const listHistory = computed(() => {
}) })
const migratorsEnabled = computed(() => configStore.availableMigrators?.length > 0) const migratorsEnabled = computed(() => configStore.availableMigrators?.length > 0)
const userInfo = computed(() => authStore.info)
const hasTasks = computed(() => baseStore.hasTasks) const hasTasks = computed(() => baseStore.hasTasks)
const defaultListId = computed(() => authStore.settings.defaultListId) const defaultListId = computed(() => authStore.settings.defaultListId)
const defaultNamespaceId = computed(() => namespaceStore.namespaces?.[0]?.id || 0) const defaultNamespaceId = computed(() => namespaceStore.namespaces?.[0]?.id || 0)