back handleError to views and refactor the submit

This commit is contained in:
Reinaldo Neto 2023-01-09 18:01:44 -03:00
parent ea20167981
commit 65dc56ad2d
3 changed files with 65 additions and 32 deletions

View File

@ -2,16 +2,6 @@ import { Alert } from 'react-native';
import I18n from '../../../i18n';
export const handleError = (e: any, _func: string, action: string) => {
if (e.data && e.data.error.includes('[error-too-many-requests]')) {
return showErrorAlert(e.data.error);
}
if (I18n.isTranslated(e.error)) {
return showErrorAlert(I18n.t(e.error));
}
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t(action) }));
};
export const showErrorAlert = (message: string, title?: string, onPress = () => {}): void =>
Alert.alert(title || '', message, [{ text: 'OK', onPress }], { cancelable: true });

View File

@ -8,7 +8,7 @@ import { compareServerVersion } from '../../lib/methods/helpers';
import KeyboardView from '../../containers/KeyboardView';
import sharedStyles from '../Styles';
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
import { showConfirmationAlert, handleError } from '../../lib/methods/helpers/info';
import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info';
import StatusBar from '../../containers/StatusBar';
import { useTheme } from '../../theme';
import SafeAreaView from '../../containers/SafeAreaView';
@ -74,34 +74,67 @@ const ChangeAvatarView = () => {
};
const submit = async () => {
try {
setSaving(true);
let result;
if (!fromUser && room?.rid) {
// Change Rooms Avatar
await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data });
result = await changeRoomsAvatar(room.rid);
} else if (avatar?.url) {
// Change User's Avatar
await Services.setAvatarFromService(avatar);
result = await changeUserAvatar(avatar);
} else if (textAvatar) {
// Change User's Avatar
await Services.resetAvatar(user.id);
result = await resetUserAvatar();
}
if (result) {
setSaving(false);
avatarUrl.current = '';
return navigation.goBack();
}
};
const changeRoomsAvatar = async (rid: string) => {
try {
setSaving(true);
await Services.saveRoomSettings(rid, { roomAvatar: avatar?.data });
return true;
} catch (e) {
log(e);
setSaving(false);
if (!fromUser && room?.rid) {
return handleError(e, 'saveRoomSettings', 'changing_avatar');
}
if (textAvatar) {
};
const changeUserAvatar = async (avatarUpload: IAvatar) => {
try {
setSaving(true);
await Services.setAvatarFromService(avatarUpload);
return true;
} catch (e) {
log(e);
setSaving(false);
return handleError(e, 'resetAvatar', 'changing_avatar');
}
};
const resetUserAvatar = async () => {
try {
await Services.resetAvatar(user.id);
return true;
} catch (e) {
return handleError(e, 'setAvatarFromService', 'changing_avatar');
}
};
const handleError = (e: any, _func: string, action: string) => {
if (e.data && e.data.error.includes('[error-too-many-requests]')) {
return showErrorAlert(e.data.error);
}
if (I18n.isTranslated(e.error)) {
return showErrorAlert(I18n.t(e.error));
}
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t(action) }));
};
const resetAvatar = () => {
setAvatar(null);
setTextAvatar(`@${user.username}`);

View File

@ -11,7 +11,7 @@ import Touch from '../../containers/Touch';
import KeyboardView from '../../containers/KeyboardView';
import sharedStyles from '../Styles';
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
import { handleError, showConfirmationAlert } from '../../lib/methods/helpers';
import { showErrorAlert, showConfirmationAlert } from '../../lib/methods/helpers';
import { LISTENER } from '../../containers/Toast';
import EventEmitter from '../../lib/methods/helpers/events';
import { FormTextInput } from '../../containers/TextInput';
@ -254,7 +254,7 @@ class ProfileView extends React.Component<IProfileViewProps, IProfileViewState>
}
logEvent(events.PROFILE_SAVE_CHANGES_F);
this.setState({ saving: false, currentPassword: null, twoFactorCode: null });
handleError(e, 'saveUserProfile', 'saving_profile');
this.handleError(e, 'saveUserProfile', 'saving_profile');
}
};
@ -271,10 +271,20 @@ class ProfileView extends React.Component<IProfileViewProps, IProfileViewState>
EventEmitter.emit(LISTENER, { message: I18n.t('Avatar_changed_successfully') });
this.init();
} catch (e) {
handleError(e, 'resetAvatar', 'changing_avatar');
this.handleError(e, 'resetAvatar', 'changing_avatar');
}
};
handleError = (e: any, _func: string, action: string) => {
if (e.data && e.data.error.includes('[error-too-many-requests]')) {
return showErrorAlert(e.data.error);
}
if (I18n.isTranslated(e.error)) {
return showErrorAlert(I18n.t(e.error));
}
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t(action) }));
};
handleEditAvatar = () => {
const { navigation } = this.props;
navigation.navigate('ChangeAvatarView', { fromUser: true });