use localSetting to store a global state of user choices

This commit is contained in:
lesion 2023-02-03 21:55:33 +01:00
parent 3eacc7ea33
commit f2376997b8
No known key found for this signature in database
GPG key ID: 352918250B012177
6 changed files with 60 additions and 61 deletions

View file

@ -4,7 +4,7 @@
ref='cal' ref='cal'
v-model='selectedDate' v-model='selectedDate'
title-position='left' title-position='left'
:is-dark="settings['theme.is_dark']" :is-dark="is_dark"
:columns="!$vuetify.breakpoint.smAndDown ? 2 : 1" :columns="!$vuetify.breakpoint.smAndDown ? 2 : 1"
@input='click' @input='click'
@update:from-page='updatePage' @update:from-page='updatePage'
@ -15,20 +15,14 @@
aria-label='Calendar' aria-label='Calendar'
is-expanded is-expanded
is-inline) is-inline)
//- template(v-slot="{ inputValue, inputEvents }")
v-btn#calendarButton(v-on='inputEvents' text tile :color='selectedDate ? "primary" : "" ') {{inputValue || $t('common.calendar')}}
v-icon(v-if='selectedDate' v-text='mdiClose' right small icon @click.prevent.stop='selectedDate = null')
v-icon(v-else v-text='mdiChevronDown' right small icon)
.calh.d-flex.justify-center.align-center(slot='placeholder') .calh.d-flex.justify-center.align-center(slot='placeholder')
v-progress-circular(indeterminate) v-progress-circular(indeterminate)
//- v-btn#calendarButton(text tile) {{$t('common.calendar')}}
//- v-icon(v-text='mdiChevronDown' right small icon)
</template> </template>
</template> </template>
<script> <script>
import { mapState } from 'vuex' import { mapState, mapGetters } from 'vuex'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { mdiChevronDown, mdiClose } from '@mdi/js' import { mdiChevronDown, mdiClose } from '@mdi/js'
import { attributesFromEvents } from '../assets/helper' import { attributesFromEvents } from '../assets/helper'
@ -46,6 +40,7 @@ export default {
}, },
computed: { computed: {
...mapState(['settings', 'events']), ...mapState(['settings', 'events']),
...mapGetters(['is_dark']),
attributes () { attributes () {
return attributesFromEvents(this.events) return attributesFromEvents(this.events)
} }

View file

@ -17,15 +17,13 @@ v-card.h-event.event.d-flex(itemscope itemtype="https://schema.org/Event")
</template> </template>
<script> <script>
import { mapState } from 'vuex' import { mapGetters } from 'vuex'
import MyPicture from '~/components/MyPicture' import MyPicture from '~/components/MyPicture'
import { mdiRepeat, mdiCalendar, mdiMapMarker } from '@mdi/js' import { mdiRepeat, mdiCalendar, mdiMapMarker } from '@mdi/js'
export default { export default {
data({ $store }) { data({ $store }) {
return { mdiRepeat, mdiMapMarker, mdiCalendar, return { mdiRepeat, mdiMapMarker, mdiCalendar }
hide_thumbs: this.$cookies.get('theme.hide_thumbs') || $store.state.settings.hide_thumbs
}
}, },
components: { components: {
MyPicture MyPicture
@ -34,6 +32,6 @@ export default {
event: { type: Object, default: () => ({}) }, event: { type: Object, default: () => ({}) },
lazy: Boolean lazy: Boolean
}, },
computed: mapState(['settings']) computed: mapGetters(['hide_thumbs'])
} }
</script> </script>

View file

@ -1,48 +1,31 @@
<template lang="pug"> <template lang="pug">
div.p-0.m-0.d-flex.justify-end(:key='reload') div.p-0.m-0.d-flex.justify-end
v-icon.ml-5(@click='_is_dark()' v-text='mdiContrastCircle') v-icon.ml-5(@click='toggleDark' v-text='mdiContrastCircle')
v-icon.ml-5(@click='_hide_thumbs()' v-if="!hide_thumbs" v-text='!hide_thumbs_icon ? mdiViewList : mdiViewModule') v-icon.ml-5(@click='toggleHideThumbs' v-text='hide_thumbs ? mdiViewList : mdiViewModule')
</template> </template>
<script> <script>
import { mapActions, mapState } from 'vuex' import { mapActions, mapGetters } from 'vuex'
import { mdiViewModule, mdiViewList, mdiContrastCircle } from '@mdi/js' import { mdiViewModule, mdiViewList, mdiContrastCircle } from '@mdi/js'
export default { export default {
name: 'ThemeView', name: 'ThemeView',
data ({ $store }) { data () {
return { return { mdiViewModule, mdiViewList, mdiContrastCircle }
mdiViewModule, mdiViewList, mdiContrastCircle, },
hide_thumbs: $store.state.settings.hide_thumbs, computed: {
hide_thumbs_icon: $store.state.settings.hide_thumbs || this.$cookies.get('theme.hide_thumbs'), ...mapGetters(['hide_thumbs', 'is_dark']),
reload: 0
}
}, },
methods: { methods: {
...mapState(['settings']), ...mapActions(['setLocalSetting']),
async _is_dark() { async toggleDark() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark this.$vuetify.theme.dark = !this.$vuetify.theme.dark
await this.$cookies.set('theme.is_dark', this.$vuetify.theme.dark, { this.setLocalSetting({ key: 'theme.is_dark', value: !this.is_dark })
path: '/',
maxAge: 60 * 60 * 24 * 7
})
},
async _hide_thumbs() {
let theme_hide_thumbs = await this.$cookies.get('theme.hide_thumbs')
if (theme_hide_thumbs != null) {
theme_hide_thumbs = !theme_hide_thumbs
} else {
theme_hide_thumbs = !this.settings.hide_thumbs
}
await this.$cookies.set('theme.hide_thumbs', theme_hide_thumbs, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
this.hide_thumbs_icon = theme_hide_thumbs
this.reload++
this.$root.$emit('layout_loaded', true)
}, },
async toggleHideThumbs() {
this.setLocalSetting({ key: 'hide_thumbs', value: !this.hide_thumbs })
}
} }
} }
</script> </script>

View file

@ -19,7 +19,7 @@ import Appbar from '../components/Appbar.vue'
import Snackbar from '../components/Snackbar' import Snackbar from '../components/Snackbar'
import Footer from '../components/Footer' import Footer from '../components/Footer'
import Confirm from '../components/Confirm' import Confirm from '../components/Confirm'
import { mapState } from 'vuex' import { mapState, mapGetters } from 'vuex'
export default { export default {
head () { head () {
@ -32,14 +32,12 @@ export default {
}, },
name: 'Default', name: 'Default',
components: { Appbar, Snackbar, Footer, Confirm }, components: { Appbar, Snackbar, Footer, Confirm },
computed: mapState(['settings']), computed: {
...mapState(['settings']),
...mapGetters(['is_dark'])
},
created () { created () {
const theme_is_dark = this.$cookies.get('theme.is_dark') this.$vuetify.theme.dark = this.is_dark
if ( theme_is_dark != null ) {
this.$vuetify.theme.dark = theme_is_dark
} else {
this.$vuetify.theme.dark = this.settings['theme.is_dark']
}
} }
} }
</script> </script>

View file

@ -10,7 +10,7 @@ v-container.px-2.px-sm-6.pt-0
Announcement(v-for='announcement in announcements' :key='`a_${announcement.id}`' :announcement='announcement') Announcement(v-for='announcement in announcements' :key='`a_${announcement.id}`' :announcement='announcement')
//- Events //- Events
#events.mt-sm-4.mt-2(:key="reload_events") #events.mt-sm-4.mt-2
Event(:event='event' v-for='(event, idx) in visibleEvents' :lazy='idx>2' :key='event.id') Event(:event='event' v-for='(event, idx) in visibleEvents' :lazy='idx>2' :key='event.id')
</template> </template>
@ -48,7 +48,6 @@ export default {
tmpEvents: [], tmpEvents: [],
selectedDay: null, selectedDay: null,
storeUnsubscribe: null, storeUnsubscribe: null,
reload_events: 0
} }
}, },
head () { head () {
@ -100,10 +99,6 @@ export default {
} }
} }
}}) }})
// this.$root.$on('search', debounce(this.search, 100))
// this.$root.$on('layout_loaded', () => {
// this.reload_events++
// })
}, },
destroyed () { destroyed () {
this.$root.$off('dayclick') this.$root.$off('dayclick')

View file

@ -1,6 +1,10 @@
import dayjs from 'dayjs' import dayjs from 'dayjs'
export const state = () => ({ export const state = () => ({
localSettings : {
hide_thumbs: null,
'theme.is_dark': null
},
settings: { settings: {
instance_timezone: 'Europe/Rome', instance_timezone: 'Europe/Rome',
instance_name: '', instance_name: '',
@ -21,7 +25,10 @@ export const state = () => ({
enable_trusted_instances: true, enable_trusted_instances: true,
trusted_instances: [], trusted_instances: [],
trusted_instances_label: '', trusted_instances_label: '',
footerLinks: [] footerLinks: [],
hide_thumbs: false,
'theme.is_dark': true,
hide_calendar: false
}, },
filter: { filter: {
query: '', query: '',
@ -32,6 +39,15 @@ export const state = () => ({
events: [] events: []
}) })
export const getters = {
hide_thumbs (state) {
return (state.localSettings['hide_thumbs'] === null) ? state.settings.hide_thumbs : state.localSettings.hide_thumbs
},
is_dark (state) {
return (state.localSettings['theme.is_dark'] === null) ? state.settings['theme.is_dark'] : state.localSettings['theme.is_dark']
}
}
export const mutations = { export const mutations = {
setSettings (state, settings) { setSettings (state, settings) {
state.settings = settings state.settings = settings
@ -39,6 +55,9 @@ export const mutations = {
setSetting (state, setting) { setSetting (state, setting) {
state.settings[setting.key] = setting.value state.settings[setting.key] = setting.value
}, },
setLocalSetting(state, setting) {
state.localSettings[setting.key] = setting.value
},
setAnnouncements (state, announcements) { setAnnouncements (state, announcements) {
state.announcements = announcements state.announcements = announcements
}, },
@ -53,13 +72,17 @@ export const mutations = {
export const actions = { export const actions = {
// this method is called server side only for each request for nuxt // this method is called server side only for each request for nuxt
// we use it to get configuration from db, set locale, etc... // we use it to get configuration from db, set locale, etc...
nuxtServerInit ({ commit }, { _req, res }) { nuxtServerInit ({ commit }, { res, app }) {
if (res.locals && res.locals.settings) { if (res.locals && res.locals.settings) {
commit('setSettings', res.locals.settings) commit('setSettings', res.locals.settings)
} }
commit('setFilter', { type: 'show_recurrent', commit('setFilter', { type: 'show_recurrent',
value: res.locals.settings.allow_recurrent_event && res.locals.settings.recurrent_event_visible }) value: res.locals.settings.allow_recurrent_event && res.locals.settings.recurrent_event_visible })
commit('setLocalSetting', { key: 'hide_thumbs', value: app.$cookies.get('hide_thumbs') })
commit('setLocalSetting', { key: 'theme.is_dark', value: app.$cookies.get('theme.is_dark') })
if (res.locals.status === 'READY') { if (res.locals.status === 'READY') {
commit('setAnnouncements', res.locals.announcements) commit('setAnnouncements', res.locals.announcements)
} }
@ -75,6 +98,13 @@ export const actions = {
await this.$axios.$post('/settings', setting) await this.$axios.$post('/settings', setting)
commit('setSetting', setting) commit('setSetting', setting)
}, },
async setLocalSetting ({ commit }, setting) {
this.$cookies.set(setting.key, setting.value, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
commit('setLocalSetting', setting)
},
setFilter ({ commit }, [type, value]) { setFilter ({ commit }, [type, value]) {
commit('setFilter', { type, value }) commit('setFilter', { type, value })
}, },