From d224cbeb3807ecad5b9474db8150fac6bd6d995b Mon Sep 17 00:00:00 2001 From: Samandar Turgunboyev Date: Fri, 28 Nov 2025 19:17:21 +0500 Subject: [PATCH] added socket --- src/shared/hooks/user-info.ts | 4 +++ src/token-layaout.tsx | 50 ++++++++++++++++++++++++++++++++--- vite.config.ts | 3 ++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/shared/hooks/user-info.ts b/src/shared/hooks/user-info.ts index d4a5b1b..978fa69 100644 --- a/src/shared/hooks/user-info.ts +++ b/src/shared/hooks/user-info.ts @@ -11,6 +11,7 @@ type State = { first_name: string; last_name: string; active: boolean; + id: number; } | null; }; @@ -26,6 +27,7 @@ type Actions = { first_name: string; last_name: string; active: boolean; + id: number; } | null, ) => void; }; @@ -37,6 +39,7 @@ export const userInfoStore = create((set) => ({ first_name: string; last_name: string; active: boolean; + id: number; } | null, ) => set(() => ({ loginUser: login })), user: { @@ -44,6 +47,7 @@ export const userInfoStore = create((set) => ({ last_name: "", user_id: "", active: false, + id: null, }, addedUser: (user) => set(() => ({ user })), })); diff --git a/src/token-layaout.tsx b/src/token-layaout.tsx index 3c87cb8..e6c00a5 100644 --- a/src/token-layaout.tsx +++ b/src/token-layaout.tsx @@ -5,11 +5,12 @@ import LoginForm from "@/features/auth/ui/login"; import { userInfoStore } from "@/shared/hooks/user-info"; import { getToken, removeToken, saveToken } from "@/shared/lib/cookie"; import { useMutation } from "@tanstack/react-query"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; const TokenLayout = ({ children }: { children: React.ReactNode }) => { const { addedUser, loginUser, setLoginUser } = userInfoStore(); - const token = getToken(); + const gettoken = getToken(); + const [token, setToken] = useState(null); const { mutate: login, isPending } = useMutation({ mutationFn: (body: { telegram_id: string }) => auth_api.login(body), @@ -19,6 +20,7 @@ const TokenLayout = ({ children }: { children: React.ReactNode }) => { active: user.is_active, first_name: user.first_name, last_name: user.last_name, + id: user.id, }); if (user.token) saveToken(user.token); }, @@ -44,6 +46,48 @@ const TokenLayout = ({ children }: { children: React.ReactNode }) => { } }, [addedUser, login]); + useEffect(() => { + if (loginUser === null) return; + + const socket = new WebSocket( + `wss://api.meridynpharma.com/ws/user_activation/${loginUser.id}/`, + ); + + socket.onopen = () => { + console.log("WebSocket connected ✅"); + }; + + socket.onmessage = (event) => { + try { + const data = JSON.parse(event.data); + if (data.token) { + saveToken(data.token); + setToken(data.token); + + // User holatini yangilash + setLoginUser({ + ...loginUser, + active: true, + }); + } + } catch (error) { + console.error("WebSocket message parse error:", error); + } + }; + + socket.onerror = (err) => { + console.error("WebSocket error:", err); + }; + + socket.onclose = () => { + console.log("WebSocket closed"); + }; + + return () => { + socket.close(); + }; + }, [loginUser]); + if (isPending && loginUser === null) { return (
@@ -66,7 +110,7 @@ const TokenLayout = ({ children }: { children: React.ReactNode }) => { ); } - return <>{token ? children : }; + return <>{token || gettoken ? children : }; }; export default TokenLayout; diff --git a/vite.config.ts b/vite.config.ts index 93cef28..fa57bdf 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,9 +14,10 @@ export default defineConfig({ "process.env": {}, }, server: { + port: 5174, host: true, // barcha hostlarga ruxsat allowedHosts: [ - "nursing-worked-delays-assist.trycloudflare.com", // ngrok host qo'shildi + "seating-hostel-amended-ooo.trycloudflare.com", // ngrok host qo'shildi ], }, });