Store claims by id in the state

This commit is contained in:
6ea86b96 2017-06-04 18:53:26 +07:00
parent 059673983c
commit f97ec7b390
2 changed files with 42 additions and 30 deletions

View file

@ -7,23 +7,19 @@ const defaultState = {};
reducers[types.RESOLVE_URI_COMPLETED] = function(state, action) { reducers[types.RESOLVE_URI_COMPLETED] = function(state, action) {
const { uri, certificate, claim } = action.data; const { uri, certificate, claim } = action.data;
const newClaims = Object.assign({}, state.claimsByUri); const byUri = Object.assign({}, state.claimsByUri);
const byId = Object.assign({}, state.byId);
newClaims[uri] = claim; if (claim) {
byId[claim.claim_id] = claim;
//This needs a sanity boost... byUri[uri] = claim.claim_id;
if (certificate !== undefined && claim === undefined) {
const uriParts = lbryuri.parse(uri);
// newChannelClaims[uri] = certificate
if (claim === undefined) {
newClaims[uri] = certificate;
}
} }
return Object.assign({}, state, { return Object.assign({}, state, {
claimsByUri: newClaims, byId,
claimsByUri: byUri
}); });
}; }
reducers[types.RESOLVE_URI_CANCELED] = function(state, action) { reducers[types.RESOLVE_URI_CANCELED] = function(state, action) {
const uri = action.data.uri; const uri = action.data.uri;
@ -41,26 +37,24 @@ reducers[types.FETCH_CLAIM_LIST_MINE_STARTED] = function(state, action) {
}; };
reducers[types.FETCH_CLAIM_LIST_MINE_COMPLETED] = function(state, action) { reducers[types.FETCH_CLAIM_LIST_MINE_COMPLETED] = function(state, action) {
const { claims } = action.data; const {
claims,
} = action.data;
const myClaims = new Set(state.myClaims); const myClaims = new Set(state.myClaims);
const byUri = Object.assign({}, state.claimsByUri); const byUri = Object.assign({}, state.claimsByUri);
const byId = Object.assign({}, state.byId);
claims.forEach(claim => { claims.forEach(claim => {
const uri = lbryuri.build({ myClaims.add(claim.claim_id);
contentName: claim.name, byId[claim.claim_id] = claim;
claimId: claim.claim_id, })
claimSequence: claim.nout,
});
myClaims.add(uri);
byUri[uri] = claim;
});
return Object.assign({}, state, { return Object.assign({}, state, {
isClaimListMinePending: false, isClaimListMinePending: false,
myClaims: myClaims, myClaims: myClaims,
claimsByUri: byUri, byId,
}); });
}; }
// reducers[types.FETCH_CHANNEL_CLAIMS_STARTED] = function(state, action) { // reducers[types.FETCH_CHANNEL_CLAIMS_STARTED] = function(state, action) {
// const { // const {

View file

@ -1,12 +1,30 @@
import { createSelector } from "reselect"; import { createSelector } from "reselect";
import lbryuri from "lbryuri"; import lbryuri from "lbryuri";
export const _selectState = state => state.claims || {}; const _selectState = state => state.claims || {};
export const selectClaimsById = createSelector(
_selectState,
(state) => state.byId || {}
);
export const selectClaimsByUri = createSelector( export const selectClaimsByUri = createSelector(
_selectState, _selectState,
state => state.claimsByUri || {} selectClaimsById,
); (state, byId) => {
const byUri = state.claimsByUri || {};
const claims = {};
Object.keys(byUri).forEach(uri => {
const claimId = byUri[uri];
const claim = byId[claimId];
claims[uri] = claim;
})
return claims;
}
)
export const selectAllClaimsByChannel = createSelector( export const selectAllClaimsByChannel = createSelector(
_selectState, _selectState,
@ -74,14 +92,14 @@ export const selectMyClaims = createSelector(
export const selectMyClaimsOutpoints = createSelector( export const selectMyClaimsOutpoints = createSelector(
selectMyClaims, selectMyClaims,
selectClaimsByUri, selectClaimsById,
(claimIds, byUri) => { (claimIds, byId) => {
const outpoints = []; const outpoints = [];
claimIds.forEach(claimId => { claimIds.forEach(claimId => {
const claim = byUri[claimId]; const claim = byId[claimId];
if (claim) outpoints.push(`${claim.txid}:${claim.nout}`); if (claim) outpoints.push(`${claim.txid}:${claim.nout}`);
}); })
return outpoints; return outpoints;
} }