Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
- import { reducer as formReducer } from 'redux-form';
- import thunkMiddleware from 'redux-thunk';
- import axios from 'axios';
- import getRootUrl from '../lib/api/getRootUrl';
- const initialState = {
- user: 0,
- agent: 0,
- };
- export const actionTypes = {
- FETCH_USER: 'FETCH_USER',
- USER_AGENT: 'USER_AGENT',
- };
- // REDUCERS
- function authReducer(state = initialState.user, action) {
- switch (action.type) {
- case actionTypes.FETCH_USER:
- return Object.assign({}, state, { user: action.payload });
- default:
- return state;
- }
- }
- function agentReducer(state = initialState.agent, action) {
- switch (action.type) {
- case actionTypes.USER_AGENT:
- return Object.assign({}, state, { agent: action.payload });
- default:
- return state;
- }
- }
- const rootReducer = combineReducers({
- authReducer,
- agentReducer,
- formReducer,
- });
- // ACTIONS
- export const fetchUser = () => async (dispatch) => {
- const ROOT_URL = getRootUrl();
- const resUser = await axios.get(`${ROOT_URL}/api/current_user`);
- dispatch({ type: actionTypes.FETCH_USER, payload: resUser.data });
- };
- export const getUserAgent = () => async (dispatch) => {
- const ROOT_URL = getRootUrl();
- const resAgent = await axios.get(`${ROOT_URL}/api/useragent`);
- dispatch({ type: actionTypes.USER_AGENT, payload: resAgent.data });
- };
- export const initStore = (newInitialState = initialState) =>
- createStore(rootReducer, newInitialState, compose(applyMiddleware(thunkMiddleware)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement