@@ -2,7 +2,9 @@ import React from 'react';
2
2
import moment from 'moment' ;
3
3
import Client from '../../../app/base/client' ;
4
4
import BinarySocket from '../../../app/base/socket' ;
5
+ import { buildOauthApps } from '../../../app/common/get_app_details' ;
5
6
import { localize } from '../../../_common/localize' ;
7
+ import { getPropertyValue } from '../../../_common/utility' ;
6
8
import DataTable from '../../components/elements/data_table.jsx' ;
7
9
8
10
// return transformed array
@@ -34,17 +36,19 @@ const handlePortfolioData = (portfolio_arr) => {
34
36
1. Move socket connections to DAO
35
37
2. Handle errors
36
38
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
41
42
*/
42
43
class Portfolio extends React . PureComponent {
43
44
constructor ( props ) {
44
45
super ( props ) ;
45
46
46
47
this . getPortfolioData = this . getPortfolioData . bind ( this ) ;
48
+ this . transactionResponseHandler = this . transactionResponseHandler . bind ( this ) ;
47
49
this . updateIndicative = this . updateIndicative . bind ( this ) ;
50
+ this . updateOAuthApps = this . updateOAuthApps . bind ( this ) ;
51
+ this . updatePortfolio = this . updatePortfolio . bind ( this ) ;
48
52
49
53
const columns = [
50
54
{
@@ -98,6 +102,7 @@ class Portfolio extends React.PureComponent {
98
102
columns,
99
103
currency,
100
104
data_source : [ ] ,
105
+ error : '' ,
101
106
has_no_contract : false ,
102
107
} ;
103
108
}
@@ -110,58 +115,91 @@ class Portfolio extends React.PureComponent {
110
115
console . log ( this . state ) ;
111
116
BinarySocket . send ( { forget_all : [ 'proposal_open_contract' , 'transaction' ] } ) ;
112
117
}
118
+
113
119
getPortfolioData ( ) {
114
120
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 ) ;
128
126
} ) ;
129
127
}
130
128
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 ) {
132
141
// handle error here
133
142
let data_source = this . state . data_source . slice ( ) ;
134
- const proposal = data . proposal_open_contract ;
143
+ const proposal = response . proposal_open_contract ;
135
144
// force to sell the expired contract, in order to remove from portfolio
136
145
if ( + proposal . is_settleable === 1 && ! proposal . is_sold ) {
137
146
BinarySocket . send ( { sell_expired : 1 } ) ;
138
147
}
139
148
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 ) ;
141
150
} 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 ) {
144
153
const amount = parseFloat ( proposal . bid_price ) ;
145
154
let style ;
146
155
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' ;
148
157
} else {
149
158
style = 'no_resale' ;
150
159
}
151
- ds . indicative = { style, amount } ;
160
+ portfolio_item . indicative = { style, amount } ;
152
161
}
153
162
} ) ;
154
163
}
155
164
this . setState ( { data_source } ) ;
156
165
}
157
166
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
+
158
193
render ( ) {
159
194
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 >
165
203
) ;
166
204
}
167
205
} ;
0 commit comments