back handleError to views and refactor the submit
This commit is contained in:
parent
ea20167981
commit
65dc56ad2d
|
@ -2,16 +2,6 @@ import { Alert } from 'react-native';
|
||||||
|
|
||||||
import I18n from '../../../i18n';
|
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 =>
|
export const showErrorAlert = (message: string, title?: string, onPress = () => {}): void =>
|
||||||
Alert.alert(title || '', message, [{ text: 'OK', onPress }], { cancelable: true });
|
Alert.alert(title || '', message, [{ text: 'OK', onPress }], { cancelable: true });
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { compareServerVersion } from '../../lib/methods/helpers';
|
||||||
import KeyboardView from '../../containers/KeyboardView';
|
import KeyboardView from '../../containers/KeyboardView';
|
||||||
import sharedStyles from '../Styles';
|
import sharedStyles from '../Styles';
|
||||||
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
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 StatusBar from '../../containers/StatusBar';
|
||||||
import { useTheme } from '../../theme';
|
import { useTheme } from '../../theme';
|
||||||
import SafeAreaView from '../../containers/SafeAreaView';
|
import SafeAreaView from '../../containers/SafeAreaView';
|
||||||
|
@ -74,34 +74,67 @@ const ChangeAvatarView = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const submit = async () => {
|
const submit = async () => {
|
||||||
try {
|
let result;
|
||||||
setSaving(true);
|
if (!fromUser && room?.rid) {
|
||||||
if (!fromUser && room?.rid) {
|
// Change Rooms Avatar
|
||||||
// Change Rooms Avatar
|
result = await changeRoomsAvatar(room.rid);
|
||||||
await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data });
|
} else if (avatar?.url) {
|
||||||
} else if (avatar?.url) {
|
// Change User's Avatar
|
||||||
// Change User's Avatar
|
result = await changeUserAvatar(avatar);
|
||||||
await Services.setAvatarFromService(avatar);
|
} else if (textAvatar) {
|
||||||
} else if (textAvatar) {
|
// Change User's Avatar
|
||||||
// Change User's Avatar
|
result = await resetUserAvatar();
|
||||||
await Services.resetAvatar(user.id);
|
}
|
||||||
}
|
if (result) {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
avatarUrl.current = '';
|
avatarUrl.current = '';
|
||||||
return navigation.goBack();
|
return navigation.goBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const changeRoomsAvatar = async (rid: string) => {
|
||||||
|
try {
|
||||||
|
setSaving(true);
|
||||||
|
await Services.saveRoomSettings(rid, { roomAvatar: avatar?.data });
|
||||||
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(e);
|
log(e);
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
if (!fromUser && room?.rid) {
|
return handleError(e, 'saveRoomSettings', 'changing_avatar');
|
||||||
return handleError(e, 'saveRoomSettings', 'changing_avatar');
|
}
|
||||||
}
|
};
|
||||||
if (textAvatar) {
|
|
||||||
return handleError(e, 'resetAvatar', 'changing_avatar');
|
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');
|
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 = () => {
|
const resetAvatar = () => {
|
||||||
setAvatar(null);
|
setAvatar(null);
|
||||||
setTextAvatar(`@${user.username}`);
|
setTextAvatar(`@${user.username}`);
|
||||||
|
|
|
@ -11,7 +11,7 @@ import Touch from '../../containers/Touch';
|
||||||
import KeyboardView from '../../containers/KeyboardView';
|
import KeyboardView from '../../containers/KeyboardView';
|
||||||
import sharedStyles from '../Styles';
|
import sharedStyles from '../Styles';
|
||||||
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
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 { LISTENER } from '../../containers/Toast';
|
||||||
import EventEmitter from '../../lib/methods/helpers/events';
|
import EventEmitter from '../../lib/methods/helpers/events';
|
||||||
import { FormTextInput } from '../../containers/TextInput';
|
import { FormTextInput } from '../../containers/TextInput';
|
||||||
|
@ -254,7 +254,7 @@ class ProfileView extends React.Component<IProfileViewProps, IProfileViewState>
|
||||||
}
|
}
|
||||||
logEvent(events.PROFILE_SAVE_CHANGES_F);
|
logEvent(events.PROFILE_SAVE_CHANGES_F);
|
||||||
this.setState({ saving: false, currentPassword: null, twoFactorCode: null });
|
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') });
|
EventEmitter.emit(LISTENER, { message: I18n.t('Avatar_changed_successfully') });
|
||||||
this.init();
|
this.init();
|
||||||
} catch (e) {
|
} 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 = () => {
|
handleEditAvatar = () => {
|
||||||
const { navigation } = this.props;
|
const { navigation } = this.props;
|
||||||
navigation.navigate('ChangeAvatarView', { fromUser: true });
|
navigation.navigate('ChangeAvatarView', { fromUser: true });
|
||||||
|
|
Loading…
Reference in New Issue