-
Notifications
You must be signed in to change notification settings - Fork 81
/
RedemptionsMenu.tsx
207 lines (186 loc) · 6.86 KB
/
RedemptionsMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Address, IDAOState, IProposalState, IRewardState, Proposal, Reward } from "@daostack/client";
import { enableWeb3ProviderAndWarn, getArc } from "arc";
import * as arcActions from "actions/arcActions";
import BN = require("bn.js");
import withSubscription, { ISubscriptionProps } from "components/Shared/withSubscription";
import ActionButton from "components/Proposal/ActionButton";
import RedemptionsString from "components/Proposal/RedemptionsString";
import ProposalSummary from "components/Proposal/ProposalSummary";
import { humanProposalTitle } from "lib/util";
import * as React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { IRootState } from "reducers";
import { showNotification } from "reducers/notifications";
import { IProfileState } from "reducers/profilesReducer";
import { combineLatest, concat, Observable, of } from "rxjs";
import { defaultIfEmpty, map, mergeMap } from "rxjs/operators";
import * as css from "./RedemptionsMenu.scss";
interface IExternalProps {
redeemableProposals: any[];
handleClose: () => void;
}
interface IStateProps {
currentAccountAddress: Address;
}
const mapStateToProps = (state: IRootState, ownProps: IExternalProps) => {
return {
currentAccountAddress: state.web3.currentAccountAddress,
...ownProps,
};
};
interface IDispatchProps {
redeemProposal: typeof arcActions.redeemProposal;
showNotification: typeof showNotification;
}
const mapDispatchToProps = {
redeemProposal: arcActions.redeemProposal,
showNotification,
};
type IProps = IExternalProps & IStateProps & IDispatchProps & ISubscriptionProps<IProposalState[]>;
class RedemptionsMenu extends React.Component<IProps, null> {
public render(): RenderOutput {
const { currentAccountAddress, handleClose, data: redeemableProposals } = this.props;
return <div className={css.menu}>
<div className={css.proposals}>
{redeemableProposals.length > 0 ?
redeemableProposals.map(proposal => (
<MenuItem
key={proposal.id}
proposal={proposal}
currentAccountAddress={currentAccountAddress}
/>
))
: <div className={css.empty}>
<h2>Nothing to redeem</h2>
<img src="/assets/images/empty-redemptions.svg"/>
</div>}
</div>
<div className={css.actions}>
<Link
to="/redemptions"
onClick={handleClose}
data-test-id="viewAllRedemptionsLink"
className={redeemableProposals.length === 0 ? css.disabled : ""}
>
View all >
</Link>
<button
className={css.redeemAllButton}
onClick={this.redeemAll.bind(this)}
disabled={redeemableProposals.length === 0}
>
<img src="/assets/images/Icon/redeem.svg" />
Redeem all
</button>
</div>
</div>;
}
private async redeemAll() {
const {
currentAccountAddress,
data: redeemableProposals,
redeemProposal,
showNotification,
} = this.props;
if (!(await enableWeb3ProviderAndWarn(showNotification.bind(this)))) { return; }
redeemableProposals.forEach(proposal => {
redeemProposal(proposal.dao.id, proposal.id, currentAccountAddress);
});
}
}
const SubscribedRedemptionsMenu = withSubscription({
wrappedComponent: RedemptionsMenu,
loadingComponent: <div className={css.menu}>Loading...</div>,
errorComponent: (props) => <div className={css.menu}>{ props.error.message }</div>,
checkForUpdate: ["redeemableProposals"],
createObservable: (props: IExternalProps) => {
const arc = getArc();
return combineLatest(
props.redeemableProposals.map(proposalData => (
new Proposal(proposalData.id, arc).state()
))
).pipe(defaultIfEmpty([]));
},
});
interface IMenuItemProps {
proposal: IProposalState;
currentAccountAddress: Address;
}
class MenuItem extends React.Component<IMenuItemProps, null> {
public render(): RenderOutput {
const { proposal } = this.props;
return <div className={css.proposal}>
<div className={css.title}>
<Link to={"/dao/" + proposal.dao.id + "/proposal/" + proposal.id}>
<span>{humanProposalTitle(proposal)}</span>
<img src="/assets/images/Icon/Open.svg" />
</Link>
</div>
<ConnectedMenuItemContent {...this.props} />
</div>;
}
}
interface IMenuItemContentStateProps {
beneficiaryProfile?: IProfileState;
}
const mapStateToItemContentProps = (state: IRootState, ownProps: IMenuItemProps) => {
const { proposal } = ownProps;
return {
...ownProps,
beneficiaryProfile: proposal.contributionReward ? state.profiles[proposal.contributionReward.beneficiary] : null,
};
};
type IMenuItemContentProps = IMenuItemProps & IMenuItemContentStateProps & ISubscriptionProps<[IDAOState, BN, IRewardState]>;
class MenuItemContent extends React.Component<IMenuItemContentProps, null> {
public render(): RenderOutput {
const { beneficiaryProfile, currentAccountAddress, data, proposal } = this.props;
const [dao, daoEthBalance, reward] = data;
const rewards = reward && [reward] || [];
return <React.Fragment>
<ProposalSummary
proposal={proposal}
dao={dao}
beneficiaryProfile={beneficiaryProfile}
detailView={false}
/>
<div className={css.rewards}>
<RedemptionsString
currentAccountAddress={currentAccountAddress}
dao={dao}
proposal={proposal}
rewards={rewards}
/>
</div>
<div className={css.redeemButton}>
<ActionButton
currentAccountAddress={currentAccountAddress}
daoState={dao}
daoEthBalance={daoEthBalance}
expanded
expired
proposalState={proposal}
rewards={reward}
/>
</div>
</React.Fragment>;
}
}
const SubscribedMenuItemContent = withSubscription({
wrappedComponent: MenuItemContent,
loadingComponent: <div>Loading...</div>,
errorComponent: (props) => <div>{ props.error.message }</div>,
checkForUpdate: [], // Parent component will rerender anyway.
createObservable: (props: IMenuItemProps) => {
const { currentAccountAddress, proposal } = props;
const arc = getArc();
const dao = arc.dao(proposal.dao.id);
const ethBalance = concat(of(new BN("0")), dao.ethBalance());
const rewards = proposal.proposal.rewards({ where: { beneficiary: currentAccountAddress }})
.pipe(map((rewards: Reward[]): Reward => rewards.length === 1 && rewards[0] || null))
.pipe(mergeMap(((reward: Reward): Observable<IRewardState> => reward ? reward.state() : of(null))));
return combineLatest(dao.state(), ethBalance, rewards);
},
});
const ConnectedMenuItemContent = connect(mapStateToItemContentProps, null)(SubscribedMenuItemContent);
export default connect(mapStateToProps, mapDispatchToProps)(SubscribedRedemptionsMenu);