fix: pref not in sync at load
This commit is contained in:
@@ -14,32 +14,11 @@ interface PreferencesContextType {
|
|||||||
|
|
||||||
const PreferencesContext = createContext<PreferencesContextType | undefined>(undefined);
|
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 }) {
|
export function PreferencesProvider({ children }: { children: React.ReactNode }) {
|
||||||
const [preferences, setPreferences] = useState<UserPreferences>(defaultPreferences);
|
const [preferences, setPreferences] = useState<UserPreferences>(defaultPreferences);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
const fetchPreferences = async () => {
|
const fetchPreferences = async () => {
|
||||||
if (!isAuthenticated()) {
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/preferences");
|
const response = await fetch("/api/preferences");
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -63,9 +42,6 @@ export function PreferencesProvider({ children }: { children: React.ReactNode })
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const updatePreferences = async (newPreferences: Partial<UserPreferences>) => {
|
const updatePreferences = async (newPreferences: Partial<UserPreferences>) => {
|
||||||
if (!isAuthenticated()) {
|
|
||||||
throw new AppError(ERROR_CODES.AUTH.UNAUTHENTICATED);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/preferences", {
|
const response = await fetch("/api/preferences", {
|
||||||
|
|||||||
Reference in New Issue
Block a user