Use Cookies Instead of Query Params

This commit is contained in:
Sam Poder 2023-08-15 13:02:25 -07:00
parent 00e2a78cdb
commit af38e43148
4 changed files with 35 additions and 10 deletions

View file

@ -10,17 +10,19 @@ import {
Box
} from 'theme-ui'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import useForm from '../../lib/use-form'
import Submit from '../submit'
import { getCookie, hasCookie } from 'cookies-next';
const JoinForm = ({ sx = {} }) => {
const router = useRouter()
const { status, formProps, useField } = useForm('/api/join/', null, {
clearOnSubmit: 5000,
method: 'POST',
initData: router.query.continent
initData: hasCookie("continent")
? {
continent: router.query.continent,
continent: getCookie("continent"),
reason: router.query.reason,
event: router.query.event
}

View file

@ -4,14 +4,12 @@ const partners = ['gb_help_desk']
export function middleware(request) {
if (request.nextUrl.pathname.startsWith('/slack')) {
let url = request.nextUrl
if (!url.searchParams.get('continent')) {
let continent = country.findByIso2(request.geo.country || 'AU').continent
if (continent === 'Oceania') {
continent = 'Australia'
}
url.searchParams.set('continent', continent)
return NextResponse.redirect(url)
let continent = country.findByIso2(request.geo.country || 'AU').continent
if (continent === 'Oceania') {
continent = 'Australia'
}
const response = NextResponse.next();
response.cookies.set("continent", continent || "");
return response;
}
}

View file

@ -29,6 +29,7 @@
"animated-value": "^0.2.4",
"animejs": "^3.2.1",
"axios": "^1.4.0",
"cookies-next": "^2.1.2",
"country-list-js": "^3.1.7",
"cursor-effects": "^1.0.11",
"devtools-detect": "^4.0.1",

View file

@ -1190,6 +1190,11 @@
resolved "https://registry.npmjs.org/@types/btoa-lite/-/btoa-lite-1.0.0.tgz"
integrity sha512-wJsiX1tosQ+J5+bY5LrSahHxr2wT+uME5UDwdN1kg4frt40euqA+wzECkmq4t5QbveHiJepfdThgQrPw6KiSlg==
"@types/cookie@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.1.tgz#bfd02c1f2224567676c1545199f87c3a861d878d"
integrity sha512-XW/Aa8APYr6jSVVA1y/DEIZX0/GMKLEVekNG727R8cs56ahETkRAy/3DR7+fJyh7oUgGwNQaRfXCun0+KbWY7Q==
"@types/hast@^2.0.0":
version "2.3.1"
resolved "https://registry.npmjs.org/@types/hast/-/hast-2.3.1.tgz"
@ -1226,6 +1231,11 @@
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz"
integrity sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==
"@types/node@^16.10.2":
version "16.18.40"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.40.tgz#968d64746d20cac747a18ca982c0f1fe518c031c"
integrity sha512-+yno3ItTEwGxXiS/75Q/aHaa5srkpnJaH+kdkTVJ3DtJEwv92itpKbxU+FjPoh2m/5G9zmUQfrL4A4C13c+iGA==
"@types/parse-json@^4.0.0":
version "4.0.0"
resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz"
@ -1758,6 +1768,20 @@ convert-source-map@^1.7.0:
dependencies:
safe-buffer "~5.1.1"
cookie@^0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
cookies-next@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/cookies-next/-/cookies-next-2.1.2.tgz#78fe2f3e7b68eb0e1c6682c9e1f4e94ce4d09904"
integrity sha512-czxcfqVaQlo0Q/3xMgp/2jpspsuLJrIm6D37wlmibP3DAcYT315c8UxQmDMohhAT/GRWpaHzpDEFANBjzTFQGg==
dependencies:
"@types/cookie" "^0.4.1"
"@types/node" "^16.10.2"
cookie "^0.4.0"
core-js-pure@^3.20.2:
version "3.20.2"
resolved "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.20.2.tgz"