-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"refactor: Use userData context for user information"
- Loading branch information
Showing
6 changed files
with
99 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
import { createContext, useContext, useState, useEffect } from "react"; | ||
import { useWebSocket } from "@/components/WebSocketProvidor"; | ||
|
||
interface UserData { | ||
ID: string; | ||
FirstName: string; | ||
LastName: string; | ||
Email: string; | ||
Country: string; | ||
} | ||
|
||
interface UserDataContextType { | ||
userData: UserData; | ||
setUserData: React.Dispatch<React.SetStateAction<UserData>>; | ||
} | ||
|
||
const UserDataContext = createContext<UserDataContextType | null>(null); | ||
|
||
export const useUserData = () => { | ||
const context = useContext(UserDataContext); | ||
if (!context) { | ||
throw new Error("useUserData must be used within a UserDataProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export const UserDataProvider = ({ children }: { children: React.ReactNode }) => { | ||
const { socket, isReady } = useWebSocket(); | ||
const [userData, setUserData] = useState<UserData>({ | ||
ID: "", | ||
FirstName: "", | ||
LastName: "", | ||
Email: "", | ||
Country: "", | ||
}); | ||
useEffect(()=>{ | ||
|
||
if(socket && isReady){ | ||
socket.send( "getUser" ); | ||
|
||
socket.onMessage((msg)=>{ | ||
const response = JSON.parse(msg) | ||
if (response.userData) { | ||
|
||
setUserData(response.userData) | ||
} | ||
})}},[socket, isReady]) | ||
useEffect(() => { | ||
console.log("User data updated", userData); | ||
}, [userData]); // This will log whenever userData is updated | ||
return ( | ||
<UserDataContext.Provider value={{ userData, setUserData }}> | ||
{children} | ||
</UserDataContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters