All files / modules/acute-common/src/components/dialogs/block-ui block-ui.component.ts

13.33% Statements 2/15
0% Branches 0/6
0% Functions 0/3
7.14% Lines 1/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          3x                                                                                                  
import {Component} from '@angular/core';
import {DialogService, DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {BehaviorSubject} from 'rxjs';
import {Logger} from '@bitblit/ratchet-common/logger/logger';
import {AsyncPipe, NgIf} from "@angular/common";
import {ProgressSpinnerModule} from "primeng/progressspinner";
 
@Component({
  selector: 'ngx-acute-common-block-ui',
  templateUrl: './block-ui.component.html',
  standalone: true,
  imports: [NgIf, AsyncPipe, ProgressSpinnerModule]
})
export class BlockUiComponent {
  constructor(
    private dialogService: DialogService,
    public cfg: DynamicDialogConfig,
  ) {}
 
  public static createUiBlock(
    dialogService: DialogService,
    message: BehaviorSubject<string> | string = 'Please wait...',
    subMessage?: string,
  ): DynamicDialogRef {
    const dlg: DynamicDialogRef = dialogService.open(BlockUiComponent, {
      closable: false,
      modal: true,
      data: {
        message: message instanceof BehaviorSubject ? message : new BehaviorSubject<string>(message),
        subMessage: subMessage,
      },
    });
    return dlg;
  }
 
  public static async runPromiseWithUiBlock<T>(
    dialogService: DialogService,
    prom: Promise<T>,
    message: BehaviorSubject<string> | string = 'Please wait...',
    subMessage?: string,
  ): Promise<T> {
    const dlg = dialogService.open(BlockUiComponent, {
      closable: false,
      modal: true,
      data: {
        message: message instanceof BehaviorSubject ? message : new BehaviorSubject<string>(message),
        subMessage: subMessage,
      },
    });
 
    try {
      const rval: T = await prom;
      dlg?.close(rval); // If it is still open, close it
      Logger.info('Blockui - Received %j - closing blocker ui and returning', rval);
      return rval;
    } catch (err) {
      Logger.error('Caught error inside block ui dialog : %s - rethrowing', err, err);
      dlg?.close(); // If it is still open, close it
      throw err;
    }
  }
}