lbry-desktop/ui/js/redux/reducers/claims.js

192 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-04-28 17:14:44 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
2017-09-17 22:33:52 +02:00
const defaultState = {};
2017-04-28 17:14:44 +02:00
reducers[types.RESOLVE_URIS_COMPLETED] = function(state, action) {
const { resolveInfo } = action.data;
2017-06-04 13:53:26 +02:00
const byUri = Object.assign({}, state.claimsByUri);
const byId = Object.assign({}, state.byId);
2017-05-13 00:50:51 +02:00
for (let [uri, { certificate, claim }] of Object.entries(resolveInfo)) {
if (claim) {
byId[claim.claim_id] = claim;
byUri[uri] = claim.claim_id;
} else if (claim === undefined && certificate !== undefined) {
byId[certificate.claim_id] = certificate;
// Don't point URI at the channel certificate unless it actually is
// a channel URI. This is brittle.
if (!uri.split(certificate.name)[1].match(/\//)) {
byUri[uri] = certificate.claim_id;
} else {
byUri[uri] = null;
}
2017-06-17 19:59:18 +02:00
} else {
byUri[uri] = null;
}
2017-05-13 00:50:51 +02:00
}
return Object.assign({}, state, {
2017-06-04 13:53:26 +02:00
byId,
2017-06-10 19:17:37 +02:00
claimsByUri: byUri,
2017-06-06 23:19:12 +02:00
});
2017-06-10 19:17:37 +02:00
};
2017-05-13 00:50:51 +02:00
reducers[types.FETCH_CLAIM_LIST_MINE_STARTED] = function(state, action) {
return Object.assign({}, state, {
isFetchingClaimListMine: true,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.FETCH_CLAIM_LIST_MINE_COMPLETED] = function(state, action) {
2017-06-10 19:17:37 +02:00
const { claims } = action.data;
2017-06-06 23:19:12 +02:00
const byUri = Object.assign({}, state.claimsByUri);
2017-06-04 13:53:26 +02:00
const byId = Object.assign({}, state.byId);
2017-07-08 10:03:12 +02:00
const pendingById = Object.assign({}, state.pendingById);
const abandoningById = Object.assign({}, state.abandoningById);
claims
.filter(claim => claim.category && claim.category.match(/claim/))
.forEach(claim => {
byId[claim.claim_id] = claim;
2017-07-08 10:03:12 +02:00
const pending = Object.values(pendingById).find(pendingClaim => {
return (
pendingClaim.name == claim.name &&
pendingClaim.channel_name == claim.channel_name
);
});
2017-07-08 10:03:12 +02:00
if (pending) {
delete pendingById[pending.claim_id];
}
});
2017-07-08 10:03:12 +02:00
// Remove old timed out pending publishes
const old = Object.values(pendingById)
.filter(pendingClaim => Date.now() - pendingClaim.time >= 20 * 60 * 1000)
.forEach(pendingClaim => {
delete pendingById[pendingClaim.claim_id];
});
return Object.assign({}, state, {
isFetchingClaimListMine: false,
2017-10-31 15:02:08 +01:00
myClaims: claims,
2017-06-04 13:53:26 +02:00
byId,
2017-07-08 10:03:12 +02:00
pendingById,
2017-06-06 23:19:12 +02:00
});
2017-06-10 19:17:37 +02:00
};
2017-07-10 16:49:12 +02:00
reducers[types.FETCH_CHANNEL_LIST_MINE_STARTED] = function(state, action) {
return Object.assign({}, state, { fetchingMyChannels: true });
};
reducers[types.FETCH_CHANNEL_LIST_MINE_COMPLETED] = function(state, action) {
const { claims } = action.data;
const myChannelClaims = new Set(state.myChannelClaims);
const byId = Object.assign({}, state.byId);
claims.forEach(claim => {
myChannelClaims.add(claim.claim_id);
byId[claims.claim_id] = claim;
});
return Object.assign({}, state, {
byId,
fetchingMyChannels: false,
myChannelClaims,
});
};
2017-05-21 16:42:34 +02:00
2017-07-17 08:06:04 +02:00
reducers[types.FETCH_CHANNEL_CLAIMS_STARTED] = function(state, action) {
const { uri, page } = action.data;
const fetchingChannelClaims = Object.assign({}, state.fetchingChannelClaims);
fetchingChannelClaims[uri] = page;
return Object.assign({}, state, {
fetchingChannelClaims,
});
};
2017-05-13 00:50:51 +02:00
reducers[types.FETCH_CHANNEL_CLAIMS_COMPLETED] = function(state, action) {
2017-07-17 08:06:04 +02:00
const { uri, claims, page } = action.data;
2017-05-13 00:50:51 +02:00
2017-07-17 08:06:04 +02:00
const claimsByChannel = Object.assign({}, state.claimsByChannel);
const byChannel = Object.assign({}, claimsByChannel[uri]);
const allClaimIds = new Set(byChannel["all"]);
const currentPageClaimIds = [];
const byId = Object.assign({}, state.byId);
const fetchingChannelClaims = Object.assign({}, state.fetchingChannelClaims);
2017-05-13 00:50:51 +02:00
if (claims !== undefined) {
2017-07-17 08:06:04 +02:00
claims.forEach(claim => {
allClaimIds.add(claim.claim_id);
currentPageClaimIds.push(claim.claim_id);
byId[claim.claim_id] = claim;
});
2017-05-13 00:50:51 +02:00
}
2017-07-17 08:06:04 +02:00
byChannel["all"] = allClaimIds;
byChannel[page] = currentPageClaimIds;
claimsByChannel[uri] = byChannel;
delete fetchingChannelClaims[uri];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
2017-07-17 08:06:04 +02:00
claimsByChannel,
byId,
fetchingChannelClaims,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
2017-07-08 10:03:12 +02:00
reducers[types.ABANDON_CLAIM_STARTED] = function(state, action) {
const { claimId } = action.data;
const abandoningById = Object.assign({}, state.abandoningById);
abandoningById[claimId] = true;
return Object.assign({}, state, {
abandoningById,
});
};
reducers[types.ABANDON_CLAIM_SUCCEEDED] = function(state, action) {
2017-10-30 17:25:56 +01:00
const { claimId } = action.data;
const byId = Object.assign({}, state.byId);
const claimsByUri = Object.assign({}, state.claimsByUri);
2017-10-30 17:25:56 +01:00
Object.keys(claimsByUri).forEach(uri => {
if (claimsByUri[uri] === claimId) {
delete claimsByUri[uri];
}
});
2017-10-30 17:25:56 +01:00
delete byId[claimId];
2017-10-24 15:10:27 +02:00
return Object.assign({}, state, {
byId,
claimsByUri,
});
};
2017-06-17 19:59:18 +02:00
reducers[types.CREATE_CHANNEL_COMPLETED] = function(state, action) {
const { channelClaim } = action.data;
const byId = Object.assign({}, state.byId);
const myChannelClaims = new Set(state.myChannelClaims);
byId[channelClaim.claim_id] = channelClaim;
myChannelClaims.add(channelClaim.claim_id);
return Object.assign({}, state, {
byId,
myChannelClaims,
});
};
2017-04-28 17:14:44 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}