[MM-42586] Reaction toggle behavior (#7674)

This commit is contained in:
KyeongSoo Kim 2023-11-22 22:05:58 +09:00 committed by GitHub
parent 908e4c08d4
commit 1a59bd9e0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 6 deletions

View file

@ -16,6 +16,35 @@ import {forceLogoutIfNecessary} from './session';
import type {Model} from '@nozbe/watermelondb';
import type PostModel from '@typings/database/models/servers/post';
export async function getIsReactionAlreadyAddedToPost(serverUrl: string, postId: string, emojiName: string) {
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const currentUserId = await getCurrentUserId(database);
const emojiAlias = getEmojiFirstAlias(emojiName);
return await queryReaction(database, emojiAlias, postId, currentUserId).fetchCount() > 0;
} catch (error) {
logDebug('error on getIsReactionAlreadyAddedToPost', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
}
export async function toggleReaction(serverUrl: string, postId: string, emojiName: string) {
try {
const isReactionAlreadyAddedToPost = await getIsReactionAlreadyAddedToPost(serverUrl, postId, emojiName);
if (isReactionAlreadyAddedToPost) {
return removeReaction(serverUrl, postId, emojiName);
}
return addReaction(serverUrl, postId, emojiName);
} catch (error) {
logDebug('error on toggleReaction', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
}
export async function addReaction(serverUrl: string, postId: string, emojiName: string) {
try {
const client = NetworkManager.getClient(serverUrl);

View file

@ -5,7 +5,7 @@ import React, {useCallback, useRef, useState} from 'react';
import {useIntl} from 'react-intl';
import {Keyboard, TouchableOpacity} from 'react-native';
import {addReaction, removeReaction} from '@actions/remote/reactions';
import {addReaction, removeReaction, toggleReaction} from '@actions/remote/reactions';
import CompassIcon from '@components/compass_icon';
import {Screens} from '@constants';
import {MAX_ALLOWED_REACTIONS} from '@constants/emoji';
@ -100,8 +100,8 @@ const Reactions = ({currentUserId, canAddReaction, canRemoveReaction, disabled,
return {reactionsByName, highlightedReactions};
}, [sortedReactions, reactions]);
const handleAddReactionToPost = (emoji: string) => {
addReaction(serverUrl, postId, emoji);
const handleToggleReactionToPost = (emoji: string) => {
toggleReaction(serverUrl, postId, emoji);
};
const handleAddReaction = useCallback(preventDoubleTap(() => {
@ -110,7 +110,7 @@ const Reactions = ({currentUserId, canAddReaction, canRemoveReaction, disabled,
screen: Screens.EMOJI_PICKER,
theme,
title: formatMessage({id: 'mobile.post_info.add_reaction', defaultMessage: 'Add Reaction'}),
props: {onEmojiPress: handleAddReactionToPost},
props: {onEmojiPress: handleToggleReactionToPost},
});
}), [formatMessage, theme]);

View file

@ -5,7 +5,7 @@ import React, {useCallback} from 'react';
import {useIntl} from 'react-intl';
import {useWindowDimensions, View} from 'react-native';
import {addReaction} from '@actions/remote/reactions';
import {toggleReaction} from '@actions/remote/reactions';
import {Screens} from '@constants';
import {
LARGE_CONTAINER_SIZE,
@ -57,7 +57,7 @@ const ReactionBar = ({bottomSheetId, recentEmojis = [], postId}: QuickReactionPr
const handleEmojiPress = useCallback(async (emoji: string) => {
await dismissBottomSheet(bottomSheetId);
addReaction(serverUrl, postId, emoji);
toggleReaction(serverUrl, postId, emoji);
}, [bottomSheetId, postId, serverUrl]);
const openEmojiPicker = useCallback(async () => {