lbry-desktop/src/renderer/redux/selectors/subscriptions.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

import { createSelector } from 'reselect';
import { selectAllClaimsByChannel, selectClaimsById } from './claims';
2017-12-08 21:14:35 +01:00
// get the entire subscriptions state
const selectState = state => state.subscriptions || {};
2017-12-08 21:14:35 +01:00
// list of saved channel names and uris
export const selectSubscriptions = createSelector(selectState, state => state.subscriptions);
2017-12-08 21:14:35 +01:00
export const selectSubscriptionsFromClaims = createSelector(
selectAllClaimsByChannel,
selectClaimsById,
selectSubscriptions,
(channelIds, allClaims, savedSubscriptions) => {
// no claims loaded yet
if (!Object.keys(channelIds).length) {
return [];
}
2017-12-13 22:36:30 +01:00
const fetchedSubscriptions = [];
2017-12-08 21:14:35 +01:00
savedSubscriptions.forEach(subscription => {
let channelClaims = [];
// if subscribed channel has content
if (channelIds[subscription.uri]) {
// This will need to be more robust, we will want to be able to load more than the first page
const pageOneChannelIds = channelIds[subscription.uri]['1'];
2017-12-08 21:14:35 +01:00
// we have the channel ids and the corresponding claims
// loop over the list of ids and grab the claim
pageOneChannelIds.forEach(id => {
const grabbedClaim = allClaims[id];
channelClaims.push(grabbedClaim);
});
}
// all we really need is a uri for each claim
channelClaims = channelClaims.map(claim => `${claim.name}#${claim.claim_id}`);
2017-12-08 21:14:35 +01:00
fetchedSubscriptions.push({
claims: channelClaims,
channelName: subscription.channelName,
uri: subscription.uri,
});
});
return fetchedSubscriptions;
}
);