feat/analytics #9
@ -23,7 +23,9 @@ declare global {
|
|||||||
analytics: boolean;
|
analytics: boolean;
|
||||||
customization: boolean;
|
customization: boolean;
|
||||||
};
|
};
|
||||||
theme: Theme;
|
settings: {
|
||||||
|
theme: Theme;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
|
import type { PageContext } from "vike/types";
|
||||||
|
|
||||||
import { getTelefuncContext } from "@/lib/getTelefuncContext";
|
import { getTelefuncContext } from "@/lib/getTelefuncContext";
|
||||||
import { CookieParser } from "@/services/CookieParser";
|
import { CookieParser } from "@/services/CookieParser";
|
||||||
|
|
||||||
export async function onUpdateConsentCookie(cookieName: string, cookieValue: boolean) {
|
type ConsentCookies = keyof PageContext["cookies"]["consent"];
|
||||||
|
|
||||||
|
export async function onUpdateConsentCookie(cookieName: ConsentCookies, cookieValue: boolean) {
|
||||||
const context = getTelefuncContext();
|
const context = getTelefuncContext();
|
||||||
console.log(`Updating cookie ${cookieName} to ${cookieValue}`);
|
console.log(`Updating cookie ${cookieName} to ${cookieValue}`);
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
|
import { onUpdateConsentCookie } from "./LayoutDefault.telefunc";
|
||||||
import { MobileNavigation } from "@syntax/MobileNavigation";
|
import { MobileNavigation } from "@syntax/MobileNavigation";
|
||||||
import { usePageContext } from "vike-react/usePageContext";
|
import { usePageContext } from "vike-react/usePageContext";
|
||||||
import { ThemeProvider } from "@/providers/ThemeProvider";
|
import { ThemeProvider } from "@/providers/ThemeProvider";
|
||||||
|
import { ToastContainer, toast } from "react-toastify";
|
||||||
import { ThemeSelector } from "@syntax/ThemeSelector";
|
import { ThemeSelector } from "@syntax/ThemeSelector";
|
||||||
import { Button } from "@/components/syntax/Button";
|
import { Button } from "@/components/syntax/Button";
|
||||||
import { Toggle } from "@/components/common/Toggle";
|
import { Toggle } from "@/components/common/Toggle";
|
||||||
import { clientOnly } from "vike-react/clientOnly";
|
import { clientOnly } from "vike-react/clientOnly";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { ToastContainer } from "react-toastify";
|
|
||||||
import { Navigation } from "@syntax/Navigation";
|
import { Navigation } from "@syntax/Navigation";
|
||||||
import { Link } from "@/components/common/Link";
|
import { Link } from "@/components/common/Link";
|
||||||
import { reload } from "vike/client/router";
|
import { reload } from "vike/client/router";
|
||||||
@ -108,7 +109,14 @@ function CookieModal() {
|
|||||||
checked={consentCookies.analytics}
|
checked={consentCookies.analytics}
|
||||||
onChange={(checked) => {
|
onChange={(checked) => {
|
||||||
setConsentCookies({ ...consentCookies, analytics: checked });
|
setConsentCookies({ ...consentCookies, analytics: checked });
|
||||||
reload();
|
|
||||||
|
toast
|
||||||
|
.promise(onUpdateConsentCookie("analytics", checked), {
|
||||||
|
pending: "Mise à jour des cookies...",
|
||||||
|
success: "Cookies mis à jour !",
|
||||||
|
error: "Erreur lors de la mise à jour des cookies.",
|
||||||
|
})
|
||||||
|
.finally(reload);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@ -117,8 +125,15 @@ function CookieModal() {
|
|||||||
label="Cookie de personnalisation (thème)"
|
label="Cookie de personnalisation (thème)"
|
||||||
checked={consentCookies.customization}
|
checked={consentCookies.customization}
|
||||||
onChange={(checked) => {
|
onChange={(checked) => {
|
||||||
setConsentCookies((prev) => ({ ...prev, customization: checked }));
|
setConsentCookies({ ...consentCookies, analytics: checked });
|
||||||
reload();
|
|
||||||
|
toast
|
||||||
|
.promise(onUpdateConsentCookie("customization", checked), {
|
||||||
|
pending: "Mise à jour des cookies...",
|
||||||
|
success: "Cookies mis à jour !",
|
||||||
|
error: "Erreur lors de la mise à jour des cookies.",
|
||||||
|
})
|
||||||
|
.finally(reload);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -186,7 +201,7 @@ export default function DefaultLayout({ children }: { children: React.ReactNode
|
|||||||
const isHomePage = urlPathname === "/";
|
const isHomePage = urlPathname === "/";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider defaultTheme={cookies.theme}>
|
<ThemeProvider defaultTheme={cookies.settings.theme}>
|
||||||
<CookieModal />
|
<CookieModal />
|
||||||
|
|
||||||
<div className="flex w-full flex-col font-sans">
|
<div className="flex w-full flex-col font-sans">
|
||||||
|
|||||||
@ -16,10 +16,12 @@ export const vikeHandler: Get<[], UniversalHandler> = () => async (request, cont
|
|||||||
headersOriginal: request.headers,
|
headersOriginal: request.headers,
|
||||||
cookies: {
|
cookies: {
|
||||||
consent: {
|
consent: {
|
||||||
analytics: cookies.get("consent-analytics", Boolean) || false,
|
analytics: cookies.get("analytics", Boolean) || false,
|
||||||
customization: cookies.get("consent-customization", Boolean) || false,
|
customization: cookies.get("customization", Boolean) || false,
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
theme: cookies.get("theme") || "light",
|
||||||
},
|
},
|
||||||
theme: cookies.get("theme") || "light",
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const pageContext = await renderPage(pageContextInit);
|
const pageContext = await renderPage(pageContextInit);
|
||||||
|
|||||||
@ -1,5 +1,12 @@
|
|||||||
|
import type { PageContext } from "vike/types";
|
||||||
|
|
||||||
import { FastifyReply } from "fastify";
|
import { FastifyReply } from "fastify";
|
||||||
|
|
||||||
|
type ConsentCookies = keyof PageContext["cookies"]["consent"];
|
||||||
|
type SettingsCookie = keyof PageContext["cookies"]["settings"];
|
||||||
|
|
||||||
|
type CookieKeys = ConsentCookies | SettingsCookie;
|
||||||
|
|
||||||
export class CookieParser {
|
export class CookieParser {
|
||||||
private rawCookies: string;
|
private rawCookies: string;
|
||||||
private cookies: Record<string, string>;
|
private cookies: Record<string, string>;
|
||||||
@ -21,14 +28,16 @@ export class CookieParser {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key: string, formatter?: Function): string | undefined {
|
get(key: CookieKeys, formatter?: Function): string | undefined {
|
||||||
const value = this.cookies[key];
|
const value = this.cookies[key];
|
||||||
|
|
||||||
|
console.log({ key, value });
|
||||||
|
|
||||||
if (formatter) return formatter(value);
|
if (formatter) return formatter(value);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
set(reply: FastifyReply, key: string, value: string, daysDuration = 30): FastifyReply {
|
static set(reply: FastifyReply, key: CookieKeys, value: string, daysDuration = 30): FastifyReply {
|
||||||
const options = {
|
const options = {
|
||||||
path: "/",
|
path: "/",
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
@ -37,7 +46,6 @@ export class CookieParser {
|
|||||||
};
|
};
|
||||||
|
|
||||||
reply.setCookie(key, value, options);
|
reply.setCookie(key, value, options);
|
||||||
this.cookies[key] = value;
|
|
||||||
|
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user