|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { MatSnackBar } from '@angular/material/snack-bar'; |
| 3 | +import { ActivatedRoute } from '@angular/router'; |
| 4 | +import { AlertController } from '@ionic/angular'; |
| 5 | +import { AlertInput } from '@ionic/core'; |
| 6 | +import { TranslocoService } from '@ngneat/transloco'; |
| 7 | +import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; |
| 8 | +import { combineLatest } from 'rxjs'; |
| 9 | +import { catchError, concatMap, map, tap } from 'rxjs/operators'; |
| 10 | +import { |
| 11 | + Action, |
| 12 | + ActionsService, |
| 13 | +} from '../../../../shared/actions/actions.service'; |
| 14 | +import { BlockingActionService } from '../../../../shared/blocking-action/blocking-action.service'; |
| 15 | +import { DiaBackendAuthService } from '../../../../shared/dia-backend/auth/dia-backend-auth.service'; |
| 16 | +import { ErrorService } from '../../../../shared/error/error.service'; |
| 17 | + |
| 18 | +@UntilDestroy() |
| 19 | +@Component({ |
| 20 | + selector: 'app-actions', |
| 21 | + templateUrl: './actions.page.html', |
| 22 | + styleUrls: ['./actions.page.scss'], |
| 23 | +}) |
| 24 | +export class ActionsPage { |
| 25 | + readonly actions$ = this.actionsService |
| 26 | + .getActions$() |
| 27 | + .pipe(catchError((err: unknown) => this.errorService.toastError$(err))); |
| 28 | + |
| 29 | + private readonly id$ = this.route.paramMap.pipe( |
| 30 | + map(params => params.get('id')) |
| 31 | + ); |
| 32 | + |
| 33 | + constructor( |
| 34 | + private readonly actionsService: ActionsService, |
| 35 | + private readonly errorService: ErrorService, |
| 36 | + private readonly alertController: AlertController, |
| 37 | + private readonly translocoService: TranslocoService, |
| 38 | + private readonly blockingActionService: BlockingActionService, |
| 39 | + private readonly route: ActivatedRoute, |
| 40 | + private readonly authService: DiaBackendAuthService, |
| 41 | + private readonly snackBar: MatSnackBar |
| 42 | + ) {} |
| 43 | + |
| 44 | + openAction(action: Action) { |
| 45 | + return combineLatest([ |
| 46 | + this.actionsService.getParams$(action.params_list_custom_param), |
| 47 | + this.authService.token$, |
| 48 | + this.id$, |
| 49 | + ]) |
| 50 | + .pipe( |
| 51 | + concatMap( |
| 52 | + ([params, token, id]) => |
| 53 | + new Promise<void>(resolve => { |
| 54 | + this.alertController |
| 55 | + .create({ |
| 56 | + header: action.title_text, |
| 57 | + message: action.description_text, |
| 58 | + inputs: params.map( |
| 59 | + param => |
| 60 | + ({ |
| 61 | + name: param.name_text, |
| 62 | + label: param.label_text, |
| 63 | + type: param.type_text, |
| 64 | + placeholder: param.placeholder_text, |
| 65 | + value: param.default_value_text, |
| 66 | + disabled: param.disabled_boolean, |
| 67 | + } as AlertInput) |
| 68 | + ), |
| 69 | + buttons: [ |
| 70 | + { |
| 71 | + text: this.translocoService.translate('cancel'), |
| 72 | + role: 'cancel', |
| 73 | + }, |
| 74 | + { |
| 75 | + text: this.translocoService.translate('ok'), |
| 76 | + handler: value => { |
| 77 | + const body = { ...value, token: token, cid: id }; |
| 78 | + return this.sendAction(action, body); |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }) |
| 83 | + .then(alert => { |
| 84 | + alert.present(); |
| 85 | + resolve(); |
| 86 | + }); |
| 87 | + }) |
| 88 | + ), |
| 89 | + untilDestroyed(this) |
| 90 | + ) |
| 91 | + .subscribe(); |
| 92 | + } |
| 93 | + |
| 94 | + sendAction(action: Action, body: any) { |
| 95 | + const action$ = this.actionsService.send$(action.base_url_text, body).pipe( |
| 96 | + catchError((err: unknown) => { |
| 97 | + return this.errorService.toastError$(err); |
| 98 | + }), |
| 99 | + tap(() => |
| 100 | + this.snackBar.open( |
| 101 | + this.translocoService.translate('message.sentSuccessfully') |
| 102 | + ) |
| 103 | + ) |
| 104 | + ); |
| 105 | + this.blockingActionService |
| 106 | + .run$(action$) |
| 107 | + .pipe(untilDestroyed(this)) |
| 108 | + .subscribe(); |
| 109 | + } |
| 110 | +} |
0 commit comments