Fix: Multi User Invite (#208)

- Use updated API structure
- Use single API to invite multiple users instead of looping.

Co-authored-by: Tejaaswini <tejaaswini.narendra@codemonk.in>
This commit is contained in:
Trisha Anand 2020-08-11 09:24:36 +05:30 committed by GitHub
parent d2442eb815
commit 9d78b020bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 23 deletions

View File

@ -100,7 +100,6 @@ class UserApi extends Api {
} }
static inviteUser(request: InviteUserRequest): AxiosPromise<ApiResponse> { static inviteUser(request: InviteUserRequest): AxiosPromise<ApiResponse> {
request.status = "INVITED";
return Api.post(UserApi.inviteUserURL, request); return Api.post(UserApi.inviteUserURL, request);
} }

View File

@ -56,7 +56,7 @@ export const inviteUsersToOrgSubmitHandler = (
export const inviteUsersToOrg = (values: any, dispatch: any): Promise<any> => { export const inviteUsersToOrg = (values: any, dispatch: any): Promise<any> => {
const data = { const data = {
roleName: values.role, roleName: values.role,
emails: values.users ? values.users.split(",") : [], usernames: values.users ? values.users.split(",") : [],
orgId: values.orgId, orgId: values.orgId,
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -72,6 +72,13 @@ const orgReducer = createReducer(initialState, {
isFetchAllUsers: false, isFetchAllUsers: false,
}, },
}), }),
[ReduxActionTypes.INVITE_USERS_TO_ORG_SUCCESS]: (
state: OrgReduxState,
action: ReduxAction<OrgUser[]>,
) => ({
...state,
orgUsers: [...state.orgUsers, ...action.payload],
}),
[ReduxActionTypes.FETCH_ALL_ROLES_SUCCESS]: ( [ReduxActionTypes.FETCH_ALL_ROLES_SUCCESS]: (
state: OrgReduxState, state: OrgReduxState,
action: ReduxAction<Org[]>, action: ReduxAction<Org[]>,

View File

@ -210,34 +210,25 @@ export function* inviteUser(
export function* inviteUsers( export function* inviteUsers(
action: ReduxActionWithPromise<{ action: ReduxActionWithPromise<{
data: { emails: string[]; orgId: string; roleName: string }; data: { usernames: string[]; orgId: string; roleName: string };
}>, }>,
) { ) {
const { data, resolve, reject } = action.payload; const { data, resolve, reject } = action.payload;
try { try {
const sagasToCall: any[] = []; const response: ApiResponse = yield callAPI(UserApi.inviteUser, {
usernames: data.usernames,
data.emails.forEach((email: string) => { orgId: data.orgId,
sagasToCall.push( roleName: data.roleName,
call(
inviteUser,
{ email, orgId: data.orgId, roleName: data.roleName },
reject,
),
);
}); });
yield all(sagasToCall); const isValidResponse = yield validateResponse(response);
if (!isValidResponse) {
let errorMessage = `${data.usernames}: `;
errorMessage += getResponseErrorMessage(response);
yield call(reject, { _error: errorMessage });
}
yield put({ yield put({
type: ReduxActionTypes.INVITE_USERS_TO_ORG_SUCCESS, type: ReduxActionTypes.INVITE_USERS_TO_ORG_SUCCESS,
payload: { payload: response.data,
inviteCount: sagasToCall.length,
},
});
yield put({
type: ReduxActionTypes.FETCH_ALL_USERS_INIT,
payload: {
orgId: data.orgId,
},
}); });
yield call(resolve); yield call(resolve);
yield put(reset(INVITE_USERS_TO_ORG_FORM)); yield put(reset(INVITE_USERS_TO_ORG_FORM));