Created
July 24, 2018 14:38
-
-
Save kristijanPetr/505c6522960dfd8fc8f153ec7bc06965 to your computer and use it in GitHub Desktop.
Test
This file contains hidden or 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
import { | |
CHAT_STARTED, | |
CHAT_ENDED, | |
CHAT_NEW_MESSAGE, | |
NO_MATCH_FOUND, | |
PLAYER_JOINED, | |
CHAT_MESSAGE, | |
FOUND_MATCH, | |
ONLINE_USERS, | |
SWITCH_ACTIVE_CHAT | |
} from "../types"; | |
export default function( | |
state = { | |
userId: "", | |
messages: [], | |
isCancelled: false, | |
isMatched: false, | |
onlineUsers: [] | |
}, | |
action | |
) { | |
switch (action.type) { | |
case CHAT_STARTED: | |
return action.payload; | |
case CHAT_NEW_MESSAGE: | |
return state; | |
case CHAT_ENDED: | |
return action.payload; | |
case FOUND_MATCH: | |
return { | |
...state, | |
...action.payload | |
}; | |
case NO_MATCH_FOUND: | |
return { | |
...state, | |
isCancelled: true, | |
isMatched: false | |
}; | |
case PLAYER_JOINED: | |
console.log("REDUCER PLAYER JOINED", action.payload); | |
return { | |
...state, | |
isMatched: true, | |
...action.payload | |
}; | |
case CHAT_MESSAGE: | |
let dateWithouthSecond = new Date(); | |
action.payload.timestamp = | |
new Date(Date.now()).toLocaleDateString() + | |
" " + | |
dateWithouthSecond.toLocaleTimeString([], { | |
hour: "2-digit", | |
minute: "2-digit" | |
}); | |
return { | |
...state, | |
messages: [...state.messages, action.payload] | |
}; | |
case ONLINE_USERS: | |
return { | |
...state, | |
onlineUsers: action.onlineUsers | |
}; | |
case SWITCH_ACTIVE_CHAT: | |
return { | |
...state, | |
activeUser: action.userId, | |
activeUsername: action.username | |
}; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment