From 9512e5bd28dff85c3c69792ef79d26fb995768ef Mon Sep 17 00:00:00 2001 From: X4Lo Date: Wed, 3 Apr 2024 21:41:28 +0000 Subject: [PATCH] add notification service + component --- .../notification-popup.component.css | 16 +++ .../notification-popup.component.html | 13 +++ .../notification-popup.component.spec.ts | 21 ++++ .../notification-popup.component.ts | 30 +++++ .../shared/services/notification.service.ts | 109 ++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 frontend/src/app/shared/notification-popup/notification-popup.component.css create mode 100644 frontend/src/app/shared/notification-popup/notification-popup.component.html create mode 100644 frontend/src/app/shared/notification-popup/notification-popup.component.spec.ts create mode 100644 frontend/src/app/shared/notification-popup/notification-popup.component.ts create mode 100644 frontend/src/app/shared/services/notification.service.ts diff --git a/frontend/src/app/shared/notification-popup/notification-popup.component.css b/frontend/src/app/shared/notification-popup/notification-popup.component.css new file mode 100644 index 0000000..9c0f85d --- /dev/null +++ b/frontend/src/app/shared/notification-popup/notification-popup.component.css @@ -0,0 +1,16 @@ +@keyframes fadeIn { + from { opacity: 0; } + to { opacity: 1; } +} + +.notification { + position: fixed; + right: 20px; + bottom: 20px; + z-index: 1050; /* Ensure it's above other content */ + margin-bottom: 20px; /* Adjust based on size and desired spacing */ + animation: fadeIn 1s ease-out; /* Use the fadeIn animation, lasts 1 second */ +} +.notif-back { + background-color: aliceblue; +} diff --git a/frontend/src/app/shared/notification-popup/notification-popup.component.html b/frontend/src/app/shared/notification-popup/notification-popup.component.html new file mode 100644 index 0000000..6a475ca --- /dev/null +++ b/frontend/src/app/shared/notification-popup/notification-popup.component.html @@ -0,0 +1,13 @@ +
+
+
+
check_circle +
+
+
{{title}}
+
{{message}}
+
+
+ +
+
\ No newline at end of file diff --git a/frontend/src/app/shared/notification-popup/notification-popup.component.spec.ts b/frontend/src/app/shared/notification-popup/notification-popup.component.spec.ts new file mode 100644 index 0000000..ea77904 --- /dev/null +++ b/frontend/src/app/shared/notification-popup/notification-popup.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { NotificationPopupComponent } from './notification-popup.component'; + +describe('NotificationPopupComponent', () => { + let component: NotificationPopupComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [NotificationPopupComponent] + }); + fixture = TestBed.createComponent(NotificationPopupComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/shared/notification-popup/notification-popup.component.ts b/frontend/src/app/shared/notification-popup/notification-popup.component.ts new file mode 100644 index 0000000..9f64bd1 --- /dev/null +++ b/frontend/src/app/shared/notification-popup/notification-popup.component.ts @@ -0,0 +1,30 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-notification-popup', + templateUrl: './notification-popup.component.html', + styleUrls: ['./notification-popup.component.css'], +}) +export class NotificationPopupComponent { + show: boolean = false; + title: string = 'Alert'; + message: string = 'Alert Message...'; + type: string = 'alert'; + + constructor() {} + + ngOnInit(): void {} + + public showNotification( + title: string, + message: string, + type: string = 'alert', + timeout: number = 3000 + ): void { + this.title = title; + this.message = message; + this.type = type; + this.show = true; + setTimeout(() => (this.show = false), timeout); + } +} diff --git a/frontend/src/app/shared/services/notification.service.ts b/frontend/src/app/shared/services/notification.service.ts new file mode 100644 index 0000000..a053342 --- /dev/null +++ b/frontend/src/app/shared/services/notification.service.ts @@ -0,0 +1,109 @@ +import { + Injectable, + ComponentFactoryResolver, + ApplicationRef, + Injector, + EmbeddedViewRef, + ComponentRef, +} from '@angular/core'; +import { NotificationPopupComponent } from '../notification-popup/notification-popup.component'; + +@Injectable({ + providedIn: 'root', +}) +export class NotificationService { + private notificationComponents: ComponentRef[] = + []; + private notificationBottomMargin = 10; // Margin between notifications + private baseBottomPosition = 20; // Starting bottom position for the first notification + + constructor( + private componentFactoryResolver: ComponentFactoryResolver, + private appRef: ApplicationRef, + private injector: Injector + ) {} + + public showNotification( + title: string, + message: string, + type: string = 'alert', + timeout: number = 5000 + ): void { + // Create a component reference for the notification + const componentRef = this.componentFactoryResolver + .resolveComponentFactory(NotificationPopupComponent) + .create(this.injector); + + // Assign the message and type to the instance + componentRef.instance.title = title; + componentRef.instance.message = message; + componentRef.instance.type = type; + + // Attach the component to the application + this.appRef.attachView(componentRef.hostView); + + // Convert the component's host view into a DOM element and append it to the body + const domElem = (componentRef.hostView as EmbeddedViewRef) + .rootNodes[0] as HTMLElement; + document.body.appendChild(domElem); + + const position = + this.baseBottomPosition + + this.notificationComponents.length * + (domElem.offsetHeight + this.notificationBottomMargin); + domElem.style.bottom = `${position}px`; + domElem.style.position = 'fixed'; + domElem.style.right = '20px'; + + // Store the component reference for later removal + this.notificationComponents.push(componentRef); + + // Remove the notification after a delay + setTimeout(() => { + domElem.style.visibility = 'hidden'; + }, timeout); + + setTimeout(() => { + this.removeNotification(componentRef); + }, timeout + 1000); // Customize this value as needed + } + + // private removeNotification( + // componentRef: ComponentRef + // ): void { + // this.appRef.detachView(componentRef.hostView); + // componentRef.destroy(); + // this.notificationComponents = this.notificationComponents.filter( + // (c) => c !== componentRef + // ); + // } + + private removeNotification( + componentRef: ComponentRef + ): void { + const domElem = (componentRef.hostView as EmbeddedViewRef) + .rootNodes[0] as HTMLElement; + this.appRef.detachView(componentRef.hostView); + document.body.removeChild(domElem); // Make sure to remove the element from the body + componentRef.destroy(); + // Filter out the destroyed notification + this.notificationComponents = this.notificationComponents.filter( + (c) => c !== componentRef + ); + + // Adjust positions of remaining notifications + this.adjustPositions(); + } + + private adjustPositions(): void { + // Start from the initial position again and reposition each remaining notification + this.notificationComponents.forEach((componentRef, index) => { + const domElem = (componentRef.hostView as EmbeddedViewRef) + .rootNodes[0] as HTMLElement; + const position = + this.baseBottomPosition + + index * (domElem.offsetHeight + this.notificationBottomMargin); + domElem.style.bottom = `${position}px`; + }); + } +}