|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + ChangeDetectionStrategy, |
| 9 | + Component, |
| 10 | + inject, |
| 11 | + OnInit, |
| 12 | +} from '@angular/core'; |
| 13 | +import { |
| 14 | + UntypedFormBuilder, |
| 15 | + UntypedFormGroup, |
| 16 | + Validators, |
| 17 | +} from '@angular/forms'; |
| 18 | +import { Cart, PaymentType } from '@spartacus/cart/base/root'; |
| 19 | +import { CheckoutPaymentTypeFacade } from '@spartacus/checkout/b2b/root'; |
| 20 | +import { CheckoutReviewSubmitComponent } from '@spartacus/checkout/base/components'; |
| 21 | +import { CmsService, Page } from '@spartacus/core'; |
| 22 | +import { |
| 23 | + OpfBaseFacade, |
| 24 | + OpfMetadataStoreService, |
| 25 | +} from '@spartacus/opf/base/root'; |
| 26 | +import { OPF_EXPLICIT_TERMS_AND_CONDITIONS_COMPONENT } from '@spartacus/opf/checkout/root'; |
| 27 | +import { |
| 28 | + Observable, |
| 29 | + take, |
| 30 | + map, |
| 31 | + filter, |
| 32 | + combineLatest, |
| 33 | + BehaviorSubject, |
| 34 | +} from 'rxjs'; |
| 35 | +import { Card } from '@spartacus/storefront'; |
| 36 | + |
| 37 | +@Component({ |
| 38 | + selector: 'cx-opf-b2b-checkout-review', |
| 39 | + templateUrl: './opf-b2b-checkout-review.component.html', |
| 40 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 41 | + standalone: false, |
| 42 | +}) |
| 43 | +export class OpfB2bCheckoutReviewComponent |
| 44 | + extends CheckoutReviewSubmitComponent |
| 45 | + implements OnInit |
| 46 | +{ |
| 47 | + protected fb = inject(UntypedFormBuilder); |
| 48 | + protected opfMetadataStoreService = inject(OpfMetadataStoreService); |
| 49 | + protected cmsService = inject(CmsService); |
| 50 | + protected checkoutPaymentTypeFacade = inject(CheckoutPaymentTypeFacade); |
| 51 | + protected opfBaseFacade = inject(OpfBaseFacade); |
| 52 | + |
| 53 | + protected defaultTermsAndConditionsFieldValue = false; |
| 54 | + |
| 55 | + protected selectedPaymentProviderName$ = new BehaviorSubject< |
| 56 | + string | undefined |
| 57 | + >(undefined); |
| 58 | + |
| 59 | + explicitTermsAndConditions$: Observable<boolean | undefined> = this.cmsService |
| 60 | + .getCurrentPage() |
| 61 | + .pipe( |
| 62 | + map((page: Page) => { |
| 63 | + return this.isCmsComponentInPage( |
| 64 | + OPF_EXPLICIT_TERMS_AND_CONDITIONS_COMPONENT, |
| 65 | + page |
| 66 | + ); |
| 67 | + }) |
| 68 | + ); |
| 69 | + |
| 70 | + checkoutSubmitForm: UntypedFormGroup = this.fb.group({ |
| 71 | + termsAndConditions: [ |
| 72 | + this.defaultTermsAndConditionsFieldValue, |
| 73 | + Validators.requiredTrue, |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + get termsAndConditionInvalid(): boolean { |
| 78 | + return this.checkoutSubmitForm.invalid; |
| 79 | + } |
| 80 | + |
| 81 | + get termsAndConditionsFieldValue(): boolean { |
| 82 | + return Boolean(this.checkoutSubmitForm.get('termsAndConditions')?.value); |
| 83 | + } |
| 84 | + |
| 85 | + get paymentType$(): Observable<PaymentType | undefined> { |
| 86 | + return this.activeCartFacade |
| 87 | + .getActive() |
| 88 | + .pipe(map((cart: Cart) => cart.paymentType)); |
| 89 | + } |
| 90 | + |
| 91 | + get poNumber$(): Observable<string | undefined> { |
| 92 | + return this.checkoutPaymentTypeFacade.getPurchaseOrderNumberState().pipe( |
| 93 | + filter((state) => !state.loading && !state.error), |
| 94 | + map((state) => state.data) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + getPoNumberCard(poNumber?: string | null): Observable<Card> { |
| 99 | + return combineLatest([ |
| 100 | + this.translationService.translate('opfCheckout.poNumber'), |
| 101 | + this.translationService.translate('opfCheckout.noPoNumber'), |
| 102 | + ]).pipe( |
| 103 | + map(([textTitle, noneTextTitle]) => { |
| 104 | + return { |
| 105 | + title: textTitle, |
| 106 | + textBold: poNumber ? poNumber : noneTextTitle, |
| 107 | + }; |
| 108 | + }) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + getSelectedPayment$ = this.opfBaseFacade.getActiveConfigurationsState(); |
| 113 | + |
| 114 | + getSelectedPaymentId$ = this.opfMetadataStoreService |
| 115 | + .getOpfMetadataState() |
| 116 | + .pipe( |
| 117 | + take(1), |
| 118 | + map((data) => data?.selectedPaymentOptionId) |
| 119 | + ); |
| 120 | + |
| 121 | + getPaymentMethodNameCard(methodName?: string): Observable<Card> { |
| 122 | + return combineLatest([ |
| 123 | + this.translationService.translate('opfCheckout.paymentMethod'), |
| 124 | + this.translationService.translate('opfCheckout.noPaymentMethod'), |
| 125 | + ]).pipe( |
| 126 | + map(([title, noPaymentMethod]) => ({ |
| 127 | + title, |
| 128 | + textBold: methodName ?? noPaymentMethod, |
| 129 | + text: [], |
| 130 | + })) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + protected isCmsComponentInPage(cmsComponentUid: string, page: Page): boolean { |
| 135 | + return !!page && JSON.stringify(page).includes(cmsComponentUid); |
| 136 | + } |
| 137 | + |
| 138 | + protected updateTermsAndConditionsState() { |
| 139 | + this.opfMetadataStoreService.updateOpfMetadata({ |
| 140 | + termsAndConditionsChecked: this.termsAndConditionsFieldValue, |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + toggleTermsAndConditions() { |
| 145 | + this.updateTermsAndConditionsState(); |
| 146 | + } |
| 147 | + |
| 148 | + onPaymentProviderSelected(providerName: string) { |
| 149 | + this.selectedPaymentProviderName$.next(providerName); |
| 150 | + } |
| 151 | + |
| 152 | + ngOnInit() { |
| 153 | + this.updateTermsAndConditionsState(); |
| 154 | + } |
| 155 | +} |
0 commit comments