From 5069cf56b143a5f05c79a10f8ba9919d756661df Mon Sep 17 00:00:00 2001 From: rikhall1515 Date: Sun, 5 May 2024 22:11:34 +0200 Subject: [PATCH 01/10] refactor: change getCookieConsentValue and add input type --- lib/cookie/utils.ts | 20 +++++++++++++++++--- types/cookie.ts | 6 ++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 types/cookie.ts diff --git a/lib/cookie/utils.ts b/lib/cookie/utils.ts index c3d2273..ea468c9 100644 --- a/lib/cookie/utils.ts +++ b/lib/cookie/utils.ts @@ -8,16 +8,30 @@ import { defaultCookieConsentName, defaultCookieExpires, SAME_SITE_OPTIONS } fro * to: https://web.dev/samesite-cookie-recipes/#handling-incompatible-clients * @param {*} name optional name of the cookie */ -export const getCookieConsentValue = (name = defaultCookieConsentName) => { +export const getCookieConsentValue = ( + name = defaultCookieConsentName +): { preferences: boolean; analytics: boolean; advertising: boolean; consent: boolean } => { let cookieValue = Cookies.get(name); // if the cookieValue is undefined check for the legacy cookie if (cookieValue === undefined) { cookieValue = Cookies.get(getLegacyCookieName(name)); } - if (cookieValue === undefined) return [false, false, false]; + if (cookieValue === undefined) { + return { + preferences: false, + analytics: false, + advertising: false, + consent: false, + }; + } const values = cookieValue.split(":")[1]; - const booleans = Array.from(values).map((v) => v === "1"); + const booleans = { + preferences: values[0] === "1", + analytics: values[1] === "1", + advertising: values[2] === "1", + consent: true, + }; return booleans; }; diff --git a/types/cookie.ts b/types/cookie.ts new file mode 100644 index 0000000..c2a7543 --- /dev/null +++ b/types/cookie.ts @@ -0,0 +1,6 @@ +export type CookieInputs = { + necessary: boolean; + preferences: boolean; + analytics: boolean; + advertising: boolean; +}; From f2a46fc5d87ca0ea76e1164f0b8c86e6d6e6c0b4 Mon Sep 17 00:00:00 2001 From: rikhall1515 Date: Sun, 5 May 2024 22:25:29 +0200 Subject: [PATCH 02/10] refactor(context): refactor context and change placement of context Introduces cookie form context. Places context providers as low into the tree as possible. --- app/(private)/dashboard/layout.tsx | 5 +- app/(public)/layout.tsx | 7 +- components/cookies/index.tsx | 5 +- components/header/index.tsx | 18 ++-- context/cookie.tsx | 136 ------------------------ context/cookie/form.tsx | 95 +++++++++++++++++ context/cookie/index.tsx | 162 +++++++++++++++++++++++++++++ context/index.tsx | 8 +- 8 files changed, 282 insertions(+), 154 deletions(-) delete mode 100644 context/cookie.tsx create mode 100644 context/cookie/form.tsx create mode 100644 context/cookie/index.tsx diff --git a/app/(private)/dashboard/layout.tsx b/app/(private)/dashboard/layout.tsx index bee429f..c24eeaa 100644 --- a/app/(private)/dashboard/layout.tsx +++ b/app/(private)/dashboard/layout.tsx @@ -1,6 +1,7 @@ "use client"; import Sidebar from "@/app/(private)/_components/sidebar"; import CookieBanner from "@/components/cookies"; +import CookieContextProvider from "@/context/cookie"; import { useDashboardContext } from "@/context/dashboard"; import { cn } from "@/lib/utils"; export default function DashboardLayout({ children }: { children: React.ReactNode }) { @@ -17,7 +18,9 @@ export default function DashboardLayout({ children }: { children: React.ReactNod > {children} - + + + ); } diff --git a/app/(public)/layout.tsx b/app/(public)/layout.tsx index 1732f05..5f49f56 100644 --- a/app/(public)/layout.tsx +++ b/app/(public)/layout.tsx @@ -1,14 +1,17 @@ import CookieBanner from "@/components/cookies"; import Footer from "@/components/footer"; import Header from "@/components/header"; +import CookieContextProvider from "@/context/cookie"; export default function PublicLayout({ children }: { children: React.ReactNode }) { return ( <>
{children}
-