diff --git a/native/chat/reaction-message-utils.js b/native/chat/reaction-message-utils.js new file mode 100644 index 000000000..b74344a7c --- /dev/null +++ b/native/chat/reaction-message-utils.js @@ -0,0 +1,81 @@ +// @flow + +import invariant from 'invariant'; +import Alert from 'react-native/Libraries/Alert/Alert'; + +import { + sendReactionMessage, + sendReactionMessageActionTypes, +} from 'lib/actions/message-actions'; +import type { BindServerCall, DispatchFunctions } from 'lib/utils/action-utils'; + +import type { TooltipRoute } from '../navigation/tooltip.react'; + +function onPressReact( + route: + | TooltipRoute<'TextMessageTooltipModal'> + | TooltipRoute<'MultimediaMessageTooltipModal'> + | TooltipRoute<'RobotextMessageTooltipModal'>, + dispatchFunctions: DispatchFunctions, + bindServerCall: BindServerCall, +) { + const messageID = route.params.item.messageInfo.id; + invariant(messageID, 'messageID should be set'); + + const threadID = route.params.item.threadInfo.id; + invariant(threadID, 'threadID should be set'); + + sendReaction( + messageID, + threadID, + '👍', + 'add_reaction', + dispatchFunctions, + bindServerCall, + ); +} + +function sendReaction( + messageID: string, + threadID: string, + reaction: string, + action: 'add_reaction' | 'remove_reaction', + dispatchFunctions: DispatchFunctions, + bindServerCall: BindServerCall, +) { + const callSendReactionMessage = bindServerCall(sendReactionMessage); + + const reactionMessagePromise = (async () => { + try { + const result = await callSendReactionMessage({ + threadID, + targetMessageID: messageID, + reaction, + action, + }); + return { + serverID: result.id, + threadID, + time: result.newMessageInfo.time, + newMessageInfos: [result.newMessageInfo], + }; + } catch (e) { + Alert.alert( + 'Couldn’t send the reaction', + 'Please try again later', + [{ text: 'OK' }], + { + cancelable: true, + }, + ); + throw e; + } + })(); + + dispatchFunctions.dispatchActionPromise( + sendReactionMessageActionTypes, + reactionMessagePromise, + ); +} + +export { onPressReact };