forked from webclinic017/passiv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractiveBrokersOauthPage.tsx
198 lines (187 loc) · 6.17 KB
/
InteractiveBrokersOauthPage.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
import React, { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { Redirect, Link } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSpinner } from '@fortawesome/free-solid-svg-icons';
import { postData } from '../api';
import { reloadEverything } from '../actions';
import ShadowBox from '../styled/ShadowBox';
import { H1, P } from '../styled/GlobalElements';
import { Button } from '../styled/Button';
import { Step } from '../styled/SignupSteps';
import { selectQueryTokens } from '../selectors/router';
import { push } from 'connected-react-router';
import { Error } from '../types/groupInfo';
const InteractiveBrokersOauthPage = () => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error>();
const [success, setSuccess] = useState(false);
const queryParams = useSelector(selectQueryTokens);
const dispatch = useDispatch();
useEffect(() => {
const oauth_token = queryParams.oauth_token;
const oauth_verifier = queryParams.oauth_verifier;
if (oauth_token === null) {
setLoading(false);
setError({ code: '0000' });
} else {
setLoading(true);
postData('/api/v1/brokerages/authComplete/', {
oauth_token: oauth_token,
oauth_verifier: oauth_verifier,
})
.then(() => {
dispatch(reloadEverything());
setTimeout(() => {
setLoading(false);
setSuccess(true);
}, 1000);
})
.catch(error => {
setLoading(false);
setError(error.response.data);
});
}
}, [dispatch, queryParams]);
// if we're done, redirect the user to the dashboard
if (success) {
return <Redirect to="/app/setup-groups" />;
}
let overrideError = false;
let errorDisplay = null;
if (error) {
switch (error.code) {
case '1006':
errorDisplay = (
<P>This connection code is invalid, please try again.</P>
);
break;
case '1007':
errorDisplay = (
<P>This connection code has expired, please try again.</P>
);
break;
case '1017':
errorDisplay = (
<P>
An identical connection already exists, please update or delete the
existing connection.
</P>
);
break;
case '1023':
errorDisplay = (
<P>
The brokerage account linked to this connection is different from
the account you're trying to link. This is not allowed because it
would leave existing portfolios without a functional connection.
Instead, create a new connection and delete this one if desired.
</P>
);
break;
case '1046':
errorDisplay = (
<P>
Passiv is not presently available to Canadian accounts at
Interactive Brokers. We apologize for the inconvenience, but this is
a business decision by Interactive Brokers Canada and not something
we can resolve without their approval.
</P>
);
break;
case '1049':
overrideError = true;
errorDisplay = (
<React.Fragment>
<P>
We have successfully connected your Interactive Brokers account,
but there is a 24-hour delay before your account information will
be available. You will receive an email letting you know when your
account data has been successfully synced.
</P>
<P>
If you don't receive an email within 2 days, please try again or{' '}
<Link to="/app/help">contact support</Link>.
</P>
</React.Fragment>
);
break;
case '1053':
errorDisplay = (
<React.Fragment>
<P>
Interactive Brokers Canada is not supported by Passiv due to our
exclusivity agreement with Questrade. Please consider transferring
this account to Questrade in order to use it with Passiv.
</P>
<P>
If you believe you have received this message in error, please{' '}
<Link to="/app/help">contact support</Link> and describe your
situation.
</P>
</React.Fragment>
);
break;
case '0000':
errorDisplay = (
<P>
No access code was provided by Interactive Brokers. Did you approve
the connection request?
</P>
);
break;
default:
errorDisplay = (
<React.Fragment>
<P>
We encountered an unexpected error while attempting to establish a
connection. Please try again later or{' '}
<Link to="/app/help">contact support</Link> if this persists.
</P>
<P>
Note that in order for a connection to be established, you must
make sure that your brokerage account is fully opened and funded.
If your brokerage account is new or the initial funding has not
been finalized by the brokerage, then you may not be able to link
your account to Passiv until it is fully opened.
</P>
</React.Fragment>
);
break;
}
}
let result = null;
if (loading === true) {
result = (
<React.Fragment>
<Step>
Establishing connection to Interactive Brokers...{' '}
<FontAwesomeIcon icon={faSpinner} spin />
</Step>
</React.Fragment>
);
} else {
result = (
<React.Fragment>
{overrideError ? (
<Step>Connection successful</Step>
) : (
<Step>Failed to establish connection :(</Step>
)}
<ShadowBox>
{errorDisplay}
<Button onClick={() => dispatch(push('/app/settings'))}>
Settings
</Button>
</ShadowBox>
</React.Fragment>
);
}
return (
<ShadowBox background="#04a287">
<H1 color="white">SETUP</H1>
{result}
</ShadowBox>
);
};
export default InteractiveBrokersOauthPage;