fix: pref not in sync at load

This commit is contained in:
Julien Froidefond
2025-03-04 21:54:24 +01:00
parent 15f71cd9b2
commit 837b029a39

View File

@@ -14,32 +14,11 @@ interface PreferencesContextType {
const PreferencesContext = createContext<PreferencesContextType | undefined>(undefined);
const isAuthenticated = (): boolean => {
if (typeof window === "undefined") return false;
const userCookie = document.cookie.split("; ").find((row) => row.startsWith("stripUser="));
if (!userCookie) return false;
try {
const userData = JSON.parse(atob(userCookie.split("=")[1]));
return userData?.authenticated === true;
} catch (error) {
console.error("Error parsing user cookie:", error);
return false;
}
};
export function PreferencesProvider({ children }: { children: React.ReactNode }) {
const [preferences, setPreferences] = useState<UserPreferences>(defaultPreferences);
const [isLoading, setIsLoading] = useState(true);
const fetchPreferences = async () => {
if (!isAuthenticated()) {
setIsLoading(false);
return;
}
try {
const response = await fetch("/api/preferences");
if (!response.ok) {
@@ -63,9 +42,6 @@ export function PreferencesProvider({ children }: { children: React.ReactNode })
}, []);
const updatePreferences = async (newPreferences: Partial<UserPreferences>) => {
if (!isAuthenticated()) {
throw new AppError(ERROR_CODES.AUTH.UNAUTHENTICATED);
}
try {
const response = await fetch("/api/preferences", {