Advertisement
neleon

Untitled

Apr 30th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
  2. import { reducer as formReducer } from 'redux-form';
  3. import thunkMiddleware from 'redux-thunk';
  4. import axios from 'axios';
  5. import getRootUrl from '../lib/api/getRootUrl';
  6.  
  7. const initialState = {
  8.   user: 0,
  9.   agent: 0,
  10. };
  11.  
  12. export const actionTypes = {
  13.   FETCH_USER: 'FETCH_USER',
  14.   USER_AGENT: 'USER_AGENT',
  15. };
  16.  
  17. // REDUCERS
  18. function authReducer(state = initialState.user, action) {
  19.   switch (action.type) {
  20.     case actionTypes.FETCH_USER:
  21.       return Object.assign({}, state, { user: action.payload });
  22.     default:
  23.       return state;
  24.   }
  25. }
  26.  
  27. function agentReducer(state = initialState.agent, action) {
  28.   switch (action.type) {
  29.     case actionTypes.USER_AGENT:
  30.       return Object.assign({}, state, { agent: action.payload });
  31.     default:
  32.       return state;
  33.   }
  34. }
  35.  
  36. const rootReducer = combineReducers({
  37.   authReducer,
  38.   agentReducer,
  39.   formReducer,
  40. });
  41.  
  42. // ACTIONS
  43. export const fetchUser = () => async (dispatch) => {
  44.   const ROOT_URL = getRootUrl();
  45.   const resUser = await axios.get(`${ROOT_URL}/api/current_user`);
  46.   dispatch({ type: actionTypes.FETCH_USER, payload: resUser.data });
  47. };
  48.  
  49. export const getUserAgent = () => async (dispatch) => {
  50.   const ROOT_URL = getRootUrl();
  51.   const resAgent = await axios.get(`${ROOT_URL}/api/useragent`);
  52.   dispatch({ type: actionTypes.USER_AGENT, payload: resAgent.data });
  53. };
  54.  
  55. export const initStore = (newInitialState = initialState) =>
  56.   createStore(rootReducer, newInitialState, compose(applyMiddleware(thunkMiddleware)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement