Add action user-admin/fetchProfile

Change-Id: Ia2379cb370652529bfdf657dec927b5350534d5a
This commit is contained in:
Manuel Stahl
2019-04-25 13:02:35 +02:00
parent b44238422a
commit 2d2bf0db53
7 changed files with 194 additions and 0 deletions
+1
View File
@@ -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';
@@ -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;
+2
View File
@@ -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) {