@@ -2,12 +2,24 @@ import { Component } from '@angular/core';
2
2
import { MatSnackBar } from '@angular/material/snack-bar' ;
3
3
import { ActivatedRoute } from '@angular/router' ;
4
4
import { Plugins } from '@capacitor/core' ;
5
+ import { NavController } from '@ionic/angular' ;
5
6
import { TranslocoService } from '@ngneat/transloco' ;
6
- import { UntilDestroy } from '@ngneat/until-destroy' ;
7
- import { combineLatest } from 'rxjs' ;
8
- import { catchError , first , map } from 'rxjs/operators' ;
7
+ import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ;
8
+ import { BehaviorSubject , combineLatest , fromEvent } from 'rxjs' ;
9
+ import {
10
+ catchError ,
11
+ concatMap ,
12
+ first ,
13
+ map ,
14
+ switchMap ,
15
+ tap ,
16
+ } from 'rxjs/operators' ;
9
17
import { OrderHistoryService } from '../../../../shared/actions/service/order-history.service' ;
18
+ import { BUBBLE_IFRAME_URL } from '../../../../shared/dia-backend/secret' ;
19
+ import { DiaBackendStoreService } from '../../../../shared/dia-backend/store/dia-backend-store.service' ;
10
20
import { ErrorService } from '../../../../shared/error/error.service' ;
21
+ import { BubbleToIonicPostMessage } from '../../../../shared/iframe/iframe' ;
22
+ import { NetworkService } from '../../../../shared/network/network.service' ;
11
23
import { isNonNullable } from '../../../../utils/rx-operators/rx-operators' ;
12
24
import { getAssetProfileForNSE } from '../../../../utils/url' ;
13
25
@@ -19,26 +31,99 @@ const { Browser, Clipboard } = Plugins;
19
31
styleUrls : [ './network-action-order-details.page.scss' ] ,
20
32
} )
21
33
export class NetworkActionOrderDetailsPage {
34
+ readonly orderId$ = this . route . paramMap . pipe (
35
+ map ( params => params . get ( 'order_id' ) ) ,
36
+ isNonNullable ( )
37
+ ) ;
38
+ readonly assetId$ = this . orderId$ . pipe (
39
+ switchMap ( orderId => this . storeService . retrieveNetworkAppOrder$ ( orderId ) ) ,
40
+ map ( order => {
41
+ if ( 'nid' in order . action_args ) return order . action_args . nid as string ;
42
+ if ( 'cid' in order . action_args ) return order . action_args . cid as string ;
43
+ return undefined ;
44
+ } )
45
+ ) ;
22
46
readonly order$ = combineLatest ( [
23
47
this . orderHistoryService . networkActionOrders$ ,
24
- this . route . paramMap . pipe (
25
- map ( params => params . get ( 'order_id' ) ) ,
26
- isNonNullable ( )
27
- ) ,
48
+ this . orderId$ ,
28
49
] ) . pipe (
29
50
first ( ) ,
30
51
map ( ( [ orders , orderId ] ) => orders . find ( o => o . order_id_text === orderId ) ) ,
31
52
isNonNullable ( ) ,
32
53
catchError ( ( err : unknown ) => this . errorService . toastError$ ( err ) )
33
54
) ;
55
+ readonly isOffline$ = this . networkService . connected$ . pipe (
56
+ map ( connected => connected === false )
57
+ ) ;
58
+
59
+ readonly iframeUrl$ = combineLatest ( [ this . orderId$ , this . assetId$ ] ) . pipe (
60
+ map ( ( [ orderId , assetId ] ) => {
61
+ const queryParams = new URLSearchParams ( ) ;
62
+
63
+ queryParams . append ( 'order_id' , orderId ) ;
64
+
65
+ /**
66
+ * Some network action orders might not have releated asset id (aka nid, cid).
67
+ * For example:
68
+ * - "Creator Gallery"
69
+ * - "One-NUM-price"
70
+ * - "CustodialWalletWithdraw"
71
+ * - etc
72
+ */
73
+ if ( assetId ) queryParams . append ( 'asset_id' , assetId ) ;
74
+
75
+ return `${ BUBBLE_IFRAME_URL } /version-test/order_details?${ queryParams } ` ;
76
+ } )
77
+ ) ;
78
+
79
+ readonly iframeLoaded$ = new BehaviorSubject ( false ) ;
34
80
35
81
constructor (
36
82
private readonly route : ActivatedRoute ,
37
83
private readonly orderHistoryService : OrderHistoryService ,
84
+ private readonly storeService : DiaBackendStoreService ,
38
85
private readonly errorService : ErrorService ,
39
86
private readonly snackBar : MatSnackBar ,
40
- private readonly translocoService : TranslocoService
41
- ) { }
87
+ private readonly translocoService : TranslocoService ,
88
+ private readonly networkService : NetworkService ,
89
+ private readonly navController : NavController
90
+ ) {
91
+ this . processIframeEvents ( ) ;
92
+ }
93
+
94
+ private processIframeEvents ( ) {
95
+ fromEvent ( window , 'message' )
96
+ . pipe (
97
+ tap ( event => {
98
+ const postMessageEvent = event as MessageEvent ;
99
+ const data = postMessageEvent . data as BubbleToIonicPostMessage ;
100
+ switch ( data ) {
101
+ case BubbleToIonicPostMessage . IFRAME_ON_LOAD :
102
+ this . iframeLoaded$ . next ( true ) ;
103
+ break ;
104
+ case BubbleToIonicPostMessage . IFRAME_BACK_BUTTON_CLICKED :
105
+ this . navController . back ( ) ;
106
+ break ;
107
+ case BubbleToIonicPostMessage . IFRAME_COPY_TO_CLIPBOARD_ORDER_ID :
108
+ this . copyToClipboardOrderId ( ) ;
109
+ break ;
110
+ default :
111
+ break ;
112
+ }
113
+ } ) ,
114
+ untilDestroyed ( this )
115
+ )
116
+ . subscribe ( ) ;
117
+ }
118
+
119
+ private copyToClipboardOrderId ( ) {
120
+ this . order$
121
+ . pipe (
122
+ first ( ) ,
123
+ concatMap ( ( { order_id_text } ) => this . copyToClipboard ( order_id_text ) )
124
+ )
125
+ . subscribe ( ) ;
126
+ }
42
127
43
128
// eslint-disable-next-line class-methods-use-this
44
129
openResultUrl ( url : string ) {
0 commit comments