Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 6cd38d4

Browse files
committed
add error handling to updatePortfolio
1 parent bb0c7aa commit 6cd38d4

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

src/javascript/app_2/pages/portfolio/portfolio.jsx

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import React from 'react';
22
import moment from 'moment';
33
import Client from '../../../app/base/client';
44
import BinarySocket from '../../../app/base/socket';
5+
import { buildOauthApps } from '../../../app/common/get_app_details';
56
import { localize } from '../../../_common/localize';
7+
import { getPropertyValue } from '../../../_common/utility';
68
import DataTable from '../../components/elements/data_table.jsx';
79

810
// return transformed array
@@ -34,17 +36,19 @@ const handlePortfolioData = (portfolio_arr) => {
3436
1. Move socket connections to DAO
3537
2. Handle errors
3638
3. Handle empty portfolio
37-
4. Subscribe to transactions to auto update new purchases
38-
5. force to sell the expired contract, in order to remove from portfolio?
39-
6. Resale not offered?
40-
7. updateOAuthApps?
39+
5. Selling both in transactionHandler and updateIndicative?
40+
6. Make tooltip appdetails tooltip
41+
7. Add styling
4142
*/
4243
class Portfolio extends React.PureComponent {
4344
constructor(props) {
4445
super(props);
4546

4647
this.getPortfolioData = this.getPortfolioData.bind(this);
48+
this.transactionResponseHandler = this.transactionResponseHandler.bind(this);
4749
this.updateIndicative = this.updateIndicative.bind(this);
50+
this.updateOAuthApps = this.updateOAuthApps.bind(this);
51+
this.updatePortfolio = this.updatePortfolio.bind(this);
4852

4953
const columns = [
5054
{
@@ -98,6 +102,7 @@ class Portfolio extends React.PureComponent {
98102
columns,
99103
currency,
100104
data_source : [],
105+
error : '',
101106
has_no_contract: false,
102107
};
103108
}
@@ -110,58 +115,91 @@ class Portfolio extends React.PureComponent {
110115
console.log(this.state);
111116
BinarySocket.send({ forget_all: ['proposal_open_contract', 'transaction'] });
112117
}
118+
113119
getPortfolioData() {
114120
BinarySocket.send({ portfolio: 1 }).then((response) => {
115-
// Handle error here
116-
if (response.portfolio.contracts && response.portfolio.contracts.length > 0) {
117-
const formatted_transactions = handlePortfolioData(response.portfolio.contracts);
118-
this.setState({
119-
data_source: [...this.state.data_source, ...formatted_transactions],
120-
});
121-
BinarySocket.send(
122-
{ proposal_open_contract: 1, subscribe: 1 },
123-
{ callback: this.updateIndicative }
124-
);
125-
} else {
126-
// empty portfolio
127-
}
121+
this.updatePortfolio(response);
122+
});
123+
BinarySocket.send({ transaction: 1, subscribe: 1 }, { callback: this.transactionResponseHandler });
124+
BinarySocket.send({ oauth_apps: 1 }).then((response) => {
125+
this.updateOAuthApps(response);
128126
});
129127
}
130128

131-
updateIndicative(data) {
129+
transactionResponseHandler(response) {
130+
// handle error
131+
if (response.transaction.action === 'buy') {
132+
BinarySocket.send({ portfolio: 1 }).then((res) => {
133+
this.updatePortfolio(res);
134+
});
135+
} else if (response.transaction.action === 'sell') {
136+
// removeContract(response.transaction.contract_id);
137+
}
138+
}
139+
140+
updateIndicative(response) {
132141
// handle error here
133142
let data_source = this.state.data_source.slice();
134-
const proposal = data.proposal_open_contract;
143+
const proposal = response.proposal_open_contract;
135144
// force to sell the expired contract, in order to remove from portfolio
136145
if (+proposal.is_settleable === 1 && !proposal.is_sold) {
137146
BinarySocket.send({ sell_expired: 1 });
138147
}
139148
if (+proposal.is_sold === 1) {
140-
data_source = data_source.filter((ds) => ds.id !== +proposal.contract_id);
149+
data_source = data_source.filter((portfolio_item) => portfolio_item.id !== +proposal.contract_id);
141150
} else {
142-
data_source.forEach(ds => {
143-
if (ds.id === +proposal.contract_id) {
151+
data_source.forEach(portfolio_item => {
152+
if (portfolio_item.id === +proposal.contract_id) {
144153
const amount = parseFloat(proposal.bid_price);
145154
let style;
146155
if (+proposal.is_valid_to_sell === 1) {
147-
style = proposal.bid_price > ds.indicative.amount ? 'price_moved_up' : 'price_moved_down';
156+
style = proposal.bid_price > portfolio_item.indicative.amount ? 'price_moved_up' : 'price_moved_down';
148157
} else {
149158
style = 'no_resale';
150159
}
151-
ds.indicative = { style, amount };
160+
portfolio_item.indicative = { style, amount };
152161
}
153162
});
154163
}
155164
this.setState({ data_source });
156165
}
157166

167+
updateOAuthApps = (response) => {
168+
console.log(response);
169+
const oauth_apps = buildOauthApps(response);
170+
console.log('oauth_apps: ', oauth_apps);
171+
// GetAppDetails.addTooltip(oauth_apps);
172+
};
173+
174+
updatePortfolio(response) {
175+
if (getPropertyValue(response, 'error')) {
176+
console.log('error: ', response);
177+
this.setState({ error: response.error.message });
178+
return;
179+
}
180+
if (response.portfolio.contracts && response.portfolio.contracts.length > 0) {
181+
const data_source = handlePortfolioData(response.portfolio.contracts);
182+
183+
this.setState({ data_source });
184+
BinarySocket.send(
185+
{ proposal_open_contract: 1, subscribe: 1 },
186+
{ callback: this.updateIndicative }
187+
);
188+
} else {
189+
// empty portfolio
190+
}
191+
}
192+
158193
render() {
159194
return (
160-
<DataTable
161-
{...this.props}
162-
data_source={this.state.data_source}
163-
columns={this.state.columns}
164-
/>
195+
<div>
196+
{this.state.error && <div>{this.state.error}</div>}
197+
<DataTable
198+
{...this.props}
199+
data_source={this.state.data_source}
200+
columns={this.state.columns}
201+
/>
202+
</div>
165203
);
166204
}
167205
};

0 commit comments

Comments
 (0)