import { Component, Input, Output, EventEmitter, AfterContentInit, OnInit } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, of } from 'rxjs'; import { catchError, exhaustMap, map, tap } from 'rxjs/operators'; import { ConfirmationService, Message } from 'primeng/primeng'; import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth'; import { AuthContainerSelector } from '@overflow/shared/auth/store'; import { DomainMember } from '@overflow/commons-typescript/model/domain'; import { NoAuthProbeService } from '../service/noauth-probe.service'; @Component({ selector: 'of-noauth-probe-list', templateUrl: './noauth-probe-list.component.html', providers: [ConfirmationService] }) export class NoAuthProbeListComponent implements OnInit { noauthProbes$: Observable; pending$: Observable; error$: Observable; constructor( private confirmationService: ConfirmationService, private store: Store, private noAuthProbeService: NoAuthProbeService, ) { } ngOnInit() { this.store.pipe( tap(() => { this.pending$ = of(true); }), select(AuthContainerSelector.selectDomainMember), exhaustMap((domainMember: DomainMember) => this.noAuthProbeService.readAllByDomainID(domainMember.domain.id) .pipe( map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes$(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }) ) ), tap(() => { this.pending$ = of(false); }), ).take(1).subscribe(); // this.noauthProbes$ = this.store.pipe( // select(AuthContainerSelector.selectDomainMember), // exhaustMap((domainMember: DomainMember) => // this.noAuthProbeService.readAllByDomainID(domainMember.domain.id) // .pipe( // map((noauthProbes: NoAuthProbe[]) => { // this.pending$ = of(false); // if (null === noauthProbes) { // return null; // } // noauthProbes.forEach(noauthProbe => { // noauthProbe.descriptions = JSON.parse(noauthProbe.description); // }); // return noauthProbes; // }), // catchError(error => { // this.pending$ = of(false); // this.error$ = of(error); // return of(null); // }) // ) // ) // ); } onAcceptOrDeny(isAccept: boolean, selected: NoAuthProbe) { const title = isAccept ? 'Are you sure to accept this Probe?' : 'Are you sure to deny this Probe'; const message = isAccept ? 'Start collecting data as a Probe.' : 'It will be permanently deleted.'; this.confirmationService.confirm({ header: title, message: message, icon: isAccept ? 'fa-check' : 'fa fa-trash', accept: () => { if (isAccept) { this.noAuthProbeService.acceptNoAuthProbe(selected.id) .pipe( tap(() => { this.pending$ = of(true); }), map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes$(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }), tap(() => { this.pending$ = of(false); }), ).take(1).subscribe(); } else { this.noAuthProbeService.denyNoauthProbe(selected.id) .pipe( tap(() => { this.pending$ = of(true); }), map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes$(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }), tap(() => { this.pending$ = of(false); }), ).take(1).subscribe(); } }, reject: () => { } }); } private setNoauthProbes$(noauthProbes: NoAuthProbe[]): void { if (null === noauthProbes) { return; } noauthProbes.forEach(noauthProbe => { noauthProbe.descriptions = JSON.parse(noauthProbe.description); }); this.noauthProbes$ = of(noauthProbes); } private setError$(error: any): void { this.error$ = of(error); } }