From 65dc56ad2d87a32542383ddc93365d2f5b6d411c Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Mon, 9 Jan 2023 18:01:44 -0300 Subject: [PATCH] back handleError to views and refactor the submit --- app/lib/methods/helpers/info.ts | 10 ---- app/views/ChangeAvatarView/index.tsx | 71 ++++++++++++++++++++-------- app/views/ProfileView/index.tsx | 16 +++++-- 3 files changed, 65 insertions(+), 32 deletions(-) diff --git a/app/lib/methods/helpers/info.ts b/app/lib/methods/helpers/info.ts index f184a76d8..2fc903d04 100644 --- a/app/lib/methods/helpers/info.ts +++ b/app/lib/methods/helpers/info.ts @@ -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 }); diff --git a/app/views/ChangeAvatarView/index.tsx b/app/views/ChangeAvatarView/index.tsx index 23fc61836..eff658ca1 100644 --- a/app/views/ChangeAvatarView/index.tsx +++ b/app/views/ChangeAvatarView/index.tsx @@ -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); - if (!fromUser && room?.rid) { - // Change Rooms Avatar - await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data }); - } else if (avatar?.url) { - // Change User's Avatar - await Services.setAvatarFromService(avatar); - } else if (textAvatar) { - // Change User's Avatar - await Services.resetAvatar(user.id); - } + let result; + if (!fromUser && room?.rid) { + // Change Rooms Avatar + result = await changeRoomsAvatar(room.rid); + } else if (avatar?.url) { + // Change User's Avatar + result = await changeUserAvatar(avatar); + } else if (textAvatar) { + // Change User's Avatar + 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) { - return handleError(e, 'resetAvatar', 'changing_avatar'); - } + return handleError(e, 'saveRoomSettings', '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'); } }; + 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}`); diff --git a/app/views/ProfileView/index.tsx b/app/views/ProfileView/index.tsx index 9a8c1891a..dd3981f85 100644 --- a/app/views/ProfileView/index.tsx +++ b/app/views/ProfileView/index.tsx @@ -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 } 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 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 });