lbry-desktop/ui/js/component/reward-link.js

110 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import React from "react";
import lbry from "lbry";
import { Icon } from "component/common";
import Modal from "component/modal";
import rewards from "rewards";
import Link from "component/link";
2017-05-02 07:31:51 +02:00
2017-05-17 10:10:25 +02:00
export class RewardLink extends React.Component {
static propTypes = {
2017-05-02 07:31:51 +02:00
type: React.PropTypes.string.isRequired,
claimed: React.PropTypes.bool,
onRewardClaim: React.PropTypes.func,
2017-06-06 23:19:12 +02:00
onRewardFailure: React.PropTypes.func,
};
2017-05-17 10:10:25 +02:00
constructor(props) {
super(props);
this.state = {
claimable: true,
pending: false,
2017-06-06 23:19:12 +02:00
errorMessage: null,
2017-05-17 10:10:25 +02:00
};
}
refreshClaimable() {
2017-06-06 23:19:12 +02:00
switch (this.props.type) {
case "new_user":
2017-05-02 07:31:51 +02:00
this.setState({ claimable: true });
return;
2017-06-06 23:19:12 +02:00
case "first_publish":
lbry.claim_list_mine().then(list => {
2017-05-02 07:31:51 +02:00
this.setState({
2017-06-06 23:19:12 +02:00
claimable: list.length > 0,
});
2017-05-21 16:42:34 +02:00
});
2017-05-02 07:31:51 +02:00
return;
}
2017-05-17 10:10:25 +02:00
}
componentWillMount() {
2017-05-02 07:31:51 +02:00
this.refreshClaimable();
2017-05-17 10:10:25 +02:00
}
claimReward() {
2017-05-02 07:31:51 +02:00
this.setState({
2017-06-06 23:19:12 +02:00
pending: true,
});
2017-05-19 18:17:19 +02:00
2017-06-06 23:19:12 +02:00
rewards
.claimReward(this.props.type)
.then(reward => {
this.setState({
pending: false,
errorMessage: null,
});
if (this.props.onRewardClaim) {
this.props.onRewardClaim(reward);
}
2017-05-02 07:31:51 +02:00
})
2017-06-06 23:19:12 +02:00
.catch(error => {
this.setState({
errorMessage: error.message,
pending: false,
});
});
2017-05-17 10:10:25 +02:00
}
clearError() {
2017-05-02 07:31:51 +02:00
if (this.props.onRewardFailure) {
2017-06-06 23:19:12 +02:00
this.props.onRewardFailure();
2017-05-02 07:31:51 +02:00
}
this.setState({
2017-06-06 23:19:12 +02:00
errorMessage: null,
});
2017-05-17 10:10:25 +02:00
}
render() {
2017-05-02 07:31:51 +02:00
return (
<div className="reward-link">
{this.props.claimed
? <span><Icon icon="icon-check" /> {__("Reward claimed.")}</span>
2017-06-06 23:19:12 +02:00
: <Link
button={this.props.button ? this.props.button : "alt"}
disabled={this.state.pending || !this.state.claimable}
label={
this.state.pending ? __("Claiming...") : __("Claim Reward")
}
onClick={() => {
this.claimReward();
}}
/>}
{this.state.errorMessage
? <Modal
isOpen={true}
contentLabel={__("Reward Claim Error")}
className="error-modal"
onConfirmed={() => {
this.clearError();
}}
>
{this.state.errorMessage}
</Modal>
: ""}
2017-05-02 07:31:51 +02:00
</div>
);
}
}