Skip to content

Commit

Permalink
feat: update user type and revalidate query
Browse files Browse the repository at this point in the history
  • Loading branch information
iaurg committed Nov 11, 2023
1 parent 08b02aa commit ad9b21a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
12 changes: 1 addition & 11 deletions frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
Expand Down
21 changes: 12 additions & 9 deletions frontend/src/contexts/ChatContext.tsx
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -38,10 +40,6 @@ export type Chat = {
owner: string;
};

export type User = {
login: string;
};

export type ChatList = Chat[];

export const ChatContext = createContext<ChatContextType>(
Expand All @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -125,7 +128,7 @@ export const ChatProvider = ({ children }: ChatProviderProps) => {
setValidationRequired(true);
setShowElement("showChannels");
setIsLoading(false);
}
};

return (
<ChatContext.Provider
Expand Down

0 comments on commit ad9b21a

Please sign in to comment.