Add action user-admin/fetchProfile
Change-Id: Ia2379cb370652529bfdf657dec927b5350534d5a
This commit is contained in:
parent
b44238422a
commit
2d2bf0db53
@ -1 +1,2 @@
|
||||
export { fetchUsers, dismissFetchUsersError } from './fetchUsers';
|
||||
export { fetchProfile, dismissFetchProfileError } from './fetchProfile';
|
||||
|
@ -2,3 +2,7 @@ export const USER_ADMIN_FETCH_USERS_BEGIN = 'USER_ADMIN_FETCH_USERS_BEGIN';
|
||||
export const USER_ADMIN_FETCH_USERS_SUCCESS = 'USER_ADMIN_FETCH_USERS_SUCCESS';
|
||||
export const USER_ADMIN_FETCH_USERS_FAILURE = 'USER_ADMIN_FETCH_USERS_FAILURE';
|
||||
export const USER_ADMIN_FETCH_USERS_DISMISS_ERROR = 'USER_ADMIN_FETCH_USERS_DISMISS_ERROR';
|
||||
export const USER_ADMIN_FETCH_PROFILE_BEGIN = 'USER_ADMIN_FETCH_PROFILE_BEGIN';
|
||||
export const USER_ADMIN_FETCH_PROFILE_SUCCESS = 'USER_ADMIN_FETCH_PROFILE_SUCCESS';
|
||||
export const USER_ADMIN_FETCH_PROFILE_FAILURE = 'USER_ADMIN_FETCH_PROFILE_FAILURE';
|
||||
export const USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR = 'USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR';
|
||||
|
86
src/features/user-admin/redux/fetchProfile.js
Normal file
86
src/features/user-admin/redux/fetchProfile.js
Normal file
@ -0,0 +1,86 @@
|
||||
import {
|
||||
USER_ADMIN_FETCH_PROFILE_BEGIN,
|
||||
USER_ADMIN_FETCH_PROFILE_SUCCESS,
|
||||
USER_ADMIN_FETCH_PROFILE_FAILURE,
|
||||
USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR,
|
||||
} from './constants';
|
||||
|
||||
export function fetchProfile(username) {
|
||||
return (dispatch, getState) => {
|
||||
dispatch({
|
||||
type: USER_ADMIN_FETCH_PROFILE_BEGIN,
|
||||
});
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const mtx = getState().common.mtx;
|
||||
const doRequest = mtx._http.authedRequest(undefined, "GET", "/profile/" + username, undefined)
|
||||
doRequest.then(
|
||||
(res) => {
|
||||
dispatch({
|
||||
type: USER_ADMIN_FETCH_PROFILE_SUCCESS,
|
||||
username: username,
|
||||
profile: res,
|
||||
});
|
||||
resolve(res);
|
||||
},
|
||||
// Use rejectHandler as the second argument so that render errors won't be caught.
|
||||
(err) => {
|
||||
dispatch({
|
||||
type: USER_ADMIN_FETCH_PROFILE_FAILURE,
|
||||
data: { error: err },
|
||||
});
|
||||
reject(err);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
|
||||
// Async action saves request error by default, this method is used to dismiss the error info.
|
||||
// If you don't want errors to be saved in Redux store, just ignore this method.
|
||||
export function dismissFetchProfileError() {
|
||||
return {
|
||||
type: USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR,
|
||||
};
|
||||
}
|
||||
|
||||
export function reducer(state, action) {
|
||||
switch (action.type) {
|
||||
case USER_ADMIN_FETCH_PROFILE_BEGIN:
|
||||
// Just after a request is sent
|
||||
return {
|
||||
...state,
|
||||
fetchProfilePending: true,
|
||||
fetchProfileError: null,
|
||||
};
|
||||
|
||||
case USER_ADMIN_FETCH_PROFILE_SUCCESS:
|
||||
// The request is success
|
||||
return {
|
||||
...state,
|
||||
userProfiles: { ...state.userProfiles, [action.username]: action.profile },
|
||||
fetchProfilePending: false,
|
||||
fetchProfileError: null,
|
||||
};
|
||||
|
||||
case USER_ADMIN_FETCH_PROFILE_FAILURE:
|
||||
// The request is failed
|
||||
return {
|
||||
...state,
|
||||
fetchProfilePending: false,
|
||||
fetchProfileError: action.data.error,
|
||||
};
|
||||
|
||||
case USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR:
|
||||
// Dismiss the request failure error
|
||||
return {
|
||||
...state,
|
||||
fetchProfileError: null,
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@ export function fetchUsers(args = {}) {
|
||||
// e.g.: handleSubmit() { this.props.actions.submitForm(data).then(()=> {}).catch(() => {}); }
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const mtx = getState().common.mtx;
|
||||
if (!mtx) return;
|
||||
const doRequest = mtx._http.authedRequest(undefined, "GET", "/admin/users/" + mtx.credentials.userId)
|
||||
doRequest.then(
|
||||
(res) => {
|
||||
|
@ -7,8 +7,11 @@
|
||||
// NOTE: initialState constant is necessary so that Rekit could auto add initial state when creating async actions.
|
||||
const initialState = {
|
||||
userList: [],
|
||||
userProfiles: {},
|
||||
fetchUsersPending: false,
|
||||
fetchUsersError: null,
|
||||
fetchProfilePending: false,
|
||||
fetchProfileError: null,
|
||||
};
|
||||
|
||||
export default initialState;
|
||||
|
@ -8,9 +8,11 @@
|
||||
|
||||
import initialState from './initialState';
|
||||
import { reducer as fetchUsersReducer } from './fetchUsers';
|
||||
import { reducer as fetchProfileReducer } from './fetchProfile';
|
||||
|
||||
const reducers = [
|
||||
fetchUsersReducer,
|
||||
fetchProfileReducer,
|
||||
];
|
||||
|
||||
export default function reducer(state = initialState, action) {
|
||||
|
97
tests/features/user-admin/redux/fetchProfile.test.js
Normal file
97
tests/features/user-admin/redux/fetchProfile.test.js
Normal file
@ -0,0 +1,97 @@
|
||||
import configureMockStore from 'redux-mock-store';
|
||||
import thunk from 'redux-thunk';
|
||||
import nock from 'nock';
|
||||
|
||||
import {
|
||||
USER_ADMIN_FETCH_PROFILE_BEGIN,
|
||||
USER_ADMIN_FETCH_PROFILE_SUCCESS,
|
||||
USER_ADMIN_FETCH_PROFILE_FAILURE,
|
||||
USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR,
|
||||
} from '../../../../src/features/user-admin/redux/constants';
|
||||
|
||||
import {
|
||||
fetchProfile,
|
||||
dismissFetchProfileError,
|
||||
reducer,
|
||||
} from '../../../../src/features/user-admin/redux/fetchProfile';
|
||||
|
||||
const middlewares = [thunk];
|
||||
const mockStore = configureMockStore(middlewares);
|
||||
|
||||
describe('user-admin/redux/fetchProfile', () => {
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
it('dispatches success action when fetchProfile succeeds', () => {
|
||||
const store = mockStore({});
|
||||
|
||||
return store.dispatch(fetchProfile())
|
||||
.then(() => {
|
||||
const actions = store.getActions();
|
||||
expect(actions[0]).toHaveProperty('type', USER_ADMIN_FETCH_PROFILE_BEGIN);
|
||||
expect(actions[1]).toHaveProperty('type', USER_ADMIN_FETCH_PROFILE_SUCCESS);
|
||||
});
|
||||
});
|
||||
|
||||
it('dispatches failure action when fetchProfile fails', () => {
|
||||
const store = mockStore({});
|
||||
|
||||
return store.dispatch(fetchProfile({ error: true }))
|
||||
.catch(() => {
|
||||
const actions = store.getActions();
|
||||
expect(actions[0]).toHaveProperty('type', USER_ADMIN_FETCH_PROFILE_BEGIN);
|
||||
expect(actions[1]).toHaveProperty('type', USER_ADMIN_FETCH_PROFILE_FAILURE);
|
||||
expect(actions[1]).toHaveProperty('data.error', expect.anything());
|
||||
});
|
||||
});
|
||||
|
||||
it('returns correct action by dismissFetchProfileError', () => {
|
||||
const expectedAction = {
|
||||
type: USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR,
|
||||
};
|
||||
expect(dismissFetchProfileError()).toEqual(expectedAction);
|
||||
});
|
||||
|
||||
it('handles action type USER_ADMIN_FETCH_PROFILE_BEGIN correctly', () => {
|
||||
const prevState = { fetchUserPending: false };
|
||||
const state = reducer(
|
||||
prevState,
|
||||
{ type: USER_ADMIN_FETCH_PROFILE_BEGIN }
|
||||
);
|
||||
expect(state).not.toBe(prevState); // should be immutable
|
||||
expect(state.fetchUserPending).toBe(true);
|
||||
});
|
||||
|
||||
it('handles action type USER_ADMIN_FETCH_PROFILE_SUCCESS correctly', () => {
|
||||
const prevState = { fetchUserPending: true };
|
||||
const state = reducer(
|
||||
prevState,
|
||||
{ type: USER_ADMIN_FETCH_PROFILE_SUCCESS, data: {} }
|
||||
);
|
||||
expect(state).not.toBe(prevState); // should be immutable
|
||||
expect(state.fetchUserPending).toBe(false);
|
||||
});
|
||||
|
||||
it('handles action type USER_ADMIN_FETCH_PROFILE_FAILURE correctly', () => {
|
||||
const prevState = { fetchUserPending: true };
|
||||
const state = reducer(
|
||||
prevState,
|
||||
{ type: USER_ADMIN_FETCH_PROFILE_FAILURE, data: { error: new Error('some error') } }
|
||||
);
|
||||
expect(state).not.toBe(prevState); // should be immutable
|
||||
expect(state.fetchUserPending).toBe(false);
|
||||
expect(state.fetchUserError).toEqual(expect.anything());
|
||||
});
|
||||
|
||||
it('handles action type USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR correctly', () => {
|
||||
const prevState = { fetchUserError: new Error('some error') };
|
||||
const state = reducer(
|
||||
prevState,
|
||||
{ type: USER_ADMIN_FETCH_PROFILE_DISMISS_ERROR }
|
||||
);
|
||||
expect(state).not.toBe(prevState); // should be immutable
|
||||
expect(state.fetchUserError).toBe(null);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user