Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promised middlevare released #80

Merged
merged 1 commit into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/containers/Home/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
USERS_FAILURE,
USERS_SUCCESS,
} from '../action';
import promisedMiddleware from '../../../redux/promisedMiddleware';

const host = 'http://localhost';

axios.defaults.host = host;
axios.defaults.adapter = httpAdapter;

const mockStore = configureMockStore([thunk]);
const mockStore = configureMockStore([thunk, promisedMiddleware(axios)]);

describe('fetch users data', () => {
const response = [{ id: '1', name: 'Welly' }];
Expand Down
16 changes: 4 additions & 12 deletions src/containers/Home/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@ export const USERS_SUCCESS = 'USERS_SUCCESS';
export const API_URL = 'https://jsonplaceholder.typicode.com/users';

// Export this for unit testing more easily
export const fetchUsers = (axios: any, URL: string = API_URL): ThunkAction =>
(dispatch: Dispatch) => {
dispatch({ type: USERS_REQUESTING });

return axios.get(URL)
.then((res) => {
dispatch({ type: USERS_SUCCESS, data: res.data });
})
.catch((err) => {
dispatch({ type: USERS_FAILURE, err });
});
};
export const fetchUsers = (axios: any, URL: string = API_URL): ThunkAction => ({
promise: client => client.get(URL).then(value => ({ data: value.data })),
events: [USERS_REQUESTING, USERS_SUCCESS, USERS_FAILURE],
});

// Preventing dobule fetching data
/* istanbul ignore next */
Expand Down
3 changes: 2 additions & 1 deletion src/containers/UserInfo/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
USER_FAILURE,
USER_SUCCESS,
} from '../action';
import promisedMiddleware from '../../../redux/promisedMiddleware';

const host = 'http://localhost';

axios.defaults.host = host;
axios.defaults.adapter = httpAdapter;

const mockStore = configureMockStore([thunk]);
const mockStore = configureMockStore([thunk, promisedMiddleware(axios)]);

describe('fetch user data', () => {
const userId = 'test';
Expand Down
17 changes: 5 additions & 12 deletions src/containers/UserInfo/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ export const USER_SUCCESS = 'USER_SUCCESS';
export const API_URL = 'https://jsonplaceholder.typicode.com/users';

// Export this for unit testing more easily
export const fetchUser = (userId: string, axios: any, URL: string = API_URL): ThunkAction =>
(dispatch: Dispatch) => {
dispatch({ type: USER_REQUESTING, userId });

return axios.get(`${URL}/${userId}`)
.then((res) => {
dispatch({ type: USER_SUCCESS, userId, data: res.data });
})
.catch((err) => {
dispatch({ type: USER_FAILURE, userId, err });
});
};
export const fetchUser = (userId: string, axios: any, URL: string = API_URL): ThunkAction => ({
promise: client => client.get(`${URL}/${userId}`).then(value => ({ data: value.data })),
events: [USER_REQUESTING, USER_SUCCESS, USER_FAILURE],
userId,
});

// Using for preventing dobule fetching data
/* istanbul ignore next */
Expand Down
17 changes: 17 additions & 0 deletions src/redux/promisedMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default (...args) => ({ dispatch, getState }) => next => (action) => {
if (typeof action === 'function') {
return action(dispatch, getState);
}
const { promise, events, ...rest } = action;
if (!promise) {
return next(action);
}
const [REQUEST, SUCCESS, FAILURE] = events;
next({ ...rest, type: REQUEST });
return promise(...args).then(
value => next({ ...rest, ...value, type: SUCCESS }),
err => next({ ...rest, err, type: FAILURE }),
).catch((err) => {
next({ ...rest, err, type: FAILURE });
});
};
2 changes: 2 additions & 0 deletions src/redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import chalk from 'chalk';

import type { Store } from '../types';
import rootReducer from './reducers';
import promisedMiddleware from './promisedMiddleware';

export default (history: Object, initialState: Object = {}): Store => {
const middlewares = [
thunk.withExtraArgument(axios),
routerMiddleware(history),
promisedMiddleware(axios),
];

const enhancers = [
Expand Down