Skip to content

Commit

Permalink
Add class to handle api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinesalib committed Apr 22, 2020
1 parent 4e998bd commit 78423cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/javascript/api/railsApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { store } from '../configureStore';
import {setErrorDialogMessage} from "../actions/dialogMessage";

const axios = require('axios');

class RailsApi {
handleError = (error) => {
store.dispatch(setErrorDialogMessage({error}))
}

get(path, params = {}) {
return axios.get(path, { params })
.catch((error) => {
this.handleError(error)
});
};
}

export default new RailsApi;
4 changes: 3 additions & 1 deletion app/javascript/configureStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const rootReducer = combineReducers({
dialogMessage: dialogMessage,
});

export let store = null;

export default function configureStore(user) {
const preloadedState = { app: { ...appReducerDefaultState, user } };

const store = createStore(
store = createStore(
rootReducer,
preloadedState,
composeWithDevTools(applyMiddleware(thunk))
Expand Down

0 comments on commit 78423cf

Please sign in to comment.