Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(opf,cart,checkout): use Angular 'inject' function for DI instead of constructors #19548

Draft
wants to merge 2 commits into
base: epic/opf
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { CartAccessCodeAdapter } from './cart-access-code.adapter';

@Injectable()
export class CartAccessCodeConnector {
constructor(protected adapter: CartAccessCodeAdapter) {}
protected adapter = inject(CartAccessCodeAdapter);

public getCartAccessCode(
userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CartGuestUser } from '@spartacus/cart/base/root';
import { Observable } from 'rxjs';
import { CartGuestUserAdapter } from './cart-guest-user.adapter';

@Injectable()
export class CartGuestUserConnector {
constructor(protected adapter: CartGuestUserAdapter) {}
protected adapter = inject(CartGuestUserAdapter);

public createCartGuestUser(
userId: string,
Expand Down
12 changes: 5 additions & 7 deletions feature-libs/cart/base/core/facade/cart-access-code.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CartAccessCodeFacade } from '@spartacus/cart/base/root';
import { Command, CommandService, QueryService } from '@spartacus/core';
import { Observable } from 'rxjs';
import { CartAccessCodeConnector } from '../connectors';

@Injectable()
export class CartAccessCodeService implements CartAccessCodeFacade {
protected queryService = inject(QueryService);
protected commandService = inject(CommandService);
protected cartAccessCodeConnector = inject(CartAccessCodeConnector);

protected getCartAccessCodeCommand: Command<
{
userId: string;
Expand All @@ -22,12 +26,6 @@ export class CartAccessCodeService implements CartAccessCodeFacade {
this.cartAccessCodeConnector.getCartAccessCode(userId, cartId)
);

constructor(
protected queryService: QueryService,
protected commandService: CommandService,
protected cartAccessCodeConnector: CartAccessCodeConnector
) {}

getCartAccessCode(
userId: string,
cartId: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ import { Observable, catchError } from 'rxjs';
@Injectable()
export class OccCartAccessCodeAdapter implements CartAccessCodeAdapter {
protected logger = inject(LoggerService);

constructor(
protected http: HttpClient,
protected occEndpointsService: OccEndpointsService,
protected converterService: ConverterService
) {}
protected http = inject(HttpClient);
protected occEndpointsService = inject(OccEndpointsService);
protected converterService = inject(ConverterService);

getCartAccessCode(
userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CheckoutConfig, CheckoutFlow } from '@spartacus/checkout/base/root';
import { BaseSiteService } from '@spartacus/core';
import { Observable } from 'rxjs';
Expand All @@ -14,10 +14,10 @@ import { map, take } from 'rxjs/operators';
providedIn: 'root',
})
export class CheckoutFlowOrchestratorService {
constructor(
protected checkoutConfig: CheckoutConfig,
protected baseSiteService: BaseSiteService
) {
protected checkoutConfig = inject(CheckoutConfig);
protected baseSiteService = inject(BaseSiteService);

constructor() {
this.getPaymentProvider().subscribe((paymentProvider) => {
this.paymentProviderName = paymentProvider;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { Address } from '@spartacus/core';
import { Observable } from 'rxjs';
import { CheckoutBillingAddressAdapter } from './checkout-billing-address.adapter';

@Injectable()
export class CheckoutBillingAddressConnector {
constructor(protected adapter: CheckoutBillingAddressAdapter) {}
protected adapter = inject(CheckoutBillingAddressAdapter);

public setBillingAddress(
userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
CheckoutBillingAddressFacade,
Expand All @@ -17,14 +17,22 @@ import {
OCC_USER_ID_ANONYMOUS,
UserIdService,
} from '@spartacus/core';
import { combineLatest, Observable } from 'rxjs';
import { Observable, combineLatest } from 'rxjs';
import { map, switchMap, take } from 'rxjs/operators';
import { CheckoutBillingAddressConnector } from '../connectors/checkout-billing-address/checkout-billing-address.connector';

@Injectable()
export class CheckoutBillingAddressService
implements CheckoutBillingAddressFacade
{
protected activeCartFacade = inject(ActiveCartFacade);
protected userIdService = inject(UserIdService);
protected commandService = inject(CommandService);
protected checkoutBillingAddressConnector = inject(
CheckoutBillingAddressConnector
);
protected checkoutQueryFacade = inject(CheckoutQueryFacade);

protected setBillingAddressCommand = this.commandService.create<Address>(
(address) =>
this.checkoutPreconditions().pipe(
Expand All @@ -44,14 +52,6 @@ export class CheckoutBillingAddressService
}
);

constructor(
protected activeCartFacade: ActiveCartFacade,
protected userIdService: UserIdService,
protected commandService: CommandService,
protected checkoutBillingAddressConnector: CheckoutBillingAddressConnector,
protected checkoutQueryFacade: CheckoutQueryFacade
) {}

/**
* Performs the necessary checkout preconditions.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ export class OccCheckoutBillingAddressAdapter
implements CheckoutBillingAddressAdapter
{
protected logger = inject(LoggerService);

constructor(
protected http: HttpClient,
protected occEndpoints: OccEndpointsService,
protected converter: ConverterService
) {}
protected http = inject(HttpClient);
protected occEndpoints = inject(OccEndpointsService);
protected converter = inject(ConverterService);

public setBillingAddress(
userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ElementRef,
HostListener,
OnInit,
inject,
} from '@angular/core';
import { OpfErrorDialogOptions } from '@spartacus/opf/base/root';
import { FocusConfig, LaunchDialogService } from '@spartacus/storefront';
Expand All @@ -24,6 +25,11 @@ import { OpfErrorModalService } from './opf-error-modal.service';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OpfErrorModalComponent implements OnInit {
protected launchDialogService = inject(LaunchDialogService);
protected el = inject(ElementRef);
protected cd = inject(ChangeDetectorRef);
protected opfErrorModalService = inject(OpfErrorModalService);

focusConfig: FocusConfig = {
trap: true,
block: true,
Expand All @@ -40,12 +46,7 @@ export class OpfErrorModalComponent implements OnInit {
}
}

constructor(
protected launchDialogService: LaunchDialogService,
protected el: ElementRef,
protected cd: ChangeDetectorRef,
protected opfErrorModalService: OpfErrorModalService
) {
constructor() {
// Mechanism needed to trigger the cpnt life cycle hooks.
timer(1).subscribe({
complete: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { TranslationService } from '@spartacus/core';
import {
OpfErrorDialogOptions,
Expand All @@ -17,7 +17,7 @@ import { map, switchMap } from 'rxjs/operators';
providedIn: 'root',
})
export class OpfErrorModalService {
constructor(protected translationService: TranslationService) {}
protected translationService = inject(TranslationService);

getMessageAndConfirmTranslations(dialogOptions: OpfErrorDialogOptions) {
return combineLatest([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { OpfActiveConfiguration } from '@spartacus/opf/base/root';
import { Observable } from 'rxjs';
import { OpfBaseAdapter } from './opf-base.adapter';

@Injectable()
export class OpfBaseConnector {
constructor(protected adapter: OpfBaseAdapter) {}
protected adapter = inject(OpfBaseAdapter);

public getActiveConfigurations(): Observable<OpfActiveConfiguration[]> {
return this.adapter.getActiveConfigurations();
Expand Down
12 changes: 5 additions & 7 deletions integration-libs/opf/base/core/facade/opf-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import {
CommandService,
Query,
Expand All @@ -20,17 +20,15 @@ import { OpfBaseConnector } from '../connectors/opf-base.connector';

@Injectable()
export class OpfBaseService implements OpfBaseFacade {
protected queryService = inject(QueryService);
protected commandService = inject(CommandService);
protected opfBaseConnector = inject(OpfBaseConnector);

protected activeConfigurationsQuery: Query<OpfActiveConfiguration[]> =
this.queryService.create<OpfActiveConfiguration[]>(() =>
this.opfBaseConnector.getActiveConfigurations()
);

constructor(
protected queryService: QueryService,
protected commandService: CommandService,
protected opfBaseConnector: OpfBaseConnector
) {}

getActiveConfigurationsState(): Observable<
QueryState<OpfActiveConfiguration[] | undefined>
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import {
BaseSiteService,
DynamicAttributes,
Expand All @@ -17,13 +17,13 @@ import { OpfApiConfig, OpfConfig } from '@spartacus/opf/base/root';
providedIn: 'root',
})
export class OpfEndpointsService {
protected opfConfig = inject(OpfConfig);
protected opfApiConfig = inject(OpfApiConfig);
protected baseSiteService = inject(BaseSiteService);

private _activeBaseSite: string;

constructor(
protected opfConfig: OpfConfig,
protected opfApiConfig: OpfApiConfig,
protected baseSiteService: BaseSiteService
) {
constructor() {
if (this.baseSiteService) {
this.baseSiteService
.getActive()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ import { catchError } from 'rxjs/operators';

@Injectable()
export class OpfApiBaseAdapter implements OpfBaseAdapter {
protected http = inject(HttpClient);
protected converter = inject(ConverterService);
protected opfEndpointsService = inject(OpfEndpointsService);
protected config = inject(OpfConfig);
protected logger = inject(LoggerService);

constructor(
protected http: HttpClient,
protected converter: ConverterService,
protected opfEndpointsService: OpfEndpointsService,
protected config: OpfConfig
) {}

protected headerWithNoLanguage: { [name: string]: string } = {
accept: 'application/json',
'Content-Type': 'application/json',
Expand Down
10 changes: 5 additions & 5 deletions integration-libs/opf/base/root/events/opf-event.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable, OnDestroy } from '@angular/core';
import { Injectable, OnDestroy, inject } from '@angular/core';
import { CreateCartEvent } from '@spartacus/cart/base/root';
import { EventService, LoginEvent } from '@spartacus/core';
import { OrderPlacedEvent } from '@spartacus/order/root';
Expand All @@ -15,12 +15,12 @@ import { OpfMetadataStoreService } from '../services';
providedIn: 'root',
})
export class OpfEventListenerService implements OnDestroy {
protected eventService = inject(EventService);
protected opfMetadataStoreService = inject(OpfMetadataStoreService);

protected subscriptions = new Subscription();

constructor(
protected eventService: EventService,
protected opfMetadataStoreService: OpfMetadataStoreService
) {
constructor() {
this.onOpfPaymentMetadataResetConditionsMet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable, OnDestroy } from '@angular/core';
import { Injectable, OnDestroy, inject } from '@angular/core';
import { StatePersistenceService } from '@spartacus/core';
import { Observable, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
Expand All @@ -24,13 +24,10 @@ export interface SyncedOpfState {
*/
@Injectable({ providedIn: 'root' })
export class OpfMetadataStatePersistanceService implements OnDestroy {
protected subscription = new Subscription();

constructor(
protected statePersistenceService: StatePersistenceService,
protected opfMetadataStoreService: OpfMetadataStoreService
) {}
protected statePersistenceService = inject(StatePersistenceService);
protected opfMetadataStoreService = inject(OpfMetadataStoreService);

protected subscription = new Subscription();
/**
* Identifier used for storage key.
*/
Expand Down
Loading
Loading