refactor the submit function

This commit is contained in:
Reinaldo Neto 2022-12-14 14:15:16 -03:00
parent 4651a2fb91
commit 84b16b2d97
1 changed files with 21 additions and 36 deletions

View File

@ -74,46 +74,31 @@ const ChangeAvatarView = () => {
}; };
const submit = async () => { const submit = async () => {
try {
setSaving(true);
if (!fromUser && room?.rid) { if (!fromUser && room?.rid) {
try { // Change Rooms Avatar
setSaving(true);
await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data }); await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data });
setSaving(false); } else if (avatar?.url) {
avatarUrl.current = ''; // Change User's Avatar
return navigation.goBack();
} catch (e) {
log(e);
setSaving(false);
return handleError(e, 'setAvatarFromService', 'changing_avatar');
}
}
// Change Users Avatar
if (avatar?.url) {
try {
setSaving(true);
await Services.setAvatarFromService(avatar); await Services.setAvatarFromService(avatar);
} else if (textAvatar) {
// Change User's Avatar
await Services.resetAvatar(user.id);
}
setSaving(false); setSaving(false);
avatarUrl.current = ''; avatarUrl.current = '';
return navigation.goBack(); return navigation.goBack();
} catch (e) { } catch (e) {
log(e); log(e);
setSaving(false); setSaving(false);
return handleError(e, 'setAvatarFromService', 'changing_avatar'); if (!fromUser && room?.rid) {
return handleError(e, 'saveRoomSettings', 'changing_avatar');
} }
}
if (textAvatar) { if (textAvatar) {
try { return handleError(e, 'resetAvatar', 'changing_avatar');
setSaving(true);
await Services.resetAvatar(user.id);
setSaving(false);
avatarUrl.current = '';
return navigation.goBack();
} catch (e) {
setSaving(false);
handleError(e, 'resetAvatar', 'changing_avatar');
} }
return handleError(e, 'setAvatarFromService', 'changing_avatar');
} }
}; };