@livelike/react-native
Version:
LiveLike React Native package
73 lines (69 loc) • 2.13 kB
text/typescript
import {
addUserReaction,
hasDebugLogger,
removeUserReaction,
} from '@livelike/javascript';
import {
UpdateUserReactionActionArg,
userReactionStoreActions,
} from '../store';
import { useAnalytics } from './useAnalytics';
import { useCallback } from 'react';
export type UseUserReactionActionsArg = {
targetGroupId: string;
targetId: string;
};
export function useUserReactionActions({
targetGroupId,
targetId,
}: UseUserReactionActionsArg) {
const { trackEvent } = useAnalytics();
const removeReaction = useCallback(
(actionArgs: UpdateUserReactionActionArg, selfReactionId: string) => {
userReactionStoreActions.removeUserReactionAction(actionArgs);
removeUserReaction({
reactionSpaceId: actionArgs.reactionSpaceId,
userReactionId: selfReactionId,
})
.then((res) => {
trackEvent('Reaction Removed', {
targetGroupId,
reactionId: actionArgs.userReaction.reaction_id,
reactedById: actionArgs.profileId,
});
return res;
})
.catch((error) => {
userReactionStoreActions.addUserReactionAction(actionArgs);
hasDebugLogger() && console.error(error);
return error;
});
},
[targetGroupId, targetId]
);
const addReaction = useCallback(
(actionArgs: UpdateUserReactionActionArg) => {
userReactionStoreActions.addUserReactionAction(actionArgs);
addUserReaction({
reactionSpaceId: actionArgs.reactionSpaceId,
targetId,
reactionId: actionArgs.userReaction.reaction_id,
})
.then((res) => {
trackEvent('Reaction Added', {
targetGroupId,
reactionId: actionArgs.userReaction.reaction_id,
reactedById: actionArgs.profileId,
});
return res;
})
.catch((error) => {
userReactionStoreActions.removeUserReactionAction(actionArgs);
hasDebugLogger() && console.error(error);
return error;
});
},
[targetGroupId, targetId]
);
return { addReaction, removeReaction };
}