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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 8x 8x 8x 8x 8x 8x 8x 8x | import { ApplicationRef, Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { BehaviorSubject, Observable, OperatorFunction, identity, timer } from 'rxjs';
import { map, sample, switchMap, take, tap } from 'rxjs/operators';
import { delayUntil, whenFalsy, whenTruthy } from 'ish-core/utils/operators';
import { QuotingHelper } from '../models/quoting/quoting.helper';
import { Quote, QuoteRequest, QuotingEntity } from '../models/quoting/quoting.model';
import {
createQuoteRequestFromBasket,
deleteQuoteFromBasket,
deleteQuotingEntity,
getQuotingEntities,
getQuotingEntity,
getQuotingLoading,
loadQuoting,
loadQuotingDetail,
} from '../store/quoting';
interface QuoteEntitiesOptions {
automaticRefresh?: boolean;
}
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class QuotingFacade {
private isStable$ = new BehaviorSubject<boolean>(false);
constructor(private store: Store, appRef: ApplicationRef) {
appRef.isStable.pipe(whenTruthy(), take(1)).subscribe(isStable => this.isStable$.next(isStable));
}
loading$ = this.store.pipe(select(getQuotingLoading));
quotingEntities$(options: QuoteEntitiesOptions = { automaticRefresh: true }) {
// update on subscription
this.loadQuoting();
return this.store.pipe(
select(getQuotingEntities),
sample(this.loading$.pipe(whenFalsy())),
options?.automaticRefresh ? this.automaticQuoteRefresh() : identity,
tap(entities => {
entities.filter(QuotingHelper.isStub).forEach(entity => {
this.store.dispatch(loadQuotingDetail({ entity, level: 'List' }));
});
}),
map(entities => entities.filter(QuotingHelper.isNotStub))
);
}
state$(quoteId: string) {
return this.store.pipe(select(getQuotingEntity(quoteId)), map(QuotingHelper.state));
}
name$(quoteId: string) {
return this.store.pipe(
select(getQuotingEntity(quoteId)),
map((quote: Quote | QuoteRequest) => quote?.displayName)
);
}
delete(entity: QuotingEntity) {
this.store.dispatch(deleteQuotingEntity({ entity }));
}
createQuoteRequestFromBasket() {
this.store.dispatch(createQuoteRequestFromBasket());
}
loadQuoting() {
this.store.dispatch(loadQuoting());
}
deleteQuoteFromBasket(quoteId: string) {
this.store.dispatch(deleteQuoteFromBasket({ id: quoteId }));
}
private automaticQuoteRefresh<T>(): OperatorFunction<T, T> {
return (source$: Observable<T>) =>
source$.pipe(
delayUntil(this.isStable$.pipe(whenTruthy())),
switchMap(entities =>
// update every minute
timer(0, 60_000).pipe(
tap(count => {
Iif (count) {
this.loadQuoting();
}
}),
map(() => entities)
)
)
);
}
}
|