fix: general user settings empty when loading the settings page

Resolves https://kolaente.dev/vikunja/frontend/issues/2183
This commit is contained in:
kolaente 2022-07-21 16:11:45 +02:00
parent cb3f269937
commit ff48178051
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 28 additions and 8 deletions

View file

@ -0,0 +1,6 @@
// https://stackoverflow.com/a/32108184/10924593
export function objectIsEmpty(obj: any): boolean {
return obj
&& Object.keys(obj).length === 0
&& Object.getPrototypeOf(obj) === Object.prototype
}

View file

@ -85,7 +85,8 @@
v-for="lang in availableLanguageOptions" v-for="lang in availableLanguageOptions"
:key="lang.code" :key="lang.code"
:value="lang.code" :value="lang.code"
>{{ lang.title }}</option> >{{ lang.title }}
</option>
</select> </select>
</div> </div>
</label> </label>
@ -154,7 +155,7 @@ export default defineComponent({
</script> </script>
<script setup lang="ts"> <script setup lang="ts">
import {computed, watch, ref, reactive} from 'vue' import {computed, watch, ref} from 'vue'
import {useI18n} from 'vue-i18n' import {useI18n} from 'vue-i18n'
import {useStore} from 'vuex' import {useStore} from 'vuex'
@ -170,7 +171,8 @@ import {success} from '@/message'
import {AuthenticatedHTTPFactory} from '@/http-common' import {AuthenticatedHTTPFactory} from '@/http-common'
import {useColorScheme} from '@/composables/useColorScheme' import {useColorScheme} from '@/composables/useColorScheme'
import { useTitle } from '@/composables/useTitle' import {useTitle} from '@/composables/useTitle'
import {objectIsEmpty} from '@/helpers/objectIsEmpty'
const {t} = useI18n({useScope: 'global'}) const {t} = useI18n({useScope: 'global'})
useTitle(() => `${t('user.settings.general.title')} - ${t('user.settings.title')}`) useTitle(() => `${t('user.settings.general.title')} - ${t('user.settings.title')}`)
@ -213,6 +215,7 @@ function useAvailableTimezones() {
return availableTimezones return availableTimezones
} }
const availableTimezones = useAvailableTimezones() const availableTimezones = useAvailableTimezones()
function getPlaySoundWhenDoneSetting() { function getPlaySoundWhenDoneSetting() {
@ -223,7 +226,7 @@ const playSoundWhenDone = ref(getPlaySoundWhenDoneSetting())
const quickAddMagicMode = ref(getQuickAddMagicMode()) const quickAddMagicMode = ref(getQuickAddMagicMode())
const store = useStore() const store = useStore()
const settings = reactive({...store.state.auth.settings}) const settings = ref({...store.state.auth.settings})
const id = ref(createRandomID()) const id = ref(createRandomID())
const availableLanguageOptions = ref( const availableLanguageOptions = ref(
Object.entries(availableLanguages) Object.entries(availableLanguages)
@ -231,10 +234,22 @@ const availableLanguageOptions = ref(
.sort((a, b) => a.title.localeCompare(b.title)), .sort((a, b) => a.title.localeCompare(b.title)),
) )
watch(
() => store.state.auth.settings,
() => {
// Only setting if we don't have values set yet to avoid overriding edited values
if (!objectIsEmpty(settings.value)) {
return
}
settings.value = {...store.state.auth.settings}
},
{immediate: true},
)
const defaultList = computed({ const defaultList = computed({
get: () => store.getters['lists/getListById'](settings.defaultListId), get: () => store.getters['lists/getListById'](settings.value.defaultListId),
set(l) { set(l) {
settings.defaultListId = l ? l.id : DEFAULT_LIST_ID settings.value.defaultListId = l ? l.id : DEFAULT_LIST_ID
}, },
}) })
const loading = computed(() => store.state.loading && store.state.loadingModule === 'general-settings') const loading = computed(() => store.state.loading && store.state.loadingModule === 'general-settings')
@ -244,13 +259,12 @@ watch(
(play) => play && playPopSound(), (play) => play && playPopSound(),
) )
async function updateSettings() { async function updateSettings() {
localStorage.setItem(playSoundWhenDoneKey, playSoundWhenDone.value ? 'true' : 'false') localStorage.setItem(playSoundWhenDoneKey, playSoundWhenDone.value ? 'true' : 'false')
setQuickAddMagicMode(quickAddMagicMode.value) setQuickAddMagicMode(quickAddMagicMode.value)
await store.dispatch('auth/saveUserSettings', { await store.dispatch('auth/saveUserSettings', {
settings: {...settings}, settings: {...settings.value},
}) })
} }
</script> </script>