Change naming and negation for better readability

This commit is contained in:
Wolfgang Huß 2022-05-06 10:50:38 +02:00
parent 46f27ccb9b
commit 652665d1fb

View File

@ -21,30 +21,30 @@ export default ({ app, req, cookie, store }) => {
const changeHandler = async (mutation) => { const changeHandler = async (mutation) => {
if (process.server) return if (process.server) return
const newLocale = mutation.payload.locale const localeInStore = mutation.payload.locale
let isCookie = true let cookieExists = true
let currentLocale = await app.$cookies.get(key) let localeInCookies = await app.$cookies.get(key)
if (!currentLocale) { if (!localeInCookies) {
isCookie = false cookieExists = false
currentLocale = navigator.language.split('-')[0] // get browser language localeInCookies = navigator.language.split('-')[0] // get browser language
} }
const isDifferent = newLocale !== currentLocale const isLocaleStoreSameAsCookies = localeInStore === localeInCookies
// cookie has to be set, otherwise Cypress test does not work // cookie has to be set, otherwise Cypress test does not work
if (isCookie && !isDifferent) { if (cookieExists && isLocaleStoreSameAsCookies) {
return return
} }
const expires = new Date() const expires = new Date()
expires.setDate(expires.getDate() + app.$env.COOKIE_EXPIRE_TIME) expires.setDate(expires.getDate() + app.$env.COOKIE_EXPIRE_TIME)
app.$cookies.set(key, newLocale, { app.$cookies.set(key, localeInStore, {
expires, expires,
// maxAge: app.$env.COOKIE_EXPIRE_TIME * 60 * 60 * 24, // days to seconds // maxAge: app.$env.COOKIE_EXPIRE_TIME * 60 * 60 * 24, // days to seconds
sameSite: 'lax', // for the meaning see https://www.thinktecture.com/de/identity/samesite/samesite-in-a-nutshell/ sameSite: 'lax', // for the meaning see https://www.thinktecture.com/de/identity/samesite/samesite-in-a-nutshell/
}) })
if (!app.$i18n.localeExists(newLocale)) { if (!app.$i18n.localeExists(localeInStore)) {
import(`~/locales/${newLocale}.json`).then((res) => { import(`~/locales/${localeInStore}.json`).then((res) => {
app.$i18n.add(newLocale, res.default) app.$i18n.add(localeInStore, res.default)
}) })
} }
@ -54,7 +54,7 @@ export default ({ app, req, cookie, store }) => {
if (user && user._id && token) { if (user && user._id && token) {
// TODO: SAVE LOCALE // TODO: SAVE LOCALE
// store.dispatch('usersettings/patch', { // store.dispatch('usersettings/patch', {
// uiLanguage: newLocale // uiLanguage: localeInStore
// }, { root: true }) // }, { root: true })
} }
} }