Created reducer and stateactions for lobby.

This commit is contained in:
Håvar Aambø Fosstveit
2019-10-15 08:03:18 +02:00
parent aa55adbb2d
commit 2e166ca2b2
2 changed files with 76 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
const lobbyPeer = (state = {}, action) =>
{
switch (action.type)
{
case 'ADD_LOBBY_PEER':
return action.payload.lobbyPeer;
case 'SET_LOBBY_PEER_DISPLAY_NAME':
return { ...state, displayName: action.payload.displayName };
default:
return state;
}
};
const lobbyPeers = (state = {}, action) =>
{
switch (action.type)
{
case 'ADD_LOBBY_PEER':
{
return { ...state, [action.payload.lobbyPeer.id]: lobbyPeer(undefined, action) };
}
case 'REMOVE_LOBBY_PEER':
{
const { peerId } = action.payload;
const newState = { ...state };
delete newState[peerId];
return newState;
}
case 'SET_LOBBY_PEER_DISPLAY_NAME':
{
const oldLobbyPeer = state[action.payload.peerId];
if (!oldLobbyPeer)
{
throw new Error('no Peer found');
}
return { ...state, [oldLobbyPeer.id]: lobbyPeer(oldLobbyPeer, action) };
}
default:
return state;
}
};
export default lobbyPeers;