Creates new selectors that are more consistent with the rest of the redux selectors for claims

This commit is contained in:
Oleg Silkin 2020-01-09 21:29:16 -05:00
parent b65149fe00
commit 1038ff16c8

View file

@ -5,9 +5,37 @@ const selectState = state => state.comments || {};
export const selectCommentsById = createSelector(
selectState,
state => state.byId || {}
state => state.commentById || {}
);
export const selectCommentsByClaimId = createSelector(
selectState,
selectCommentsById,
(state, byId) => {
const byClaimId = state.byId || {};
const comments = {};
// for every claimId -> commentId, put comments in the object
Object.keys(byClaimId).forEach(claimId => {
// get all the commentIds that commented on this ClaimId
const commentIds = byClaimId[claimId];
// map a new array of comments by the claimId
comments[claimId] = Array(commentIds === null ? 0 : commentIds.length);
for (let i = 0; i < commentIds.length; i++) {
comments[claimId][i] = byId[commentIds[i]];
}
});
return comments;
}
);
// previously this used a mapping from claimId -> Array<Comments>
/* export const selectCommentsById = createSelector(
selectState,
state => state.byId || {}
); */
export const selectCommentsByUri = createSelector(
selectState,
state => {
@ -21,16 +49,17 @@ export const selectCommentsByUri = createSelector(
comments[uri] = claimId;
}
});
return comments;
}
);
export const makeSelectCommentsForUri = (uri: string) =>
createSelector(
selectCommentsById,
selectCommentsByClaimId,
selectCommentsByUri,
(byId, byUri) => {
(byClaimId, byUri) => {
const claimId = byUri[uri];
return byId && byId[claimId];
return byClaimId && byClaimId[claimId];
}
);