From ad9b21a832f51efbfe3150c44d614251a0087a32 Mon Sep 17 00:00:00 2001 From: Italo A Date: Sat, 11 Nov 2023 10:43:53 -0300 Subject: [PATCH] feat: update user type and revalidate query --- frontend/src/contexts/AuthContext.tsx | 12 +----------- frontend/src/contexts/ChatContext.tsx | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/frontend/src/contexts/AuthContext.tsx b/frontend/src/contexts/AuthContext.tsx index 130a4a6a..bc5418c9 100644 --- a/frontend/src/contexts/AuthContext.tsx +++ b/frontend/src/contexts/AuthContext.tsx @@ -3,6 +3,7 @@ import nookies from "nookies"; import React, { createContext, useEffect, useState } from "react"; import { api } from "@/services/apiClient"; import { redirect } from "next/navigation"; +import { User } from "@/types/user"; type AuthContextType = { payload: TokenPayload; @@ -20,17 +21,6 @@ export type TokenPayload = { exp: number; }; -export type User = { - id: string; - login: string; - displayName: string; - email: string; - avatar: string; - status: "online" | "offline" | "away" | "busy"; - victory: number; - mfaEnabled: boolean; -}; - type AuthProviderProps = { children: React.ReactNode; }; diff --git a/frontend/src/contexts/ChatContext.tsx b/frontend/src/contexts/ChatContext.tsx index 1974e6c1..932727a9 100644 --- a/frontend/src/contexts/ChatContext.tsx +++ b/frontend/src/contexts/ChatContext.tsx @@ -1,5 +1,7 @@ "use client"; import chatService from "@/services/chatClient"; +import { queryClient } from "@/services/queryClient"; +import { User } from "@/types/user"; import React, { createContext, useEffect, useState } from "react"; type Elements = @@ -38,10 +40,6 @@ export type Chat = { owner: string; }; -export type User = { - login: string; -}; - export type ChatList = Chat[]; export const ChatContext = createContext( @@ -68,16 +66,19 @@ export const ChatProvider = ({ children }: ChatProviderProps) => { }; const timeout = (delay: number) => { - return new Promise(res => setTimeout(res, delay)); - } + return new Promise((res) => setTimeout(res, delay)); + }; useEffect(() => { // Connect to the Socket.IO server chatService.connect(); + chatService.socket?.on("userLogin", (user: User) => { - console.log(`Current user login: ${user.login}`); + console.log(`Current user login: ${user.login}`, user); setUser(() => user); + queryClient.invalidateQueries(["user_status", user.id]); }); + // Listen for incoming messages recursively every 10 seconds chatService.socket?.on("listChats", (newChatList: ChatList) => { setChatList(() => newChatList); @@ -88,7 +89,9 @@ export const ChatProvider = ({ children }: ChatProviderProps) => { }); chatService.socket?.on("deleteChat", (deletedChat: any) => { - setChatList((chatList) => chatList.filter((chat: Chat) => chat.id !== deletedChat.chatId)); + setChatList((chatList) => + chatList.filter((chat: Chat) => chat.id !== deletedChat.chatId) + ); }); chatService.socket?.emit("listChats"); @@ -125,7 +128,7 @@ export const ChatProvider = ({ children }: ChatProviderProps) => { setValidationRequired(true); setShowElement("showChannels"); setIsLoading(false); - } + }; return (