Add general util to handle actions that are forbidden (#1206)

This commit is contained in:
enahum 2017-11-27 17:30:26 -03:00 committed by GitHub
parent b27a1f8e0b
commit 6b4100d517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 8 deletions

View file

@ -8,9 +8,9 @@ export function handleAddChannelMembers(channelId, members) {
try {
const requests = members.map((m) => dispatch(addChannelMember(channelId, m, getState)));
await Promise.all(requests);
return await Promise.all(requests);
} catch (error) {
// should be handled by global error handling
return error;
}
};
}

View file

@ -8,9 +8,9 @@ export function handleRemoveChannelMembers(channelId, members) {
try {
const requests = members.map((m) => dispatch(removeChannelMember(channelId, m, getState)));
await Promise.all(requests);
return await Promise.all(requests);
} catch (error) {
// should be handled by global error handling
return error;
}
};
}

View file

@ -15,6 +15,7 @@ import CustomList from 'app/components/custom_list';
import UserListRow from 'app/components/custom_list/user_list_row';
import SearchBar from 'app/components/search_bar';
import StatusBar from 'app/components/status_bar';
import {alertErrorIfInvalidPermissions} from 'app/utils/general';
import {createMembersSections, loadingText, markSelectedProfiles} from 'app/utils/member_list';
import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme';
@ -144,7 +145,7 @@ class ChannelAddMembers extends PureComponent {
});
};
handleAddMembersPress = () => {
handleAddMembersPress = async () => {
const {selectedMembers} = this.state;
const {actions, currentChannel} = this.props;
const membersToAdd = Object.keys(selectedMembers).filter((m) => selectedMembers[m]);
@ -154,7 +155,9 @@ class ChannelAddMembers extends PureComponent {
return;
}
actions.handleAddChannelMembers(currentChannel.id, membersToAdd);
alertErrorIfInvalidPermissions(
await actions.handleAddChannelMembers(currentChannel.id, membersToAdd)
);
};
handleRowSelect = (id) => {

View file

@ -14,6 +14,7 @@ import Loading from 'app/components/loading';
import CustomList from 'app/components/custom_list';
import SearchBar from 'app/components/search_bar';
import StatusBar from 'app/components/status_bar';
import {alertErrorIfInvalidPermissions} from 'app/utils/general';
import {createMembersSections, loadingText, markSelectedProfiles} from 'app/utils/member_list';
import UserListRow from 'app/components/custom_list/user_list_row';
import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme';
@ -228,9 +229,11 @@ class ChannelMembers extends PureComponent {
}
};
removeMembers = (membersToRemove) => {
removeMembers = async (membersToRemove) => {
const {actions, currentChannel} = this.props;
actions.handleRemoveChannelMembers(currentChannel.id, membersToRemove);
alertErrorIfInvalidPermissions(
await actions.handleRemoveChannelMembers(currentChannel.id, membersToRemove)
);
};
renderMemberRow = (props) => {

View file

@ -11,6 +11,27 @@ export function alertErrorWithFallback(intl, error, fallback, values) {
Alert.alert('', msg);
}
export function alertErrorIfInvalidPermissions(result) {
function isForbidden(data) {
const {error} = data;
return error && error.status_code === 403;
}
let error = null;
if (Array.isArray(result)) {
const item = result.find((r) => isForbidden(r));
if (item) {
error = item.error;
}
} else if (isForbidden(result)) {
error = result.error;
}
if (error) {
Alert.alert(error.message);
}
}
export function emptyFunction() {
return;
}