lbry-desktop/src/renderer/redux/reducers/content.js

102 lines
2.5 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
2017-04-23 11:56:50 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
const defaultState = {
playingUri: null,
2017-07-30 00:56:08 +02:00
rewardedContentClaimIds: [],
channelClaimCounts: {},
positions: {},
};
2017-04-23 11:56:50 +02:00
reducers[ACTIONS.FETCH_FEATURED_CONTENT_STARTED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
fetchingFeaturedContent: true,
});
2017-04-23 11:56:50 +02:00
reducers[ACTIONS.FETCH_FEATURED_CONTENT_COMPLETED] = (state, action) => {
2017-06-06 23:19:12 +02:00
const { uris, success } = action.data;
2017-05-04 05:44:08 +02:00
2017-04-23 11:56:50 +02:00
return Object.assign({}, state, {
fetchingFeaturedContent: false,
2017-05-04 05:44:08 +02:00
fetchingFeaturedContentFailed: !success,
2017-06-06 23:19:12 +02:00
featuredUris: uris,
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 11:56:50 +02:00
reducers[ACTIONS.FETCH_REWARD_CONTENT_COMPLETED] = (state, action) => {
const { claimIds } = action.data;
return Object.assign({}, state, {
2017-07-30 00:56:08 +02:00
rewardedContentClaimIds: claimIds,
});
};
reducers[ACTIONS.RESOLVE_URIS_STARTED] = (state, action) => {
2017-12-13 22:36:30 +01:00
const { uris } = action.data;
2017-04-23 16:01:00 +02:00
2017-06-06 23:19:12 +02:00
const oldResolving = state.resolvingUris || [];
const newResolving = Object.assign([], oldResolving);
2017-04-23 16:01:00 +02:00
uris.forEach(uri => {
if (!newResolving.includes(uri)) {
newResolving.push(uri);
}
});
2017-04-23 16:01:00 +02:00
return Object.assign({}, state, {
resolvingUris: newResolving,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[ACTIONS.RESOLVE_URIS_COMPLETED] = (state, action) => {
const { resolveInfo } = action.data;
const channelClaimCounts = Object.assign({}, state.channelClaimCounts);
Object.entries(resolveInfo).forEach(([uri, { certificate, claimsInChannel }]) => {
if (certificate && !Number.isNaN(claimsInChannel)) {
channelClaimCounts[uri] = claimsInChannel;
}
});
return Object.assign({}, state, {
channelClaimCounts,
resolvingUris: (state.resolvingUris || []).filter(uri => !resolveInfo[uri]),
});
};
reducers[ACTIONS.SET_PLAYING_URI] = (state, action) =>
2017-12-13 22:36:30 +01:00
Object.assign({}, state, {
playingUri: action.data.uri,
});
reducers[ACTIONS.FETCH_CHANNEL_CLAIM_COUNT_COMPLETED] = (state, action) => {
const channelClaimCounts = Object.assign({}, state.channelClaimCounts);
2017-08-24 23:12:23 +02:00
const { uri, totalClaims } = action.data;
2017-07-17 08:06:04 +02:00
channelClaimCounts[uri] = totalClaims;
2017-07-17 08:06:04 +02:00
return Object.assign({}, state, {
channelClaimCounts,
2017-07-17 08:06:04 +02:00
});
};
reducers[ACTIONS.SET_CONTENT_POSITION] = (state, action) => {
const { claimId, outpoint, position } = action.data;
return {
...state,
positions: {
...state.positions,
[claimId]: {
...state.positions[claimId],
[outpoint]: position,
},
},
};
};
2017-04-23 11:56:50 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}