From 2e166ca2b26b48d89896612cc101f38279e64696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5var=20Aamb=C3=B8=20Fosstveit?= Date: Tue, 15 Oct 2019 08:03:18 +0200 Subject: [PATCH] Created reducer and stateactions for lobby. --- app/src/actions/stateActions.js | 24 +++++++++++++++ app/src/reducers/lobbyPeers.js | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/src/reducers/lobbyPeers.js diff --git a/app/src/actions/stateActions.js b/app/src/actions/stateActions.js index fda3ceb..13df082 100644 --- a/app/src/actions/stateActions.js +++ b/app/src/actions/stateActions.js @@ -391,6 +391,30 @@ export const setPeerVolume = (peerId, volume) => }; }; +export const addLobbyPeer = (lobbyPeer) => +{ + return { + type : 'ADD_LOBBY_PEER', + payload : { lobbyPeer } + }; +}; + +export const removeLobbyPeer = (peerId) => +{ + return { + type : 'REMOVE_LOBBY_PEER', + payload : { peerId } + }; +}; + +export const setLobbyPeerDisplayName = (displayName, peerId) => +{ + return { + type : 'SET_LOBBY_PEER_DISPLAY_NAME', + payload : { displayName, peerId } + }; +}; + export const addNotification = (notification) => { return { diff --git a/app/src/reducers/lobbyPeers.js b/app/src/reducers/lobbyPeers.js new file mode 100644 index 0000000..ad538f7 --- /dev/null +++ b/app/src/reducers/lobbyPeers.js @@ -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;