feat(language): selected language is now saved in account.user.lang and persists in navigation
gitea/salix-front/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2022-04-20 16:23:03 +02:00
parent 913be03e94
commit 05272cbbb2
4 changed files with 61 additions and 11 deletions

View File

@ -1,8 +1,10 @@
<script setup> <script setup>
import { useI18n } from 'vue-i18n';
import { useState } from 'src/composables/useState'; import { useState } from 'src/composables/useState';
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import UserPanel from 'src/components/UserPanel.vue'; import UserPanel from 'src/components/UserPanel.vue';
const { t } = useI18n();
const session = useSession(); const session = useSession();
const state = useState(); const state = useState();
const user = state.getUser(); const user = state.getUser();
@ -16,12 +18,19 @@ function onToggleDrawer() {
<template> <template>
<q-header class="bg-dark" color="white" elevated bordered> <q-header class="bg-dark" color="white" elevated bordered>
<q-toolbar class="q-py-sm q-px-md"> <q-toolbar class="q-py-sm q-px-md">
<q-btn flat @click="onToggleDrawer()" round dense icon="menu" /> <q-btn flat @click="onToggleDrawer()" round dense icon="menu">
<q-tooltip bottom anchor="bottom right">
{{ t('globals.collapseMenu') }}
</q-tooltip>
</q-btn>
<router-link to="/"> <router-link to="/">
<q-btn flat round class="q-ml-xs" v-if="$q.screen.gt.xs"> <q-btn flat round class="q-ml-xs" v-if="$q.screen.gt.xs">
<q-avatar square size="md"> <q-avatar square size="md">
<q-img src="~/assets/logo_icon.svg" alt="Logo" /> <q-img src="~/assets/logo_icon.svg" alt="Logo" />
</q-avatar> </q-avatar>
<q-tooltip bottom>
{{ t('globals.backToDashboard') }}
</q-tooltip>
</q-btn> </q-btn>
</router-link> </router-link>
<q-toolbar-title shrink class="text-weight-bold">Salix</q-toolbar-title> <q-toolbar-title shrink class="text-weight-bold">Salix</q-toolbar-title>
@ -40,16 +49,23 @@ function onToggleDrawer() {
</q-list> </q-list>
</q-menu> </q-menu>
</q-btn> </q-btn>
<q-btn v-if="$q.screen.gt.xs" dense flat round size="md" icon="notifications" /> <q-btn v-if="$q.screen.gt.xs" dense flat round size="md" icon="notifications">
<q-tooltip bottom>
{{ t('globals.notifications') }}
</q-tooltip>
</q-btn>
<q-btn dense flat no-wrap> <q-btn dense flat no-wrap>
<q-avatar size="lg"> <q-avatar size="lg">
<q-img <q-img
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`" :src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`"
spinner-color="white" spinner-color="white"
/> >
</q-img>
</q-avatar> </q-avatar>
<q-tooltip>Account</q-tooltip>
<q-icon name="arrow_drop_down" size="s" /> <q-icon name="arrow_drop_down" size="s" />
<q-tooltip bottom>
{{ t('globals.userPanel') }}
</q-tooltip>
<UserPanel /> <UserPanel />
</q-btn> </q-btn>
</div> </div>

View File

@ -28,14 +28,13 @@ const darkMode = computed({
const user = state.getUser(); const user = state.getUser();
const token = session.getToken(); const token = session.getToken();
if (user.value.darkMode) darkMode.value = user.value.darkMode;
onMounted(async () => { onMounted(async () => {
try { try {
const stateRoles = state.getRoles().value; const stateRoles = state.getRoles().value;
if (stateRoles.length === 0) { if (stateRoles.length === 0) {
await role.fetch(); await role.fetch();
} }
updatePreferences();
} catch (error) { } catch (error) {
quasar.notify({ quasar.notify({
message: t('errors.statusUnauthorized'), message: t('errors.statusUnauthorized'),
@ -45,6 +44,15 @@ onMounted(async () => {
} }
}); });
function updatePreferences() {
if (user.value.darkMode) {
darkMode.value = user.value.darkMode;
}
if (user.value.lang) {
locale.value = user.value.lang;
}
}
async function saveDarkMode(value) { async function saveDarkMode(value) {
const query = `/api/UserConfigs/${user.value.id}`; const query = `/api/UserConfigs/${user.value.id}`;
await axios.patch(query, { await axios.patch(query, {
@ -67,9 +75,9 @@ function logout() {
</script> </script>
<template> <template>
<q-menu> <q-menu anchor="bottom left">
<div class="row no-wrap q-pa-md"> <div class="row no-wrap q-pa-md">
<div class="column"> <div class="column panel">
<div class="text-h6 q-mb-md">{{ t('components.userPanel.settings') }}</div> <div class="text-h6 q-mb-md">{{ t('components.userPanel.settings') }}</div>
<q-toggle <q-toggle
v-model="locale" v-model="locale"
@ -83,16 +91,16 @@ function logout() {
<q-toggle <q-toggle
v-model="darkMode" v-model="darkMode"
@update:model-value="saveDarkMode" @update:model-value="saveDarkMode"
:label="t(`globals.theme`)"
checked-icon="dark_mode" checked-icon="dark_mode"
color="orange" color="orange"
unchecked-icon="light_mode" unchecked-icon="light_mode"
/> />
<q-btn color="orange" outline size="sm" label="Settings" icon="settings" />
</div> </div>
<q-separator vertical inset class="q-mx-lg" /> <q-separator vertical inset class="q-mx-lg" />
<div class="column items-center" style="min-width: 150px"> <div class="column items-center panel">
<q-avatar size="80px"> <q-avatar size="80px">
<q-img <q-img
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`" :src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`"
@ -105,8 +113,22 @@ function logout() {
</div> </div>
<div class="text-subtitle3 text-grey-7 q-mb-xs">@{{ user.name }}</div> <div class="text-subtitle3 text-grey-7 q-mb-xs">@{{ user.name }}</div>
<q-btn color="orange" flat label="Log Out" size="sm" icon="logout" @click="logout()" v-close-popup /> <q-btn
color="orange"
flat
:label="t('globals.logOut')"
size="sm"
icon="logout"
@click="logout()"
v-close-popup
/>
</div> </div>
</div> </div>
</q-menu> </q-menu>
</template> </template>
<style lang="scss" scoped>
.panel {
width: 150px;
}
</style>

View File

@ -4,6 +4,12 @@ export default {
es: 'Spanish', es: 'Spanish',
en: 'English', en: 'English',
}, },
collapseMenu: 'Collapse left menu',
backToDashboard: 'Return to dashboard',
notifications: 'Notifications',
userPanel: 'User panel',
theme: 'Theme',
logOut: 'Log out',
}, },
errors: { errors: {
statusUnauthorized: 'Access denied', statusUnauthorized: 'Access denied',

View File

@ -4,6 +4,12 @@ export default {
es: 'Español', es: 'Español',
en: 'Inglés', en: 'Inglés',
}, },
collapseMenu: 'Contraer menú lateral',
backToDashboard: 'Volver al tablón',
notifications: 'Notificaciones',
userPanel: 'Panel de usuario',
theme: 'Tema',
logOut: 'Cerrar sesión',
}, },
errors: { errors: {
statusUnauthorized: 'Acceso denegado', statusUnauthorized: 'Acceso denegado',