1
- import { useEffect } from 'react' ;
1
+ import { useEffect , useState } from 'react' ;
2
+ import { useHistory , withRouter } from 'react-router-dom' ;
3
+ import Cookies from 'js-cookie' ;
2
4
import PropTypes from 'prop-types' ;
3
- import { withRouter , useHistory } from 'react-router-dom' ;
4
- import { loginUrl , routes , redirectToLogin , SessionStore , getDomainName } from '@deriv/shared' ;
5
+
6
+ import { useOauth2 } from '@deriv/hooks' ;
7
+ import { getDomainName , loginUrl , redirectToLogin , routes , SessionStore } from '@deriv/shared' ;
5
8
import { observer , useStore } from '@deriv/stores' ;
6
9
import { getLanguage } from '@deriv/translations' ;
7
- import { WS } from 'Services' ;
8
- import { Analytics } from '@deriv-com/analytics' ;
9
- import Cookies from 'js-cookie' ;
10
10
import { Chat } from '@deriv/utils' ;
11
+ import { Analytics } from '@deriv-com/analytics' ;
12
+ import { requestOidcAuthentication } from '@deriv-com/auth-client' ;
13
+
14
+ import { WS } from 'Services' ;
11
15
12
16
const Redirect = observer ( ( ) => {
13
17
const history = useHistory ( ) ;
14
18
const { client, ui } = useStore ( ) ;
19
+ const { isOAuth2Enabled } = useOauth2 ( { } ) ;
20
+ const [ queryCurrency , setQueryCurrency ] = useState ( 'USD' ) ;
15
21
16
22
const {
23
+ authorize_accounts_list,
17
24
currency,
18
25
has_wallet,
19
26
is_logged_in,
@@ -22,6 +29,8 @@ const Redirect = observer(() => {
22
29
setVerificationCode,
23
30
verification_code,
24
31
setPreventRedirectToHub,
32
+ switchAccount,
33
+ is_client_store_initialized,
25
34
} = client ;
26
35
27
36
const {
@@ -321,10 +330,29 @@ const Redirect = observer(() => {
321
330
default :
322
331
break ;
323
332
}
333
+
324
334
useEffect ( ( ) => {
325
- if ( ! redirected_to_route && history . location . pathname !== routes . traders_hub ) {
326
- const account_currency = url_params . get ( 'account' ) ;
327
- const client_account_lists = JSON . parse ( localStorage . getItem ( 'client.accounts' ) ) ;
335
+ const account_currency = url_params . get ( 'account' ) ;
336
+ setQueryCurrency ( account_currency ) ;
337
+ } , [ ] ) ;
338
+
339
+ useEffect ( ( ) => {
340
+ const account_currency = queryCurrency ;
341
+ if ( ! redirected_to_route && history . location . pathname !== routes . traders_hub && is_client_store_initialized ) {
342
+ const client_account_lists = JSON . parse ( localStorage . getItem ( 'client.accounts' ) || '{}' ) ;
343
+ const is_correct_currency = authorize_accounts_list . some (
344
+ account => account . currency ?. toUpperCase ( ) === account_currency ?. toUpperCase ( )
345
+ ) ;
346
+ const currency_exists = Object . values ( client_account_lists ) . some (
347
+ account => account . currency ?. toUpperCase ( ) === account_currency ?. toUpperCase ( )
348
+ ) ;
349
+ if ( ! currency_exists && is_correct_currency && authorize_accounts_list . length > 0 ) {
350
+ if ( isOAuth2Enabled ) {
351
+ requestOidcAuthentication ( {
352
+ redirectCallbackUri : `${ window . location . origin } /callback` ,
353
+ } ) ;
354
+ }
355
+ }
328
356
329
357
if ( account_currency ) {
330
358
let matching_loginid ;
@@ -341,7 +369,8 @@ const Redirect = observer(() => {
341
369
) ;
342
370
}
343
371
344
- if ( matching_loginid ) {
372
+ if ( matching_loginid && is_client_store_initialized ) {
373
+ switchAccount ( matching_loginid ) ;
345
374
sessionStorage . setItem ( 'active_loginid' , matching_loginid ) ;
346
375
}
347
376
}
@@ -363,16 +392,23 @@ const Redirect = observer(() => {
363
392
) ;
364
393
365
394
let updated_search = url_query_string ;
395
+ const params = new URLSearchParams ( url_query_string ) ;
396
+ params . set ( 'account' , queryCurrency ) ;
366
397
if ( matched_route && matched_route . type ) {
367
- updated_search = `${ url_query_string } &trade_type= ${ matched_route . type } ` ;
398
+ updated_search = `${ params . toString ( ) } ` ;
368
399
}
369
400
401
+ sessionStorage . setItem (
402
+ 'tradershub_redirect_to' ,
403
+ matched_route ? `redirect?${ updated_search } ` : default_route
404
+ ) ;
405
+
370
406
history . push ( {
371
407
pathname : matched_route ? matched_route . route : default_route ,
372
408
search : updated_search ,
373
409
} ) ;
374
410
}
375
- } , [ redirected_to_route , url_query_string , history ] ) ;
411
+ } , [ redirected_to_route , url_query_string , history , is_client_store_initialized , authorize_accounts_list ] ) ;
376
412
377
413
return null ;
378
414
} ) ;
0 commit comments