lbry-desktop/ui/redux/selectors/reactions.js

74 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2020-10-02 21:18:53 +02:00
import * as REACTION_TYPES from 'constants/reactions';
import { createSelector } from 'reselect';
import { makeSelectClaimForUri } from 'lbry-redux';
2021-06-25 22:25:27 +02:00
const selectState = (state) => state.reactions || {};
2020-10-02 21:18:53 +02:00
2021-06-25 22:25:27 +02:00
export const selectReactionsById = createSelector(selectState, (state) => state.reactionsById);
2020-10-02 21:18:53 +02:00
2021-06-25 22:25:27 +02:00
export const selectFetchingReactions = createSelector(selectState, (state) => state.fetchingReactions);
2020-10-02 21:18:53 +02:00
2021-06-25 22:25:27 +02:00
export const makeSelectReactionsForUri = (uri) =>
2020-10-02 21:18:53 +02:00
createSelector(makeSelectClaimForUri(uri), selectReactionsById, (claim, reactionsById) => {
return claim ? reactionsById[claim.claim_id] : {};
});
2021-06-25 22:25:27 +02:00
export const makeSelectMyReactionForUri = (uri) =>
2020-10-02 21:18:53 +02:00
createSelector(makeSelectClaimForUri(uri), makeSelectReactionsForUri(uri), (claim, reactions) => {
2021-06-25 22:25:27 +02:00
if (!claim || !reactions || reactions.my_reactions === null) {
2020-10-02 21:18:53 +02:00
return undefined;
}
2021-06-25 22:25:27 +02:00
const claimId = claim.claim_id;
2020-10-02 21:18:53 +02:00
const myReactions = reactions.my_reactions[claimId];
if (myReactions[REACTION_TYPES.LIKE]) {
return REACTION_TYPES.LIKE;
} else if (myReactions[REACTION_TYPES.DISLIKE]) {
return REACTION_TYPES.DISLIKE;
} else {
// Ignore other types of reactions for now
return undefined;
}
});
2021-06-25 22:25:27 +02:00
export const makeSelectLikeCountForUri = (uri) =>
2020-10-02 21:18:53 +02:00
createSelector(makeSelectClaimForUri(uri), makeSelectReactionsForUri(uri), (claim, reactions) => {
2021-06-25 22:25:27 +02:00
if (!claim || !reactions || reactions.my_reactions === null || reactions.others_reactions === null) {
2020-10-02 21:18:53 +02:00
return 0;
}
2021-06-25 22:25:27 +02:00
const claimId = claim.claim_id;
2020-10-02 21:18:53 +02:00
let count = 0;
if (reactions.others_reactions) {
const likeCount = reactions.others_reactions[claimId][REACTION_TYPES.LIKE] || 0;
count += likeCount;
}
if (reactions.my_reactions) {
const likeCount = reactions.my_reactions[claimId][REACTION_TYPES.LIKE] || 0;
count += likeCount;
}
return count;
});
2021-06-25 22:25:27 +02:00
export const makeSelectDislikeCountForUri = (uri) =>
2020-10-02 21:18:53 +02:00
createSelector(makeSelectClaimForUri(uri), makeSelectReactionsForUri(uri), (claim, reactions) => {
2021-08-06 19:05:30 +02:00
if (!claim || !reactions || reactions.my_reactions === null || reactions.others_reactions === null) {
2020-10-02 21:18:53 +02:00
return 0;
}
2021-08-06 19:05:30 +02:00
const claimId = claim.claim_id;
2020-10-02 21:18:53 +02:00
let count = 0;
if (reactions.others_reactions) {
const dislikeCount = reactions.others_reactions[claimId][REACTION_TYPES.DISLIKE] || 0;
count += dislikeCount;
}
if (reactions.my_reactions) {
const dislikeCount = reactions.my_reactions[claimId][REACTION_TYPES.DISLIKE] || 0;
count += dislikeCount;
}
return count;
});