From 46815de81aab3cca93a7f44309029443e1c372e1 Mon Sep 17 00:00:00 2001 From: AndcoolSystems Date: Tue, 30 Jul 2024 17:26:28 +0300 Subject: [PATCH] theme global save --- src/app/modules/header.module.tsx | 3 ++- src/app/modules/me.module.tsx | 18 +++++------------- src/app/modules/theme_select.module.tsx | 7 +++++++ src/app/users/[name]/page.tsx | 12 ++++-------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/app/modules/header.module.tsx b/src/app/modules/header.module.tsx index 1d77af0..00ec1fd 100644 --- a/src/app/modules/header.module.tsx +++ b/src/app/modules/header.module.tsx @@ -20,7 +20,8 @@ export interface Query { joined_at: Date; banner_color: string; has_unreaded_notifications: Boolean, - permissions: string[] + permissions: string[], + profile_theme: number } const Header = (): JSX.Element => { diff --git a/src/app/modules/me.module.tsx b/src/app/modules/me.module.tsx index c641bac..ae104ae 100644 --- a/src/app/modules/me.module.tsx +++ b/src/app/modules/me.module.tsx @@ -54,9 +54,7 @@ export const Me = ({ children, user_data }: { children: JSX.Element, user_data?: const [islogged, setIsLogged] = useState(false); const pathname = usePathname(); const path = pathname.split('/')[pathname.split('/').length - 1]; - const cookies = useCookies(); - const initial_theme = Number(cookies.get('theme')) || 0 - const [theme, setTheme] = useState(initial_theme); + const [theme, setTheme] = useState(user_data?.profile_theme || 0); const { data, isLoading, isError } = useQuery({ queryKey: [`user_${user_data?.username}`], @@ -64,7 +62,9 @@ export const Me = ({ children, user_data }: { children: JSX.Element, user_data?: queryFn: async () => { if (!user_data) { const res = await authApi.get("/user/me", { withCredentials: true }); - return res.data as Query; + const data = res.data as Query; + setTheme(data.profile_theme); + return data; } else { return user_data; } @@ -75,10 +75,6 @@ export const Me = ({ children, user_data }: { children: JSX.Element, user_data?: setIsLogged(true); } - useEffect(() => { - cookies.set('theme', theme.toString(), { expires: (365 * 10) }); - }, [theme]); - let background; switch (theme) { case 1: @@ -92,10 +88,6 @@ export const Me = ({ children, user_data }: { children: JSX.Element, user_data?: break; } - if (!!user_data) { - background = ; - } - return (
@@ -103,7 +95,7 @@ export const Me = ({ children, user_data }: { children: JSX.Element, user_data?:
{background} - {!user_data && } + {!user_data && }
{!user_data &&
diff --git a/src/app/modules/theme_select.module.tsx b/src/app/modules/theme_select.module.tsx index 33a0450..96deaa4 100644 --- a/src/app/modules/theme_select.module.tsx +++ b/src/app/modules/theme_select.module.tsx @@ -4,6 +4,7 @@ import { useEffect, useState } from 'react'; import { CSSTransition } from 'react-transition-group'; import Styles from '@/app/styles/theme_selector.module.css'; import Image from 'next/image'; +import { authApi } from '../api.module'; interface MenuProps { initialValue?: number; @@ -25,11 +26,17 @@ const getIcon = (theme: number) => { const Menu = ({ initialValue, color_available, onChange }: MenuProps) => { const [expanded, setExpanded] = useState(false); const [theme, setTheme] = useState(initialValue); + const [firstLoad, setFirstLoad] = useState(false); useEffect(() => { onChange(theme); + !firstLoad ? setFirstLoad(true) : theme !== initialValue && authApi.post('/user/me/profile_theme', { theme: theme }); }, [theme]); + useEffect(() => { + setTheme(initialValue); + }, [initialValue]) + return (