feat: Update Toggle component styling and add onUpdateConsentCookie function

This commit is contained in:
Gauthier Daniels 2025-04-18 14:37:41 +02:00
parent fb2d767a37
commit 438d3e62e4
5 changed files with 50 additions and 4 deletions

View File

@ -26,7 +26,7 @@ export function Toggle(props: ToggleProps) {
<span <span
className={clsx( className={clsx(
"h-4 w-4 rounded-full bg-white shadow-md transition-transform duration-200 ease-in-out z-10", "h-4 w-4 rounded-full bg-white shadow-md transition-transform duration-200 ease-in-out z-10",
props.checked ? "translate-x-full" : "translate-x-0", props.checked ? "translate-x-[calc(100%+.25em)]" : "translate-x-1",
)} )}
/> />
<span <span

View File

@ -0,0 +1,12 @@
import { getTelefuncContext } from "@/lib/getTelefuncContext";
import { CookieParser } from "@/services/CookieParser";
export async function onUpdateConsentCookie(cookieName: string, cookieValue: boolean) {
const context = getTelefuncContext();
console.log(`Updating cookie ${cookieName} to ${cookieValue}`);
const { reply } = context;
CookieParser.set(reply, cookieName, cookieValue.toString(), 365);
return { ok: true, message: "Updated consent cookie" };
}

View File

@ -105,9 +105,9 @@ function CookieModal() {
<Toggle <Toggle
id="cookies-analytics" id="cookies-analytics"
label="Cookies d'analyse (Umami et Google Analytics)" label="Cookies d'analyse (Umami et Google Analytics)"
checked={cookies.consent.analytics} checked={consentCookies.analytics}
onChange={(checked) => { onChange={(checked) => {
setConsentCookies((prev) => ({ ...prev, analytics: checked })); setConsentCookies({ ...consentCookies, analytics: checked });
reload(); reload();
}} }}
/> />
@ -115,7 +115,7 @@ function CookieModal() {
<Toggle <Toggle
id="cookies-customization" id="cookies-customization"
label="Cookie de personnalisation (thème)" label="Cookie de personnalisation (thème)"
checked={cookies.consent.customization} checked={consentCookies.customization}
onChange={(checked) => { onChange={(checked) => {
setConsentCookies((prev) => ({ ...prev, customization: checked })); setConsentCookies((prev) => ({ ...prev, customization: checked }));
reload(); reload();

View File

@ -0,0 +1,18 @@
import type { FastifyRequest, FastifyReply } from "fastify";
import { getContext } from "telefunc";
export function getTelefuncContext() {
const context = getContext<{
fastify: {
request: FastifyRequest;
reply: FastifyReply;
};
}>();
return {
...context,
reply: context.fastify.reply,
request: context.fastify.request,
};
}

View File

@ -1,3 +1,5 @@
import { FastifyReply } from "fastify";
export class CookieParser { export class CookieParser {
private rawCookies: string; private rawCookies: string;
private cookies: Record<string, string>; private cookies: Record<string, string>;
@ -25,4 +27,18 @@ export class CookieParser {
if (formatter) return formatter(value); if (formatter) return formatter(value);
return value; return value;
} }
set(reply: FastifyReply, key: string, value: string, daysDuration = 30): FastifyReply {
const options = {
path: "/",
httpOnly: true,
secure: process.env.NODE_ENV === "production",
expires: new Date(Date.now() + 60 * 60 * 24 * daysDuration * 1000),
};
reply.setCookie(key, value, options);
this.cookies[key] = value;
return reply;
}
} }