Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import {combineReducers} from 'redux'
  2.  
  3. // Component Reducers
  4. import login from '../components/LoginContainer/reducers'
  5. import forgotPassword from '../components/ForgotPasswordContainer/reducers'
  6. import resetPassword from '../components/ResetPasswordContainer/reducers'
  7.  
  8. // Action Constants
  9. import {RECEIVE_USER_SUCCESS, RECEIVE_LOGIN_SUCCESS, RECEIVE_LOGIN_ERROR} from '../components/LoginContainer/actions'
  10. import {RECEIVE_RESET_SUCCESS} from '../components/ResetPasswordContainer/actions'
  11. import {RECEIVE_PING_SUCCESS, REQUEST_PING, LOGOUT_USER, STORE_AUTH_TOKEN} from '../actions'
  12.  
  13. const user = (state = null, action) => {
  14. switch (action.type) {
  15. case RECEIVE_USER_SUCCESS:
  16. return action.user;
  17. case RECEIVE_LOGIN_ERROR:
  18. case LOGOUT_USER:
  19. return null;
  20. default:
  21. return state;
  22. }
  23. };
  24.  
  25. const preferences = (state = null, action) => {
  26. switch (action.type) {
  27. case RECEIVE_USER_SUCCESS:
  28. const prefs = {};
  29. const {preferences} = action.user;
  30.  
  31. preferences.map(pref => prefs[pref.code] = pref.user_preference || pref.default_option);
  32.  
  33. return prefs;
  34. case LOGOUT_USER:
  35. return null;
  36. default:
  37. return state;
  38. }
  39. };
  40.  
  41. const authToken = (state = null, action) => {
  42. switch (action.type) {
  43. case STORE_AUTH_TOKEN:
  44. return action.authToken;
  45. case RECEIVE_LOGIN_ERROR:
  46. case LOGOUT_USER:
  47. return null;
  48. default:
  49. return state;
  50. }
  51. };
  52.  
  53. const isPinging = (state = false, action) => {
  54. switch (action.type) {
  55. case REQUEST_PING:
  56. return true;
  57. case RECEIVE_PING_SUCCESS:
  58. case RECEIVE_LOGIN_SUCCESS:
  59. case RECEIVE_LOGIN_ERROR:
  60. case LOGOUT_USER:
  61. return false;
  62. default:
  63. return state;
  64. }
  65. };
  66.  
  67. const isLoggedIn = (state = false, action) => {
  68. switch (action.type) {
  69. case RECEIVE_LOGIN_SUCCESS:
  70. case RECEIVE_RESET_SUCCESS:
  71. case RECEIVE_PING_SUCCESS:
  72. return true;
  73. case RECEIVE_LOGIN_ERROR:
  74. case LOGOUT_USER:
  75. return false;
  76. default:
  77. return state;
  78. }
  79. };
  80.  
  81. // Create reducer chain
  82. const authentication = combineReducers({
  83. user,
  84. preferences,
  85. authToken,
  86. isLoggedIn,
  87. isPinging,
  88. });
  89.  
  90. export default {
  91. authentication,
  92. login,
  93. forgotPassword,
  94. resetPassword
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement