From 447a373271cadd3c407de8ba2bcfe96252297c62 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 18 Nov 2023 19:32:30 +0100 Subject: [PATCH] Created the GroupContext.jsx --- client/src/common/contexts/GroupContext.jsx | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 client/src/common/contexts/GroupContext.jsx diff --git a/client/src/common/contexts/GroupContext.jsx b/client/src/common/contexts/GroupContext.jsx new file mode 100644 index 0000000..122c8d0 --- /dev/null +++ b/client/src/common/contexts/GroupContext.jsx @@ -0,0 +1,34 @@ +import {createContext, useEffect, useState} from "react"; +import {socket} from "@/common/utils/socket.js"; + +export const GroupContext = createContext({}); + +export const GroupProvider = ({children}) => { + + const [groups, setGroups] = useState([]); + + const handleJoin = (group) => { + console.log(group) + setGroups(groups => [...groups, group]); + } + + const handleLeave = (group) => { + setGroups(groups => groups.filter(g => g.id !== group.id)); + } + + useEffect(() => { + socket.on("JOINED", handleJoin); + socket.on("LEFT", handleLeave); + + return () => { + socket.off("JOINED", handleJoin); + socket.off("LEFT", handleLeave); + } + }, []); + + return ( + + {children} + + ) +} \ No newline at end of file