From e39919094ee113190209b454e6f66f03ec5f4d7a Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Wed, 6 Jul 2022 17:05:13 -0300 Subject: [PATCH] [FIX] User status update (#4333) --- app/definitions/rest/v1/users.ts | 2 +- app/lib/methods/helpers/info.ts | 2 +- app/lib/services/restApi.ts | 2 +- app/views/StatusView/index.tsx | 39 +++++++++++++------------------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/app/definitions/rest/v1/users.ts b/app/definitions/rest/v1/users.ts index 4ec9260d..ab12be4d 100644 --- a/app/definitions/rest/v1/users.ts +++ b/app/definitions/rest/v1/users.ts @@ -36,7 +36,7 @@ export type UsersEndpoints = { POST: (params: { name: string; email: string; username: string; pass: string }) => { user: IUserRegistered }; }; 'users.setStatus': { - POST: (params: { status: string; message: string }) => {}; + POST: (params: { status?: string; message?: string }) => {}; }; 'users.updateOwnBasicInfo': { POST: (params: { diff --git a/app/lib/methods/helpers/info.ts b/app/lib/methods/helpers/info.ts index d3b85c33..4971f743 100644 --- a/app/lib/methods/helpers/info.ts +++ b/app/lib/methods/helpers/info.ts @@ -3,7 +3,7 @@ import { Alert } from 'react-native'; import I18n from '../../../i18n'; 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 }); interface IShowConfirmationAlert { title?: string; diff --git a/app/lib/services/restApi.ts b/app/lib/services/restApi.ts index 15e31674..cb97af0a 100644 --- a/app/lib/services/restApi.ts +++ b/app/lib/services/restApi.ts @@ -280,7 +280,7 @@ export const setUserPreferences = (userId: string, data: Partial // RC 1.2.0 - sdk.post('users.setStatus', { status, message }); + sdk.methodCall('setUserStatus', status, message); export const setReaction = (emoji: string, messageId: string) => // RC 0.62.2 diff --git a/app/views/StatusView/index.tsx b/app/views/StatusView/index.tsx index 155cc12a..411da8b3 100644 --- a/app/views/StatusView/index.tsx +++ b/app/views/StatusView/index.tsx @@ -57,7 +57,7 @@ const styles = StyleSheet.create({ } }); -const Status = ({ status, statusText }: { status: IStatus; statusText: string }) => { +const Status = ({ status }: { status: IStatus }) => { const user = useSelector((state: IApplicationState) => getUserSelector(state)); const dispatch = useDispatch(); @@ -70,15 +70,13 @@ const Status = ({ status, statusText }: { status: IStatus; statusText: string }) logEvent(events[key]); if (user.status !== status.id) { try { - const result = await Services.setUserStatus(status.id, statusText); - if (result.success) { - dispatch(setUser({ status: status.id })); - } + await Services.setUserStatus(status.id, user.statusText || ''); + dispatch(setUser({ status: status.id })); } catch (e: any) { const messageError = - e.data && e.data.error.includes('[error-too-many-requests]') - ? I18n.t('error-too-many-requests', { seconds: e.data.error.replace(/\D/g, '') }) - : e.data.errorType; + e.error && e.error.includes('[error-too-many-requests]') + ? I18n.t('error-too-many-requests', { seconds: e.reason.replace(/\D/g, '') }) + : e.reason; showErrorAlert(messageError); logEvent(events.SET_STATUS_FAIL); log(e); @@ -108,7 +106,7 @@ const StatusView = (): React.ReactElement => { const submit = async () => { logEvent(events.STATUS_DONE); if (statusText !== user.statusText) { - await setCustomStatus(statusText); + await setCustomStatus(user.status, statusText); } goBack(); }; @@ -126,23 +124,18 @@ const StatusView = (): React.ReactElement => { setHeader(); }, [statusText, user.status]); - const setCustomStatus = async (statusText: string) => { + const setCustomStatus = async (status: string, statusText: string) => { setLoading(true); try { - const result = await Services.setUserStatus(user.status, statusText); - if (result.success) { - dispatch(setUser({ statusText })); - logEvent(events.STATUS_CUSTOM); - showToast(I18n.t('Status_saved_successfully')); - } else { - logEvent(events.STATUS_CUSTOM_F); - showToast(I18n.t('error-could-not-change-status')); - } + await Services.setUserStatus(status, statusText); + dispatch(setUser({ statusText })); + logEvent(events.STATUS_CUSTOM); + showToast(I18n.t('Status_saved_successfully')); } catch (e: any) { const messageError = - e.data && e.data.error.includes('[error-too-many-requests]') - ? I18n.t('error-too-many-requests', { seconds: e.data.error.replace(/\D/g, '') }) - : e.data.errorType; + e.error && e.error.includes('[error-too-many-requests]') + ? I18n.t('error-too-many-requests', { seconds: e.reason.replace(/\D/g, '') }) + : e.reason; logEvent(events.STATUS_CUSTOM_F); showErrorAlert(messageError); } @@ -156,7 +149,7 @@ const StatusView = (): React.ReactElement => { item.id} - renderItem={({ item }) => } + renderItem={({ item }) => } ListHeaderComponent={ <>