diff --git a/feature-libs/organization/README.md b/feature-libs/organization/README.md index 14a5f7be6bb..012a896dbf0 100644 --- a/feature-libs/organization/README.md +++ b/feature-libs/organization/README.md @@ -1,5 +1,5 @@ -# Spartacus Organization +# Spartacus My Account -`@spartacus/organization` is a package that you can include in your application, which allows you to use various organization features. +`@spartacus/my-account` is a package that you can include in your application, which allows you to use various my-account features. For more information, see [Spartacus](https://github.com/SAP/spartacus). diff --git a/feature-libs/organization/administration/assets/translations/en/cost-center.i18n.ts b/feature-libs/organization/administration/assets/translations/en/cost-center.i18n.ts index f814ef9f252..098be39dc65 100644 --- a/feature-libs/organization/administration/assets/translations/en/cost-center.i18n.ts +++ b/feature-libs/organization/administration/assets/translations/en/cost-center.i18n.ts @@ -22,11 +22,8 @@ export const costCenter = { confirmDisabled: 'Cost Center {{ item.name }} disabled successfully', }, - title: 'Details Cost Center', - subtitle: 'Cost Center: {{ item.name }}', - details: { - title: 'Details Cost Center', + title: 'Cost Center Details', subtitle: 'Cost Center: {{ item.name }}', }, diff --git a/feature-libs/organization/administration/assets/translations/en/units.i18n.ts b/feature-libs/organization/administration/assets/translations/en/units.i18n.ts index 2c06fcafeaa..ee1fa2fc786 100644 --- a/feature-libs/organization/administration/assets/translations/en/units.i18n.ts +++ b/feature-libs/organization/administration/assets/translations/en/units.i18n.ts @@ -37,6 +37,7 @@ export const unit = { expandAll: 'Expand all', collapseAll: 'Collapse all', }, + children: { create: { title: 'Create child unit', @@ -44,6 +45,13 @@ export const unit = { }, }, + costCenters: { + create: { + title: 'Create cost center', + subtitle: '', + }, + }, + form: { parentOrgUnit: 'Parent business unit', create: 'Create Unit', @@ -74,10 +82,6 @@ export const unit = { changes: 'Changes are saved automatically.', }, }, - // costCenters: { - // header: 'Cost centers in {{code}}', - // new: 'New cost center', - // }, breadcrumbs: { list: 'All units', @@ -87,6 +91,7 @@ export const unit = { approvers: 'Approvers', addresses: 'Shipping addresses', addressDetails: '{{formattedAddress}}', + costCenters: 'Cost Centers', }, }; diff --git a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.spec.ts b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.spec.ts index 13e0845434c..66370413089 100644 --- a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.spec.ts +++ b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.spec.ts @@ -33,11 +33,15 @@ class MockOrgUnitService { } class MockCurrencyService { - getAll() {} + getAll() { + return of(); + } } class MockOrganizationItemService { - getForm() {} + getForm() { + return mockForm; + } } describe('CostCenterFormComponent', () => { @@ -78,7 +82,6 @@ describe('CostCenterFormComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(CostCenterFormComponent); component = fixture.componentInstance; - component.ngOnInit(); }); it('should create', () => { @@ -86,7 +89,6 @@ describe('CostCenterFormComponent', () => { }); it('should render form controls', () => { - component.form = mockForm; fixture.detectChanges(); const formControls = fixture.debugElement.queryAll(By.css('input')); expect(formControls.length).toBeGreaterThan(0); @@ -100,18 +102,10 @@ describe('CostCenterFormComponent', () => { }); it('should get currencies from service', () => { - component.form = mockForm; expect(currencyService.getAll).toHaveBeenCalled(); }); it('should get active units from service', () => { - component.form = mockForm; expect(b2bUnitService.getActiveUnitList).toHaveBeenCalled(); }); - - it('should load list of b2bUnits on subscription', () => { - component.form = mockForm; - fixture.detectChanges(); - expect(b2bUnitService.loadList).toHaveBeenCalled(); - }); }); diff --git a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.ts b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.ts index b316206fe43..fb48e27ddd4 100644 --- a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.ts +++ b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.component.ts @@ -1,4 +1,4 @@ -import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { CostCenter, Currency, CurrencyService } from '@spartacus/core'; import { @@ -10,6 +10,7 @@ import { OrganizationItemService } from '../../shared/organization-item.service' import { CostCenterItemService } from '../services/cost-center-item.service'; @Component({ + selector: 'cx-cost-center-form', templateUrl: './cost-center-form.component.html', changeDetection: ChangeDetectionStrategy.OnPush, providers: [ @@ -19,8 +20,19 @@ import { CostCenterItemService } from '../services/cost-center-item.service'; }, ], }) -export class CostCenterFormComponent implements OnInit { +export class CostCenterFormComponent { form: FormGroup = this.itemService.getForm(); + /** + * Initialize the business unit for the cost center. + * + * If there's a unit provided, we disable the form control. + */ + @Input() set unitKey(value: string) { + if (value) { + this.form?.get('unit.uid').setValue(value); + this.form?.get('unit')?.disable(); + } + } units$: Observable = this.unitService.getActiveUnitList(); currencies$: Observable = this.currencyService.getAll(); @@ -30,8 +42,4 @@ export class CostCenterFormComponent implements OnInit { protected unitService: OrgUnitService, protected currencyService: CurrencyService ) {} - - ngOnInit(): void { - this.unitService.loadList(); - } } diff --git a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.module.ts b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.module.ts index e4822426728..7a1c4ffc59f 100644 --- a/feature-libs/organization/administration/components/cost-center/form/cost-center-form.module.ts +++ b/feature-libs/organization/administration/components/cost-center/form/cost-center-form.module.ts @@ -23,6 +23,5 @@ import { CostCenterFormComponent } from './cost-center-form.component'; declarations: [CostCenterFormComponent], exports: [CostCenterFormComponent], providers: [CurrencyService, OrgUnitService], - entryComponents: [CostCenterFormComponent], }) export class CostCenterFormModule {} diff --git a/feature-libs/organization/administration/components/shared/organization-form/organization-form.component.ts b/feature-libs/organization/administration/components/shared/organization-form/organization-form.component.ts index b17363637c4..1cbc0a64935 100644 --- a/feature-libs/organization/administration/components/shared/organization-form/organization-form.component.ts +++ b/feature-libs/organization/administration/components/shared/organization-form/organization-form.component.ts @@ -1,7 +1,7 @@ import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { Observable } from 'rxjs'; -import { first, map, switchMap } from 'rxjs/operators'; +import { first, map } from 'rxjs/operators'; import { OrganizationItemService } from '../organization-item.service'; /** @@ -25,19 +25,15 @@ export class OrganizationFormComponent { */ i18n: string; - form$: Observable = this.itemService.unit$.pipe( - switchMap((unitUid) => - this.itemService.current$.pipe( - map((item) => { - this.setI18nRoot(item); - if (!item && unitUid) { - // TODO: as long as we do not have harmonized unit property, we have to workaround here, - item = { parentOrgUnit: { uid: unitUid } } as any; - } - return this.itemService.getForm(item); - }) - ) - ) + form$: Observable = this.itemService.current$.pipe( + map((item) => { + this.setI18nRoot(item); + if (!item) { + // we trick the form builder... + item = {} as any; + } + return this.itemService.getForm(item); + }) ); constructor(protected itemService: OrganizationItemService) {} @@ -48,7 +44,7 @@ export class OrganizationFormComponent { }); } - protected setI18nRoot(item) { + protected setI18nRoot(item: T): void { // concatenate the i18n root with .edit or .create suffix this.i18n = this.i18nRoot + (item ? '.edit' : '.create'); } diff --git a/feature-libs/organization/administration/components/shared/organization-form/organization-form.service.ts b/feature-libs/organization/administration/components/shared/organization-form/organization-form.service.ts index a5eb5a1043d..084f03272d9 100644 --- a/feature-libs/organization/administration/components/shared/organization-form/organization-form.service.ts +++ b/feature-libs/organization/administration/components/shared/organization-form/organization-form.service.ts @@ -11,12 +11,19 @@ export abstract class OrganizationFormService { protected abstract build(item?: T): void; getForm(item?: T): FormGroup { + if (this.form && !!item) { + this.patchData(item); + return this.form; + } + if (!this.form) { this.build(item); } + // while we should be able to reset with initial value, this doesn't always work // hence, we're patching afterwards. this.form.reset(); + this.form.enable(); this.patchData(item); return this.form; diff --git a/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.spec.ts b/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.spec.ts index 8983714f7a3..64ad4e94a60 100644 --- a/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.spec.ts +++ b/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.spec.ts @@ -71,10 +71,6 @@ describe('AssignCellComponent', () => { expect(component).toBeTruthy(); }); - it('should not have an item', () => { - expect(component.hasItem).toBeFalsy(); - }); - it('should not be assigned', () => { expect(component.isAssigned).toBeFalsy(); }); @@ -91,10 +87,6 @@ describe('AssignCellComponent', () => { expect(component).toBeTruthy(); }); - it('should have item', () => { - expect(component.hasItem).toBeTruthy(); - }); - it('should be assigned', () => { expect(component.isAssigned).toBeTruthy(); }); diff --git a/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.ts b/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.ts index 7e381f6c8a5..b539dd312f4 100644 --- a/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.ts +++ b/feature-libs/organization/administration/components/shared/organization-sub-list/assign-cell.component.ts @@ -6,7 +6,8 @@ import { import { OrganizationItemService } from '../organization-item.service'; import { OrganizationListService } from '../organization-list/organization-list.service'; import { MessageService } from '../organization-message/services/message.service'; -import { OrganizationSubListService } from './organization-sub-list.service'; +import { OrganizationSubListService } from '../organization-sub-list/organization-sub-list.service'; +import { OrganizationCellComponent } from '../organization-table/organization-cell.component'; @Component({ template: ` @@ -16,7 +17,9 @@ import { OrganizationSubListService } from './organization-sub-list.service'; `, changeDetection: ChangeDetectionStrategy.OnPush, }) -export class AssignCellComponent implements OnDestroy { +export class AssignCellComponent + extends OrganizationCellComponent + implements OnDestroy { /** * Indicates that we need to show a notification message. */ @@ -27,13 +30,8 @@ export class AssignCellComponent implements OnDestroy { protected organizationItemService: OrganizationItemService, protected messageService: MessageService, protected organizationSubListService: OrganizationListService - ) {} - - /** - * Indicates whether the item is loaded. - */ - get hasItem(): boolean { - return !!this.item && Object.keys(this.item).length > 0; + ) { + super(outlet); } get isAssigned(): boolean { @@ -81,14 +79,6 @@ export class AssignCellComponent implements OnDestroy { ); } - protected get item(): T | null { - if (!this.outlet.context) { - return null; - } - const { _field, _options, _type, _i18nRoot, ...all } = this.outlet.context; - return all as T; - } - ngOnDestroy() { // We're playing a dirty trick here; The store is not equipped with a // selector to select any updated assignments. Moreover, as soon as diff --git a/feature-libs/organization/administration/components/shared/organization-table/organization-cell.component.ts b/feature-libs/organization/administration/components/shared/organization-table/organization-cell.component.ts index f599d0e755b..17bd3232966 100644 --- a/feature-libs/organization/administration/components/shared/organization-table/organization-cell.component.ts +++ b/feature-libs/organization/administration/components/shared/organization-table/organization-cell.component.ts @@ -59,4 +59,19 @@ export class OrganizationCellComponent { get type(): string { return this.model._type; } + + /** + * Indicates whether the item is loaded. + */ + get hasItem(): boolean { + return !!this.item && Object.keys(this.item).length > 0; + } + + protected get item(): any { + if (!this.outlet.context) { + return null; + } + const { _field, _options, _type, _i18nRoot, ...all } = this.outlet.context; + return all; + } } diff --git a/feature-libs/organization/administration/components/unit/form/unit-form.component.html b/feature-libs/organization/administration/components/unit/form/unit-form.component.html index bdb5909fd14..eeb3aa608b1 100644 --- a/feature-libs/organization/administration/components/unit/form/unit-form.component.html +++ b/feature-libs/organization/administration/components/unit/form/unit-form.component.html @@ -1,5 +1,5 @@ - +