@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
96 lines (83 loc) • 2.58 kB
text/typescript
import { getActiveClient } from '~/client/api';
import { pullFromCache } from '~/cache/api';
import { fireEvent } from '~/core/events';
import { ASCApiError } from '~/core/errors';
export const removeReaction = async (
referenceType: Amity.Reaction['referenceType'],
referenceId: Amity.Reaction['referenceId'],
reactionName: Amity.InternalReactor['reactionName'],
referenceVersion?: number,
): Promise<boolean> => {
const client = getActiveClient();
client.log('reaction/removeReaction', {
referenceId,
referenceType,
reactionName,
});
if (!['post', 'comment', 'story', 'message'].includes(referenceType))
throw new ASCApiError(
'The reference type is not valid. It should be one of post, comment, story, or message',
Amity.ServerError.BAD_REQUEST,
Amity.ErrorLevel.ERROR,
);
const { data } = await client.http.delete<{ removedId: string }>(`/api/v2/reactions`, {
data: {
referenceId,
referenceType,
reactionName,
referenceVersion,
},
});
if (client.cache) {
const model = pullFromCache<Amity.Models[Amity.ReactableType]>([
referenceType,
'get',
referenceId,
]);
if (!model) return true;
const updatedModel = {
...model.data,
reactionsCount: Math.max(0, model.data.reactionsCount - 1),
myReactions: (model.data.myReactions ?? []).filter(item => item !== reactionName),
reactions: {
...model.data.reactions,
[reactionName]: Math.max(0, (model.data.reactions[reactionName] ?? 0) - 1),
},
updatedAt: new Date().toISOString(),
} as Amity.Models[Amity.ReactableType];
if (referenceType === 'comment') {
fireEvent('local.comment.removeReaction', {
comment: updatedModel as Amity.InternalComment,
reactor: {
reactionId: data.removedId,
reactionName,
userId: client.userId!,
},
});
return true;
}
if (referenceType === 'post') {
fireEvent('local.post.removeReaction', {
post: updatedModel as Amity.InternalPost,
reactor: {
reactionId: data.removedId,
reactionName,
userId: client.userId!,
},
});
return true;
}
if (referenceType === 'story') {
fireEvent('local.story.reactionAdded', {
story: updatedModel as Amity.InternalStory,
reactor: {
userId: client.userId!,
reactionName,
reactionId: data.removedId,
},
});
return true;
}
}
return true;
};