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

feat: add notifications limit option #1192

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
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
Loading