Refresh user preferences from back-end when entering the Options view

This commit is contained in:
Bubka 2024-02-18 14:58:21 +01:00
parent f2d4c43239
commit 300d3c3dc8
4 changed files with 28 additions and 2 deletions

View File

@ -4,6 +4,15 @@ const apiClient = httpClientFactory('api')
const webClient = httpClientFactory('web')
export default {
/**
* Get current signed-in user preferences
*
* @returns promise
*/
getPreferences(config = {}) {
return apiClient.get('/user/preferences', { ...config })
},
/**
* Update a user preference
*

View File

@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import authService from '@/services/authService'
import userService from '@/services/userService'
import router from '@/router'
import { useColorMode } from '@vueuse/core'
import { useTwofaccounts } from '@/stores/twofaccounts'
@ -108,7 +109,6 @@ export const useUserStore = defineStore({
mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme
},
/**
* Applies user language
*/
@ -121,13 +121,27 @@ export const useUserStore = defineStore({
else loadLanguageAsync('en')
},
/**
* Applies both user theme & language
*/
applyUserPrefs() {
this.applyTheme()
this.applyLanguage()
},
/**
* Refresh user preferences with backend state
*/
refreshPreferences() {
userService.getPreferences({returnError: true})
.then(response => {
response.data.forEach(preference => {
this.preferences[preference.key] = preference.value
})
})
.catch(error => {
notify.alert({ text: trans('errors.data_cannot_be_refreshed_from_server') })
})
}
},

View File

@ -78,6 +78,8 @@
})
}
})
user.refreshPreferences()
})
/**

View File

@ -65,4 +65,5 @@ return [
'sso_no_register' => 'Registrations are disabled',
'sso_email_already_used' => 'A user account with the same email address already exists but it does not match your external account ID. Do not use SSO if you are already registered on 2FAuth with this email.',
'account_managed_by_external_provider' => 'Account managed by an external provider',
'data_cannot_be_refreshed_from_server' => 'Data cannot be refreshed from server'
];