From 542d16227f1c5c42ebcb528dcde97060860d1f0e Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 16:50:37 +0200 Subject: [PATCH] feat: Add function to update theme cookie --- app/providers/ThemeProvider.telefunc.ts | 13 +++++++++++++ app/providers/ThemeProvider.tsx | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/providers/ThemeProvider.telefunc.ts b/app/providers/ThemeProvider.telefunc.ts index e69de29..3dfc4a1 100644 --- a/app/providers/ThemeProvider.telefunc.ts +++ b/app/providers/ThemeProvider.telefunc.ts @@ -0,0 +1,13 @@ +import type { Theme } from "@/contexts/ThemeContext"; + +import { getTelefuncContext } from "@/lib/getTelefuncContext"; +import { CookieParser } from "@/services/CookieParser"; + +export async function onUpdateThemeCookie(value: Theme) { + const context = getTelefuncContext(); + const { reply } = context; + + CookieParser.set(reply, "theme", value, 365); + + return { ok: true, message: "Updated theme cookie", value }; +} diff --git a/app/providers/ThemeProvider.tsx b/app/providers/ThemeProvider.tsx index 8220459..79961e4 100644 --- a/app/providers/ThemeProvider.tsx +++ b/app/providers/ThemeProvider.tsx @@ -1,6 +1,8 @@ +import { onUpdateThemeCookie } from "@/providers/ThemeProvider.telefunc"; import { ThemeContext, type Theme } from "@/contexts/ThemeContext"; import { usePageContext } from "vike-react/usePageContext"; import React, { useEffect, useState } from "react"; +import { toast } from "react-toastify"; type ThemeProviderProps = { children: React.ReactNode; @@ -18,8 +20,9 @@ export function ThemeProvider(props: ThemeProviderProps) { rootElement.classList.toggle("light", theme === "light"); if (cookies.consent.customization) { - console.log(`ThemeProvider: ${theme}`); - // TODO: update the theme in the cookies + onUpdateThemeCookie(theme).catch(() => { + toast.error("Erreur lors de la mise à jour du cookie de thème"); + }); } }, [theme]);