Skip to content

Commit

Permalink
Merge branch 'main' into 1183-add-icon-button-+-dropdown-multselect
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicius-guedes-brisa authored Nov 18, 2024
2 parents 16c4478 + 9c2ed06 commit 0175a89
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions projects/ion/src/lib/core/types/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export interface NotificationConfigOptions {
fadeOut?: fadeOutDirection;
ionOnClose?: EventEmitter<void>;
}

export interface NotificationServiceConfig {
maxStack?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ import { Component, ComponentRef, Renderer2, ElementRef } from '@angular/core';
styleUrls: ['notification.container.scss'],
})
export class IonNotificationContainerComponent {
private notificationRefControl: ComponentRef<IonNotificationComponent>[] = [];
constructor(private renderer: Renderer2, private element: ElementRef) {}

addNotification(notification: ComponentRef<IonNotificationComponent>): void {
addNotification(
notification: ComponentRef<IonNotificationComponent>,
maxStack: number
): void {
if (this.notificationRefControl.length === maxStack) {
this.notificationRefControl[0].instance.ionOnClose.complete();
this.removeNotification(
this.notificationRefControl[0].location.nativeElement
);
this.notificationRefControl.shift();
}

this.notificationRefControl.push(notification);
notification.instance.ionOnClose.subscribe(() => {
this.removeNotification(notification.location.nativeElement);
this.notificationRefControl = this.notificationRefControl.filter(
(value) => value !== notification
);
});

this.renderer.appendChild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,33 @@ describe('NotificationService -> notification types', () => {
}
);
});

describe('NotificationService -> notification maxStack', () => {
let notificationService: IonNotificationService;

it('should not exceed the maxStack', () => {
const removeNotification = screen.getAllByTestId('btn-remove');
removeNotification.forEach((element) => fireEvent.click(element));

TestBed.configureTestingModule({
imports: [TestModule],
}).compileComponents();

notificationService = TestBed.get(IonNotificationService);

notificationService.notificationServiceConfig = { maxStack: 2 };
notificationService.success(
DEFAULT_NOTIFICATION_OPTIONS.title,
DEFAULT_NOTIFICATION_OPTIONS.message
);
notificationService.error(
DEFAULT_NOTIFICATION_OPTIONS.title,
DEFAULT_NOTIFICATION_OPTIONS.message
);
notificationService.error(
DEFAULT_NOTIFICATION_OPTIONS.title,
DEFAULT_NOTIFICATION_OPTIONS.message
);
expect(screen.getAllByTestId('ion-notification').length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { StatusType } from './../../core/types/status';
import { NotificationConfigOptions } from './../../core/types/notification';
import {
NotificationConfigOptions,
NotificationServiceConfig,
} from './../../core/types/notification';
import { IonNotificationComponent } from './../component/notification.component';
import {
Injectable,
Expand All @@ -25,6 +28,7 @@ enum NOTIFICATION_TYPES {
providedIn: 'root',
})
export class IonNotificationService {
public notificationServiceConfig: NotificationServiceConfig = { maxStack: 8 };
private notificationContainerComponentRef: ComponentRef<IonNotificationContainerComponent>;
private componentSubscriber!: Subject<SafeAny>;

Expand Down Expand Up @@ -183,7 +187,8 @@ export class IonNotificationService {
notification.changeDetectorRef.detectChanges();

this.notificationContainerComponentRef.instance.addNotification(
notification
notification,
this.notificationServiceConfig.maxStack
);

this.notificationContainerComponentRef.changeDetectorRef.detectChanges();
Expand Down

0 comments on commit 0175a89

Please sign in to comment.