lbry-desktop/ui/js/reducers/rewards.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-06-08 23:15:34 +02:00
const reducers = {};
const defaultState = {
fetching: false,
2017-06-08 23:15:34 +02:00
rewardsByType: {},
claimPendingByType: {},
2017-06-08 23:15:34 +02:00
claimErrorsByType: {},
};
2017-05-26 10:53:32 +02:00
reducers[types.FETCH_REWARDS_STARTED] = function(state, action) {
2017-06-08 23:15:34 +02:00
return Object.assign({}, state, {
2017-05-26 10:53:32 +02:00
fetching: true,
2017-06-08 23:15:34 +02:00
});
};
2017-05-26 10:53:32 +02:00
reducers[types.FETCH_REWARDS_COMPLETED] = function(state, action) {
2017-06-08 23:15:34 +02:00
const { userRewards } = action.data;
const rewardsByType = {};
userRewards.forEach(reward => (rewardsByType[reward.reward_type] = reward));
2017-05-26 10:53:32 +02:00
2017-06-08 23:15:34 +02:00
return Object.assign({}, state, {
rewardsByType: rewardsByType,
fetching: false,
});
};
function setClaimRewardState(state, reward, isClaiming, errorMessage = "") {
const newClaimPendingByType = Object.assign({}, state.claimPendingByType);
const newClaimErrorsByType = Object.assign({}, state.claimErrorsByType);
2017-06-02 17:45:37 +02:00
if (isClaiming) {
2017-06-08 23:15:34 +02:00
newClaimPendingByType[reward.reward_type] = isClaiming;
2017-06-02 17:45:37 +02:00
} else {
2017-06-08 23:15:34 +02:00
delete newClaimPendingByType[reward.reward_type];
2017-06-02 17:45:37 +02:00
}
if (errorMessage) {
2017-06-08 23:15:34 +02:00
newClaimErrorsByType[reward.reward_type] = errorMessage;
2017-06-02 17:45:37 +02:00
} else {
2017-06-08 23:15:34 +02:00
delete newClaimErrorsByType[reward.reward_type];
2017-06-02 17:45:37 +02:00
}
return Object.assign({}, state, {
claimPendingByType: newClaimPendingByType,
claimErrorsByType: newClaimErrorsByType,
2017-06-08 23:15:34 +02:00
});
}
2017-05-26 10:53:32 +02:00
reducers[types.CLAIM_REWARD_STARTED] = function(state, action) {
2017-06-08 23:15:34 +02:00
const { reward } = action.data;
2017-05-26 10:53:32 +02:00
2017-06-08 23:15:34 +02:00
return setClaimRewardState(state, reward, true, "");
};
2017-05-26 10:53:32 +02:00
reducers[types.CLAIM_REWARD_SUCCESS] = function(state, action) {
2017-06-08 23:15:34 +02:00
const { reward } = action.data;
2017-06-08 23:15:34 +02:00
const existingReward = state.rewardsByType[reward.reward_type];
const newReward = Object.assign({}, reward, {
reward_title: existingReward.reward_title,
reward_description: existingReward.reward_description,
});
const rewardsByType = Object.assign({}, state.rewardsByType);
rewardsByType[reward.reward_type] = newReward;
const newState = Object.assign({}, state, { rewardsByType });
return setClaimRewardState(newState, newReward, false, "");
};
2017-05-26 10:53:32 +02:00
reducers[types.CLAIM_REWARD_FAILURE] = function(state, action) {
2017-06-08 23:15:34 +02:00
const { reward, error } = action.data;
2017-05-26 10:53:32 +02:00
2017-06-08 23:15:34 +02:00
return setClaimRewardState(state, reward, false, error ? error.message : "");
};
2017-05-26 10:53:32 +02:00
reducers[types.CLAIM_REWARD_CLEAR_ERROR] = function(state, action) {
2017-06-08 23:15:34 +02:00
const { reward } = action.data;
2017-06-08 23:15:34 +02:00
return setClaimRewardState(
state,
reward,
state.claimPendingByType[reward.reward_type],
""
);
};
2017-05-26 10:53:32 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}