1+ import { HttpErrorResponse } from '@angular/common/http' ;
12import { Component } from '@angular/core' ;
23import { MatDialog } from '@angular/material/dialog' ;
34import { MatSnackBar } from '@angular/material/snack-bar' ;
45import { ActivatedRoute } from '@angular/router' ;
56import { TranslocoService } from '@ngneat/transloco' ;
67import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ;
7- import { combineLatest } from 'rxjs' ;
8+ import { combineLatest , of } from 'rxjs' ;
89import { catchError , concatMap , first , map , tap } from 'rxjs/operators' ;
910import { ActionsDialogComponent } from '../../../../shared/actions/actions-dialog/actions-dialog.component' ;
1011import {
@@ -13,7 +14,12 @@ import {
1314} from '../../../../shared/actions/service/actions.service' ;
1415import { BlockingActionService } from '../../../../shared/blocking-action/blocking-action.service' ;
1516import { DiaBackendAuthService } from '../../../../shared/dia-backend/auth/dia-backend-auth.service' ;
17+ import {
18+ DiaBackendStoreService ,
19+ NetworkAppOrderStatus ,
20+ } from '../../../../shared/dia-backend/store/dia-backend-store.service' ;
1621import { ErrorService } from '../../../../shared/error/error.service' ;
22+ import { OrderDetailDialogComponent } from '../../../../shared/order-detail-dialog/order-detail-dialog.component' ;
1723import { isNonNullable } from '../../../../utils/rx-operators/rx-operators' ;
1824
1925@UntilDestroy ( )
@@ -39,54 +45,108 @@ export class ActionsPage {
3945 private readonly route : ActivatedRoute ,
4046 private readonly authService : DiaBackendAuthService ,
4147 private readonly snackBar : MatSnackBar ,
42- private readonly dialog : MatDialog
48+ private readonly dialog : MatDialog ,
49+ private readonly storeService : DiaBackendStoreService
4350 ) { }
4451
45- openAction ( action : Action ) {
52+ openActionDialog$ ( action : Action ) {
4653 return combineLatest ( [
4754 this . actionsService . getParams$ ( action . params_list_custom_param1 ) ,
4855 this . authService . token$ ,
4956 this . id$ ,
50- ] )
57+ ] ) . pipe (
58+ first ( ) ,
59+ concatMap ( ( [ params , token , id ] ) => {
60+ const dialogRef = this . dialog . open < ActionsDialogComponent > (
61+ ActionsDialogComponent ,
62+ {
63+ disableClose : true ,
64+ data : {
65+ action : action ,
66+ params : params ,
67+ } ,
68+ }
69+ ) ;
70+ return dialogRef . afterClosed ( ) . pipe (
71+ isNonNullable ( ) ,
72+ concatMap ( data =>
73+ of ( {
74+ networkApp : action . network_app_id_text ,
75+ actionArgs : { ...data , token : token , cid : id } ,
76+ } as CreateOrderInput )
77+ )
78+ ) ;
79+ } )
80+ ) ;
81+ }
82+
83+ openOrderDialog$ ( orderStatus : NetworkAppOrderStatus ) {
84+ const dialogRef = this . dialog . open < OrderDetailDialogComponent > (
85+ OrderDetailDialogComponent ,
86+ {
87+ disableClose : true ,
88+ data : orderStatus ,
89+ width : '80%' ,
90+ }
91+ ) ;
92+ return dialogRef . afterClosed ( ) . pipe (
93+ isNonNullable ( ) ,
94+ concatMap ( ( orderId : string ) => of ( orderId ) )
95+ ) ;
96+ }
97+
98+ createOrder$ ( appName : string , actionArgs : any ) {
99+ return this . storeService . createNetworkAppOrder ( appName , actionArgs ) . pipe (
100+ catchError ( ( err : unknown ) => {
101+ return this . errorService . toastError$ ( err ) ;
102+ } ) ,
103+ isNonNullable ( )
104+ ) ;
105+ }
106+
107+ confirmOrder$ ( id : string ) {
108+ return this . storeService . confirmNetworkAppOrder ( id ) . pipe (
109+ catchError ( ( err : unknown ) => {
110+ if ( err instanceof HttpErrorResponse ) {
111+ const errorType = err . error . error ?. type ;
112+ if ( errorType === 'insufficient_fund' )
113+ return this . errorService . toastError$ (
114+ this . translocoService . translate ( `error.diaBackend.${ errorType } ` )
115+ ) ;
116+ }
117+ return this . errorService . toastError$ ( err ) ;
118+ } ) ,
119+ isNonNullable ( )
120+ ) ;
121+ }
122+
123+ doAction ( action : Action ) {
124+ this . openActionDialog$ ( action )
51125 . pipe (
52- first ( ) ,
53- concatMap ( ( [ params , token , id ] ) => {
54- const dialogRef = this . dialog . open < ActionsDialogComponent > (
55- ActionsDialogComponent ,
56- {
57- disableClose : true ,
58- data : {
59- action : action ,
60- params : params ,
61- } ,
62- }
63- ) ;
64- return dialogRef . afterClosed ( ) . pipe (
65- isNonNullable ( ) ,
66- tap ( data =>
67- this . sendAction ( action , { ...data , token : token , cid : id } )
126+ concatMap ( createOrderInput =>
127+ this . blockingActionService . run$ (
128+ this . createOrder$ (
129+ createOrderInput . networkApp ,
130+ createOrderInput . actionArgs
68131 )
132+ )
133+ ) ,
134+ concatMap ( orderStatus => this . openOrderDialog$ ( orderStatus ) ) ,
135+ concatMap ( orderId =>
136+ this . blockingActionService . run$ ( this . confirmOrder$ ( orderId ) )
137+ ) ,
138+ tap ( ( ) => {
139+ this . snackBar . open (
140+ this . translocoService . translate ( 'message.sentSuccessfully' )
69141 ) ;
70142 } ) ,
71143 untilDestroyed ( this )
72144 )
73145 . subscribe ( ) ;
74146 }
147+ }
75148
76- sendAction ( action : Action , body : any ) {
77- const action$ = this . actionsService . send$ ( action . base_url_text , body ) . pipe (
78- catchError ( ( err : unknown ) => {
79- return this . errorService . toastError$ ( err ) ;
80- } ) ,
81- tap ( ( ) =>
82- this . snackBar . open (
83- this . translocoService . translate ( 'message.sentSuccessfully' )
84- )
85- )
86- ) ;
87- this . blockingActionService
88- . run$ ( action$ )
89- . pipe ( untilDestroyed ( this ) )
90- . subscribe ( ) ;
91- }
149+ interface CreateOrderInput {
150+ networkApp : string ;
151+ actionArgs : any ;
92152}
0 commit comments