Set cookie on language change

This commit is contained in:
Philip 2023-02-18 16:51:39 +01:00
parent 9a39328468
commit 138e0d47c9
5 changed files with 58 additions and 4 deletions

23
package-lock.json generated
View file

@ -15,6 +15,7 @@
"next": "13.1.6",
"next-intl": "^2.10.2",
"next-pwa": "^5.6.0",
"nookies": "^2.5.2",
"quagga": "^0.12.1",
"react": "18.2.0",
"react-dom": "18.2.0",
@ -2687,6 +2688,14 @@
"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz",
"integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A=="
},
"node_modules/cookie": {
"version": "0.4.2",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz",
"integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/core-js-compat": {
"version": "3.27.2",
"resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.27.2.tgz",
@ -4424,6 +4433,15 @@
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.10.tgz",
"integrity": "sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w=="
},
"node_modules/nookies": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/nookies/-/nookies-2.5.2.tgz",
"integrity": "sha512-x0TRSaosAEonNKyCrShoUaJ5rrT5KHRNZ5DwPCuizjgrnkpE5DRf3VL7AyyQin4htict92X1EQ7ejDbaHDVdYA==",
"dependencies": {
"cookie": "^0.4.1",
"set-cookie-parser": "^2.4.6"
}
},
"node_modules/normalize-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
@ -5117,6 +5135,11 @@
"randombytes": "^2.1.0"
}
},
"node_modules/set-cookie-parser": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.5.1.tgz",
"integrity": "sha512-1jeBGaKNGdEq4FgIrORu/N570dwoPYio8lSoYLWmX7sQ//0JY08Xh9o5pBcgmHQ/MbsYp/aZnOe1s1lIsbLprQ=="
},
"node_modules/side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",

View file

@ -16,6 +16,7 @@
"next": "13.1.6",
"next-intl": "^2.10.2",
"next-pwa": "^5.6.0",
"nookies": "^2.5.2",
"quagga": "^0.12.1",
"react": "18.2.0",
"react-dom": "18.2.0",

View file

@ -1,6 +1,7 @@
import Link from "next/link";
import Image from "next/image";
import { useRouter } from "next/router";
import { setCookie } from 'nookies';
import { GetStaticPropsContext } from "next";
import { useTranslations } from "next-intl";
import Nav from "@/components/nav";
@ -12,6 +13,15 @@ import OLEDMode from "@/components/elements/contents/oledmode";
export default function more() {
const router = useRouter();
const t = useTranslations("More");
function handleLanguageChange(locale: string) {
setCookie(null, "NEXT_LOCALE", locale, {
maxAge: 30 * 24 * 60 * 60, // 30 days
path: "/",
});
router.push("/more", undefined, { locale });
}
return (
<>
<div id="modal-root"></div>
@ -122,7 +132,7 @@ export default function more() {
/>
<h1>{t('language')}</h1>
</span>
<Link className="nolink" href="/more" locale="en">
<Link className="nolink" href="/more" locale="en" onClick={() => handleLanguageChange('en')}>
<div
className={router.locale === "en" ? "option active" : "option"}
>
@ -135,7 +145,7 @@ export default function more() {
<span className="price">{t('english')}</span>
</div>
</Link>
<Link className="nolink" href="/more" locale="de">
<Link className="nolink" href="/more" locale="de" onClick={() => handleLanguageChange('de')}>
<div
className={router.locale === "de" ? "option active" : "option"}
>
@ -148,7 +158,7 @@ export default function more() {
<span className="price">{t('german')}</span>
</div>
</Link>
<Link className="nolink" href="/more" locale="es">
<Link className="nolink" href="/more" locale="es" onClick={() => handleLanguageChange('es')}>
<div
className={router.locale === "es" ? "option active" : "option"}
>
@ -161,7 +171,7 @@ export default function more() {
<span className="price">{t('spanish')}</span>
</div>
</Link>
<Link className="nolink" href="/more" locale="fr">
<Link className="nolink" href="/more" locale="fr" onClick={() => handleLanguageChange('fr')}>
<div
className={router.locale === "fr" ? "option active" : "option"}
>
@ -174,6 +184,7 @@ export default function more() {
<span className="price">{t('french')}</span>
</div>
</Link>
<span className="info" id="cookieinfo">{t('thissetsacookie')}</span>
</ModalWrapper>
<div className="Grid-cell icons">
<span

View file

@ -291,4 +291,5 @@
.icon-kofi:before {
content: "\f097";
margin-right: 0.7rem;
}

View file

@ -1679,6 +1679,11 @@ convert-source-map@^1.7.0:
resolved "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz"
integrity sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==
cookie@^0.4.1:
version "0.4.2"
resolved "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
core-js-compat@^3.25.1:
version "3.27.2"
resolved "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.27.2.tgz"
@ -2788,6 +2793,14 @@ node-releases@^2.0.8:
resolved "https://registry.npmjs.org/node-releases/-/node-releases-2.0.10.tgz"
integrity sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w==
nookies@^2.5.2:
version "2.5.2"
resolved "https://registry.npmjs.org/nookies/-/nookies-2.5.2.tgz"
integrity sha512-x0TRSaosAEonNKyCrShoUaJ5rrT5KHRNZ5DwPCuizjgrnkpE5DRf3VL7AyyQin4htict92X1EQ7ejDbaHDVdYA==
dependencies:
cookie "^0.4.1"
set-cookie-parser "^2.4.6"
normalize-path@^3.0.0, normalize-path@~3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"
@ -3225,6 +3238,11 @@ serialize-javascript@^6.0.0:
dependencies:
randombytes "^2.1.0"
set-cookie-parser@^2.4.6:
version "2.5.1"
resolved "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.5.1.tgz"
integrity sha512-1jeBGaKNGdEq4FgIrORu/N570dwoPYio8lSoYLWmX7sQ//0JY08Xh9o5pBcgmHQ/MbsYp/aZnOe1s1lIsbLprQ==
side-channel@^1.0.4:
version "1.0.4"
resolved "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz"