From 84b16b2d97ad64909cb02ca2375647e0ff7e7020 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 14 Dec 2022 14:15:16 -0300 Subject: [PATCH] refactor the submit function --- app/views/ChangeAvatarView/index.tsx | 57 ++++++++++------------------ 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/app/views/ChangeAvatarView/index.tsx b/app/views/ChangeAvatarView/index.tsx index 03570e5f9..fdd8b04c8 100644 --- a/app/views/ChangeAvatarView/index.tsx +++ b/app/views/ChangeAvatarView/index.tsx @@ -74,46 +74,31 @@ const ChangeAvatarView = () => { }; const submit = async () => { - if (!fromUser && room?.rid) { - try { - setSaving(true); + try { + setSaving(true); + if (!fromUser && room?.rid) { + // Change Rooms Avatar await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data }); - setSaving(false); - avatarUrl.current = ''; - return navigation.goBack(); - } catch (e) { - log(e); - setSaving(false); - return handleError(e, 'setAvatarFromService', 'changing_avatar'); - } - } - - // Change Users Avatar - if (avatar?.url) { - try { - setSaving(true); + } else if (avatar?.url) { + // Change User's Avatar await Services.setAvatarFromService(avatar); - setSaving(false); - avatarUrl.current = ''; - return navigation.goBack(); - } catch (e) { - log(e); - setSaving(false); - return handleError(e, 'setAvatarFromService', 'changing_avatar'); - } - } - - if (textAvatar) { - try { - setSaving(true); + } else if (textAvatar) { + // Change User's Avatar await Services.resetAvatar(user.id); - setSaving(false); - avatarUrl.current = ''; - return navigation.goBack(); - } catch (e) { - setSaving(false); - handleError(e, 'resetAvatar', 'changing_avatar'); } + setSaving(false); + avatarUrl.current = ''; + return navigation.goBack(); + } 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, 'setAvatarFromService', 'changing_avatar'); } };