daily pastebin goal
79%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.   chat:{},
  3.   highscores:{},
  4.   gameHistory:{},
  5. }
  6.    
  7. {
  8.   currentUserId: "1",
  9.   chat:{ "1": { // Chats for user id 1 }, "2": { // Chats for user id 2 }},
  10.   highscores:{ // Same structure as above },
  11.   gameHistory:{ // Same structure as above },
  12. }
  13.    
  14. const getCurrUserId = state => state.currentUserId
  15.  
  16. const getChats = state => {
  17.    const userId = getCurrUserId(state);
  18.    return state.chat[userId];
  19. }
  20.    
  21. function makeRootReducer(rootReducer){
  22.  return function reducer(state, action){
  23.    if(action.type==='SWITCH_USER'){
  24.       //LOAD USER DATA..
  25.       const data = JSON.parse(localStorage.getItem("store.user."+action.id)||"{}");
  26.       return makeInitialData(data); //this just return initialData.
  27.     }
  28.     let newState = rootReducer(state, action);
  29.     //simple save state to localStorage if state changed
  30.     if(state !== newState)localStorage.setItem('store.user.'+state.user_id',JSON.stringify(newState);
  31.     return newState;
  32.   }
  33.  
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top