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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x | import { Injectable, OnDestroy } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { RxState } from '@rx-angular/state';
import { Observable } from 'rxjs';
import { distinctUntilChanged, filter, first, map, sample, switchMap, tap } from 'rxjs/operators';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { LineItemUpdate } from 'ish-core/models/line-item-update/line-item-update.model';
import { whenFalsy, whenTruthy } from 'ish-core/utils/operators';
import { QuotingHelper } from '../models/quoting/quoting.helper';
import { Quote, QuoteRequest, QuoteStatus } from '../models/quoting/quoting.model';
import {
addQuoteToBasket,
createQuoteRequestFromQuote,
createQuoteRequestFromQuoteRequest,
getQuotingEntity,
getQuotingError,
getQuotingLoading,
isQuotingInitialized,
loadQuoting,
loadQuotingDetail,
rejectQuote,
submitQuoteRequest,
updateQuoteRequest,
} from '../store/quoting';
export const isQuoteStarted = (state$: Observable<{ entityAsQuote: Quote }>) =>
state$.pipe(map(state => Date.now() > state.entityAsQuote?.validFromDate));
export const isQuoteValid = (state$: Observable<{ entityAsQuote: Quote }>) =>
state$.pipe(
map(state => Date.now() < state.entityAsQuote?.validToDate && Date.now() > state.entityAsQuote?.validFromDate)
);
@Injectable()
export abstract class QuoteContextFacade
extends RxState<{
id: string;
loading: boolean;
error: HttpError;
entity: Quote | QuoteRequest;
entityAsQuoteRequest: QuoteRequest;
entityAsQuote: Quote;
state: QuoteStatus;
editable: boolean;
justSubmitted: boolean;
}>
implements OnDestroy
{
constructor(private store: Store) {
super();
store.pipe(first()).subscribe(state => {
Iif (!isQuotingInitialized(state)) {
this.store.dispatch(loadQuoting());
}
});
this.connect('loading', this.store.pipe(select(getQuotingLoading)));
this.connect('error', this.store.pipe(select(getQuotingError)));
this.connect(
'entity',
this.select('id').pipe(
whenTruthy(),
distinctUntilChanged(),
switchMap(quoteId =>
this.store.pipe(
select(getQuotingEntity(quoteId)),
whenTruthy(),
tap(entity => {
Iif (entity?.completenessLevel !== 'Detail') {
this.store.dispatch(loadQuotingDetail({ entity, level: 'Detail' }));
}
}),
sample(this.select('loading').pipe(whenFalsy())),
filter(entity => entity?.completenessLevel === 'Detail'),
map(entity => entity as Quote | QuoteRequest)
)
),
whenTruthy()
)
);
this.connect('entityAsQuoteRequest', this.select('entity').pipe(map(QuotingHelper.asQuoteRequest)));
this.connect('entityAsQuote', this.select('entity').pipe(map(QuotingHelper.asQuote)));
this.connect('state', this.select('entity').pipe(map(QuotingHelper.state), distinctUntilChanged()));
this.connect('editable', this.select('state').pipe(map(state => state === 'New')));
}
updateItem(item: LineItemUpdate) {
if (!item.quantity) {
this.deleteItem(item.itemId);
} else {
this.store.dispatch(
updateQuoteRequest({
id: this.get('entity', 'id'),
changes: [{ type: 'change-item', itemId: item.itemId, quantity: item.quantity }],
})
);
}
}
deleteItem(itemId: string) {
this.store.dispatch(
updateQuoteRequest({
id: this.get('entity', 'id'),
changes: [{ type: 'remove-item', itemId }],
})
);
}
reject() {
this.store.dispatch(rejectQuote({ id: this.get('entity', 'id') }));
}
copy() {
if (this.get('entity', 'type') === 'Quote') {
this.store.dispatch(createQuoteRequestFromQuote({ id: this.get('entity', 'id') }));
} else {
this.store.dispatch(createQuoteRequestFromQuoteRequest({ id: this.get('entity', 'id') }));
}
}
addToBasket() {
this.store.dispatch(addQuoteToBasket({ id: this.get('entity', 'id') }));
}
update(meta: { displayName: string; description: string }) {
this.store.dispatch(
updateQuoteRequest({ id: this.get('entity', 'id'), changes: [{ type: 'meta-data', ...meta }] })
);
}
submit() {
this.set('justSubmitted', () => true);
this.store.dispatch(submitQuoteRequest({ id: this.get('entity', 'id') }));
}
}
|