Skip to content

Commit

Permalink
wip: other strategy redirect in locale-choice page
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Jan 1, 2023
1 parent 098d384 commit f75bf87
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
33 changes: 31 additions & 2 deletions pages/pix-site/locale-choice.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<client-only>
<div class="locale-choice">
<div :class="localeChoiceClassName">
<h1>
<pix-image
class="logo-pix"
Expand All @@ -13,7 +13,10 @@
class="locale-choice__link"
:locale="locale"
/>
<pix-image class="planet" :field="{ url: '/images/planet.svg' }" />
<pix-image
:class="planetClassName"
:field="{ url: '/images/planet.svg' }"
/>
</div>
</client-only>
</template>
Expand All @@ -26,12 +29,38 @@ export default {
data() {
return {
locales: language.locales,
localeChoiceClassName: 'not-visible',
planetClassName: 'not-visible',
}
},
mounted() {
console.log('mounted')
const cookieLocale = _getLocaleFromCookie()
console.log({ cookieLocale })
if (!cookieLocale) {
this.localeChoiceClassName = 'locale-choice'
this.planetClassName = 'planet'
return
}
this.$router.push(`/${cookieLocale}`)
},
}
function _getLocaleFromCookie() {
const localeCookie = document.cookie
.split(';')
.find((item) => item.startsWith('locale'))
if (!localeCookie) return null
return localeCookie.split('=')[1]
}
</script>

<style lang="scss" scoped>
.not-visible {
visibility: hidden;
}
.logo-pix {
margin: 0 auto 5vh;
display: block;
Expand Down
24 changes: 12 additions & 12 deletions plugins/locale-observer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
export default function ({ app, route }) {
export default function ({ app }) {
window.onNuxtReady(() => {
console.log('window.onNuxtReady')
const cookieLocale = _getLocaleFromCookie()
if (!cookieLocale) return
// const cookieLocale = _getLocaleFromCookie()
// if (!cookieLocale) return

console.log({ path: route.path })
window.$nuxt.$router.push(`/${cookieLocale}`)
// console.log({ path: route.path })
// window.$nuxt.$router.push(`/${cookieLocale}`)
})

app.i18n.onBeforeLanguageSwitch = (oldLocale, newLocale) => {
_setLocaleCookie(newLocale)
}
}

function _getLocaleFromCookie() {
const localeCookie = document.cookie
.split(';')
.find((item) => item.startsWith('locale'))
if (!localeCookie) return null
return localeCookie.split('=')[1]
}
// function _getLocaleFromCookie() {
// const localeCookie = document.cookie
// .split(';')
// .find((item) => item.startsWith('locale'))
// if (!localeCookie) return null
// return localeCookie.split('=')[1]
// }

function _setLocaleCookie(locale) {
document.cookie = `locale=${locale};path=/`
Expand Down
36 changes: 18 additions & 18 deletions tests/plugins/locale-observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ describe('Plugins | locale-observer', () => {
})

describe('when is first page', () => {
describe('when cookie', () => {
it('redirects to locale index', () => {
// given
document.cookie = 'locale=fr'
const context = {
app: {
i18n: {},
},
route: {
path: '/',
},
}
// describe('when cookie', () => {
// it('redirects to locale index', () => {
// // given
// document.cookie = 'locale=fr'
// const context = {
// app: {
// i18n: {},
// },
// route: {
// path: '/',
// },
// }

// when
localeObserver(context)
// // when
// localeObserver(context)

// then
expect(window.$nuxt.$router.push).toHaveBeenCalledWith('/fr')
})
})
// // then
// expect(window.$nuxt.$router.push).toHaveBeenCalledWith('/fr')
// })
// })

describe('when no cookie', () => {
it('save locale', () => {
Expand Down

0 comments on commit f75bf87

Please sign in to comment.