forked from webclinic017/passiv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestradeOauthPage.tsx
284 lines (274 loc) · 9.72 KB
/
QuestradeOauthPage.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import React, { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { Link } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSpinner } from '@fortawesome/free-solid-svg-icons';
import { push, replace } from 'connected-react-router';
import { postData } from '../api';
import { reloadEverything } from '../actions';
import ShadowBox from '../styled/ShadowBox';
import { H1, H2, P, BulletUL, A } from '../styled/GlobalElements';
import { Button } from '../styled/Button';
import { Step } from '../styled/SignupSteps';
import { selectQueryTokens } from '../selectors/router';
import { selectBrokerages, selectMaintenanceBrokerages } from '../selectors';
import { selectIsPaid } from '../selectors/subscription';
import { selectQuestradeOfferFeature } from '../selectors/features';
import { Error } from '../types/groupInfo';
import styled from '@emotion/styled';
import { Brokerage as BrokerageType } from '../types/brokerage';
import { toast } from 'react-toastify';
const BulletULPadded = styled(BulletUL)`
padding-left: 20px;
padding-top: 30px;
padding-bottom: 10px;
`;
const H2Padded = styled(H2)`
padding-bottom: 20px;
`;
const QuestradeOauthPage = () => {
const [loading, setLoading] = useState(true);
const [showUpgradeOffer, setShowUpgradeOffer] = useState(false);
const [error, setError] = useState<Error>();
const queryParams = useSelector(selectQueryTokens);
const isPaid = useSelector(selectIsPaid);
const questradeOfferFeatureActive = useSelector(selectQuestradeOfferFeature);
const dispatch = useDispatch();
const brokerages = useSelector(selectBrokerages);
const maintenanceBrokerages = useSelector(selectMaintenanceBrokerages);
useEffect(() => {
const token = queryParams.code;
if (token === null) {
setLoading(false);
setError({ code: '0000' });
} else {
postData('/api/v1/brokerages/authComplete/', { token: token })
.then(() => {
dispatch(reloadEverything());
if (isPaid || !questradeOfferFeatureActive) {
setTimeout(() => {
dispatch(replace('/app/setup-groups'));
}, 1000);
} else {
setLoading(false);
setShowUpgradeOffer(true);
}
})
.catch((error) => {
setLoading(false);
setError(error.response.data);
});
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const checkBrokerageMaintenance = (brokerage: BrokerageType) => {
if (
maintenanceBrokerages.find((b: BrokerageType) => b.id === brokerage.id)
) {
return true;
} else {
return false;
}
};
const startConnection = () => {
const brokerage =
brokerages &&
brokerages.find((brokerage) => brokerage.name === 'Questrade');
if (brokerage) {
if (checkBrokerageMaintenance(brokerage) === true) {
toast.error(
`${brokerage.name} is currently undergoing maintenance and cannot establish new connections at this time. Please try again later.`,
);
} else {
postData(`/api/v1/brokerages/${brokerage.id}/authorize/`, {
type: 'read',
}).then((response) => {
window.location = response.data.url;
});
}
}
};
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 = (
<React.Fragment>
<P>
The brokerage account you tried to link is already connected with
Passiv.
</P>
<P>
If you are trying to add a new connection for another account,
please make sure that you are logged out of Questrade's dashboard.{' '}
<A
href="https://my.questrade.com/clients/signout.aspx"
target="_blank"
rel="noopener noreferrer"
>
Logout Questrade
</A>
</P>
<P>
If you are trying to change your connection type (for example,
switching from read-access to trade-access), check out our
tutorial on{' '}
<A
href="https://passiv.com/help/tutorials/how-to-manage-your-brokerage-connections/"
target="_blank"
rel="noopener noreferrer"
>
Managing your Brokerage Connections
</A>
.
</P>
<Button onClick={() => startConnection()}>Try Again</Button>
</React.Fragment>
);
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 '0000':
errorDisplay = (
<P>
No access code was provided by Questrade. 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;
}
}
return (
<ShadowBox background="#04a287">
<H1 color="white">SETUP</H1>
{loading ? (
<React.Fragment>
<Step>
Establishing connection to Questrade...{' '}
<FontAwesomeIcon icon={faSpinner} spin />
</Step>
</React.Fragment>
) : showUpgradeOffer ? (
<React.Fragment>
<Step>Questrade connection established.</Step>
<ShadowBox>
<H2Padded>
Congratulations, you're eligible for a <strong>free</strong>{' '}
upgrade to Passiv Elite!
</H2Padded>
<P>
<A
href="https://www.questrade.com/self-directed-investing/tools/partners/passiv"
target="_blank"
rel="noopener noreferrer"
>
Questrade
</A>{' '}
has offered to pay for your subscription for one year, with no
commitment on your part. No credit card is required and you can
cancel at any time.
</P>
<P>
You’ll get access to all basic features plus:
<BulletULPadded>
<li>
<A
href="https://passiv.com/help/tutorials/how-to-use-one-click-trades/"
target="_blank"
rel="noopener noreferrer"
>
One-click trades
</A>{' '}
- place orders easily through Passiv
</li>
<li>
<A
href="https://passiv.com/help/tutorials/how-to-set-up-multi-account-portfolios"
target="_blank"
rel="noopener noreferrer"
>
Multi-account portfolios
</A>{' '}
- combine accounts into a single target portfolio
</li>
<li>
<A
href="https://passiv.com/help/tutorials/how-to-manage-your-brokerage-connections"
target="_blank"
rel="noopener noreferrer"
>
Multiple brokerage connections
</A>{' '}
- connect more accounts and manage them together
</li>
<li>
<A
href="https://passiv.com/help/tutorials/how-to-manage-your-drift-notifications"
target="_blank"
rel="noopener noreferrer"
>
Portfolio drift notifications
</A>{' '}
- know when your portfolio drifts off target
</li>
</BulletULPadded>
</P>
<P>To claim your free year, just click the button below!</P>
<Button onClick={() => dispatch(push('/app/questrade-offer'))}>
Upgrade Now
</Button>
<Button onClick={() => dispatch(push('/app/setup-groups'))}>
Setup Groups
</Button>
</ShadowBox>
</React.Fragment>
) : (
<React.Fragment>
<Step>Failed to establish connection :(</Step>
<ShadowBox>
{errorDisplay}
<Button onClick={() => dispatch(push('/app/settings'))}>
Go to Settings
</Button>
</ShadowBox>
</React.Fragment>
)}
</ShadowBox>
);
};
export default QuestradeOauthPage;