Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Ne pas passer par la page /locale-choice pour choisir sa locale #459

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions layouts/empty.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<nuxt />
</template>

<script>
export default {
name: 'Empty',
}
</script>
29 changes: 0 additions & 29 deletions layouts/home.vue

This file was deleted.

90 changes: 86 additions & 4 deletions pages/pix-site/index.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
<template><div></div></template>
<template>
<client-only v-if="shouldDisplayLocaleChoice">
<main class="home">
<div class="locale-choice">
<h1>
<pix-image
class="logo-pix"
:field="{ url: '/images/logo-pix-blanc.svg', alt: 'Pix' }"
/>
</h1>
<locale-link
v-for="locale in locales"
:key="locale.code"
class="locale-choice__link"
:locale="locale"
/>
<pix-image class="planet" :field="{ url: '/images/planet.svg' }" />
</div>
</main>
</client-only>
</template>

<script>
import { language } from '~/config/language'

export default {
layout: 'empty',
nuxtI18n: false,
layout: 'simple',
data() {
return {
locales: language.locales,
shouldDisplayLocaleChoice: false,
}
},
mounted() {
const chosenLocale = this.getLocaleFromCookie()
const url = chosenLocale ? `/${chosenLocale}/` : '/locale-choice'
this.$router.push(url)
if (chosenLocale) {
return this.$router.replace(`/${chosenLocale}/`)
}
this.shouldDisplayLocaleChoice = true
},
methods: {
getLocaleFromCookie() {
Expand All @@ -30,3 +58,57 @@ export default {
},
}
</script>

<style lang="scss" scoped>
.home {
width: 100%;
min-width: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 5rem 1rem 0;
background: url('/images/stars.svg') repeat, $default-gradient;

@include device-is('large-mobile') {
padding: 0;
}
}

.locale-choice {
padding: 3rem 0;
}

.logo-pix {
margin: 0 auto 5vh;
display: block;
width: 11rem;
max-width: 100%;
}

.locale-choice__link + .locale-choice__link {
margin-top: 1rem;
}

.planet {
margin: 5vh auto 0;
display: block;
width: max(16vw, 90%);
}

@include device-is('large-mobile') {
.locale-choice {
position: relative;
}

.planet {
position: absolute;
top: 50%;
right: calc(100% + 4vw);
max-width: unset;
margin: 0;
transform: translateY(-50%);
}
}
</style>
70 changes: 0 additions & 70 deletions pages/pix-site/locale-choice.vue

This file was deleted.

42 changes: 32 additions & 10 deletions tests/pages/pix-site/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { shallowMount } from '@vue/test-utils'
import Vue from 'vue'
import Index from '@/pages/pix-site/index.vue'

describe('Index Page', () => {
let $router

beforeEach(() => {
$router = {
push: jest.fn(),
replace: jest.fn(),
}

let cookieJar = ''
Expand All @@ -19,28 +20,40 @@ describe('Index Page', () => {

describe('#mounted', () => {
describe('when there is no cookie', () => {
test('redirects to /locale-choice page', () => {
test('do not redirect', async () => {
// given
document.cookie = ''

// when
shallowMount(Index, { mocks: { $router } })
const wrapper = shallowMount(Index, {
mocks: { $router },
stubs: ['client-only', 'pix-image', 'locale-link'],
})
await Vue.nextTick()

// then
expect($router.push).toHaveBeenCalledWith('/locale-choice')
expect($router.replace).not.toHaveBeenCalled()
const localeLinks = wrapper.findAll('locale-link-stub')
expect(localeLinks.length).toBe(4)
})
})

describe('when there is a cookie', () => {
test('redirects to locale page', () => {
test('redirects to locale page', async () => {
// given
document.cookie = 'foo=bar; locale=fr'

// when
shallowMount(Index, { mocks: { $router } })
const wrapper = shallowMount(Index, {
mocks: { $router },
stubs: ['client-only', 'pix-image', 'locale-link'],
})
await Vue.nextTick()

// then
expect($router.push).toHaveBeenCalledWith('/fr/')
expect($router.replace).toHaveBeenCalledWith('/fr/')
const localeLinks = wrapper.findAll('locale-link-stub')
expect(localeLinks.length).toBe(0)
})
})
})
Expand All @@ -51,7 +64,10 @@ describe('Index Page', () => {
test('returns no locale', () => {
// given
document.cookie = ''
const wrapper = shallowMount(Index, { mocks: { $router } })
const wrapper = shallowMount(Index, {
mocks: { $router },
stubs: ['client-only', 'pix-image', 'locale-link'],
})

// when
const chosenLocale = wrapper.vm.getLocaleFromCookie()
Expand All @@ -65,7 +81,10 @@ describe('Index Page', () => {
test('returns the proper locale', () => {
// given
document.cookie = 'foo=bar; locale=fr'
const wrapper = shallowMount(Index, { mocks: { $router } })
const wrapper = shallowMount(Index, {
mocks: { $router },
stubs: ['client-only', 'pix-image', 'locale-link'],
})

// when
const chosenLocale = wrapper.vm.getLocaleFromCookie()
Expand All @@ -79,7 +98,10 @@ describe('Index Page', () => {
test('cookie value is ignored', () => {
// given
document.cookie = 'foo=bar; locale=1234-crafted-cookie'
const wrapper = shallowMount(Index, { mocks: { $router } })
const wrapper = shallowMount(Index, {
mocks: { $router },
stubs: ['client-only', 'pix-image', 'locale-link'],
})

// when
const chosenLocale = wrapper.vm.getLocaleFromCookie()
Expand Down