1
0

Added new functions to the GroupContext.jsx

This commit is contained in:
Mathias Wagner 2023-11-18 23:38:39 +01:00
parent 20c8ab25f1
commit ba2c610c6f
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -5,17 +5,48 @@ export const GroupContext = createContext({});
export const GroupProvider = ({children}) => {
const [allGroups, setAllGroups] = useState([]);
const [groups, setGroups] = useState([]);
const [round, setRound] = useState([]);
const handleJoin = (group) => {
console.log(group)
setGroups(groups => [...groups, group]);
setGroups(groups => [...groups, {...group, capital: 25000}]);
}
const getGroupById = (id) => {
return groups.find(group => group.id === id);
}
const handleRound = (round) => {
setRound(current => [...current, round].sort((a, b) => a.price - b.price));
}
const handleLeave = (group) => {
if (group.capital !== 25000) {
setGroups(groups_ => {
const current = groups_.find(g => g.id === group.id);
if (current) setAllGroups(all => [...all, current]);
return groups_;
});
}
setGroups(groups => groups.filter(g => g.id !== group.id));
}
const updateCapital = (groupId, newCapital) => {
socket.emit("UPDATE_CAPITAL", {id: groupId, capital: newCapital});
setGroups(groups => groups.map(g => {
if (g.id === groupId) {
return {...g, capital: newCapital};
}
return g;
}));
}
const endRound = () => {
setRound([]);
}
useEffect(() => {
socket.on("JOINED", handleJoin);
socket.on("LEFT", handleLeave);
@ -27,7 +58,7 @@ export const GroupProvider = ({children}) => {
}, []);
return (
<GroupContext.Provider value={{groups}}>
<GroupContext.Provider value={{groups, round, handleRound, getGroupById, updateCapital, endRound, allGroups}}>
{children}
</GroupContext.Provider>
)