From 852ac9ad0d8760f2ecf5d534df1d8b91da8d9cea Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Wed, 7 May 2025 10:42:23 +0300 Subject: [PATCH] feat: real login and logout --- src/components/menu.tsx | 51 ++++++++++++++----------- src/pages/profile_settings.tsx | 68 +++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/components/menu.tsx b/src/components/menu.tsx index 94522be..3549e9a 100644 --- a/src/components/menu.tsx +++ b/src/components/menu.tsx @@ -4,8 +4,22 @@ import { FunctionComponent, h } from "preact"; import { useLocation } from "preact-iso"; import { tv } from "tailwind-variants"; import classes from "./menu.module.scss"; -import { calculatePoints, getCurrentStatus } from "@/utils/status-system"; import { useEffect, useState } from "preact/hooks"; +import apiClient from "@/services/api"; +import { useAppContext } from "@/providers/AuthProvider"; + +interface UserProfile { + username: string; + email: string; + status: string; + avatar_url: string | null; + telegram_notifications: boolean; + telegram_chat_id: string; +} + +interface UserSettings { + profile: UserProfile; +} interface MenuItemProps { title: string; @@ -36,32 +50,27 @@ const MenuItem: FunctionComponent = ({ title, link, icon }: MenuI ); }; - const Avatar: FunctionComponent = () => { const [status, setStatus] = useState(""); + const [username, setUsername] = useState(""); const { route, path } = useLocation(); + const { isLoggedIn } = useAppContext(); useEffect(() => { - const updateStatus = () => { - const tasks = JSON.parse(localStorage.getItem("tasks") || "[]"); - const completedTasks = tasks.filter((task: { checked: boolean }) => task.checked).length; - const points = calculatePoints(completedTasks); - setStatus(getCurrentStatus(points)); - }; - - // Initial update - updateStatus(); - - // Update when tasks change - const handleStorage = (e: StorageEvent) => { - if (e.key === "tasks") { - updateStatus(); + const fetchUserData = async () => { + try { + const response = await apiClient("/api/settings/view_settings/", { method: "GET" }, isLoggedIn); + setUsername(response.profile.username); + setStatus(response.profile.status); + } catch (error) { + console.error("Failed to fetch user data:", error); } }; - - window.addEventListener('storage', handleStorage); - return () => window.removeEventListener('storage', handleStorage); - }, []); + + if (isLoggedIn.value) { + fetchUserData(); + } + }, [isLoggedIn.value]); return (