index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import thunk from 'redux-thunk';
  2. import {createStore, combineReducers, applyMiddleware} from 'redux';
  3. function authReducer(state, action){
  4. if(state === undefined){
  5. if(localStorage.authToken || localStorage.authToken === 'null'){
  6. action.type = 'LOGIN'
  7. action.token = localStorage.authToken
  8. }
  9. else {
  10. return {}
  11. }
  12. }
  13. if(action.type === 'LOGIN'){
  14. console.log('LOGIN')
  15. localStorage.authToken = action.token
  16. let tok = localStorage.authToken.split('.')[1]
  17. let decoded = JSON.parse(atob((tok)))
  18. return {token:action.token, payload: decoded}
  19. }
  20. if(action.type === 'LOGOUT'){
  21. console.log("LOGOUT")
  22. localStorage.authToken = ''
  23. return {}
  24. }
  25. return state
  26. }
  27. function promiseReducer(state={}, {type, status, payload, error, name}){
  28. if (type === 'PROMISE'){
  29. return {
  30. ...state,
  31. [name]:{status, payload, error}
  32. }
  33. }
  34. return state
  35. }
  36. const rootReducer = combineReducers({authReducer, promiseReducer});
  37. const store = createStore(rootReducer, applyMiddleware(thunk))
  38. export default store