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

Revert "[ACS-5311] Notification History Bug Fix" As storage quotas issues arise #9096

Merged
merged 1 commit into from
Nov 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
id="adf-notification-history-open-button"
(menuOpened)="onMenuOpened()">
<mat-icon matBadge="&#8288;"
[matBadgeHidden]="!unreadNotifications.length"
[matBadgeHidden]="!notifications.length"
matBadgeColor="accent"
matBadgeSize="small">notifications</mat-icon>
</button>
Expand All @@ -21,7 +21,7 @@
(click)="$event.stopPropagation()">
<div mat-subheader role="menuitem">
<span>{{ 'NOTIFICATIONS.TITLE' | translate }}</span>
<button *ngIf="unreadNotifications.length"
<button *ngIf="notifications.length"
id="adf-notification-history-mark-as-read"
mat-icon-button
title="{{ 'NOTIFICATIONS.MARK_AS_READ' | translate }}"
Expand All @@ -33,7 +33,7 @@
<mat-divider></mat-divider>

<mat-list role="menuitem">
<ng-container *ngIf="unreadNotifications.length; else empty_list_template">
<ng-container *ngIf="notifications.length; else empty_list_template">
<mat-list-item *ngFor="let notification of paginatedNotifications"
class="adf-notification-history-menu-item"
(click)="onNotificationClick(notification)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { OverlayContainer } from '@angular/cdk/overlay';
import { NotificationService } from '../services/notification.service';
import { StorageService } from '../../common/services/storage.service';
import { TranslateModule } from '@ngx-translate/core';
import { NotificationModel, NOTIFICATION_TYPE, NOTIFICATION_STORAGE } from '../models/notification.model';
import { NotificationModel, NOTIFICATION_TYPE } from '../models/notification.model';

describe('Notification History Component', () => {

Expand All @@ -32,12 +32,11 @@ describe('Notification History Component', () => {
let notificationService: NotificationService;
let overlayContainerElement: HTMLElement;
let storage: StorageService;
let testNotifications: NotificationModel[];

const openNotification = () => {
fixture.detectChanges();
const button = element.querySelector<HTMLButtonElement>('#adf-notification-history-open-button');
button?.click();
button.click();
fixture.detectChanges();
};

Expand All @@ -55,34 +54,14 @@ describe('Notification History Component', () => {
storage = TestBed.inject(StorageService);
notificationService = TestBed.inject(NotificationService);
component.notifications = [];
component.unreadNotifications = [];

testNotifications = [
{
type: NOTIFICATION_TYPE.INFO,
icon: 'info',
datetime: new Date(),
initiator: { key: '*', displayName: 'SYSTEM' },
messages: ['Moved 1 item.'],
read: false
},
{
type: NOTIFICATION_TYPE.INFO,
icon: 'info',
datetime: new Date(),
initiator: { key: '*', displayName: 'SYSTEM' },
messages: ['Copied 1 item.'],
read: false
}
];
});

beforeEach(inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainerElement = oc.getContainerElement();
}));

afterEach(() => {
storage.removeItem(NOTIFICATION_STORAGE);
storage.removeItem(NotificationHistoryComponent.NOTIFICATION_STORAGE);
fixture.destroy();
});

Expand All @@ -104,11 +83,12 @@ describe('Notification History Component', () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(component.unreadNotifications.length).toBe(1);
expect(component.notifications.length).toBe(1);
const markAllAsRead = overlayContainerElement.querySelector<HTMLButtonElement>('#adf-notification-history-mark-as-read');
markAllAsRead?.click();
markAllAsRead.click();
fixture.detectChanges();
expect(component.unreadNotifications).toEqual([]);
expect(storage.getItem(NotificationHistoryComponent.NOTIFICATION_STORAGE)).toBeNull();
expect(component.notifications.length).toBe(0);
done();
});
});
Expand All @@ -121,7 +101,7 @@ describe('Notification History Component', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(overlayContainerElement.querySelector('#adf-notification-history-component-no-message')).toBeNull();
expect(overlayContainerElement.querySelector('.adf-notification-history-list')?.innerHTML).toContain('Example Message');
expect(overlayContainerElement.querySelector('.adf-notification-history-list').innerHTML).toContain('Example Message');
done();
});
});
Expand All @@ -143,7 +123,7 @@ describe('Notification History Component', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
const notification = overlayContainerElement.querySelector<HTMLButtonElement>('.adf-notification-history-menu-item');
notification?.click();
notification.click();
expect(callBackSpy).toHaveBeenCalled();
done();
});
Expand All @@ -168,7 +148,7 @@ describe('Notification History Component', () => {
});

it('should read notifications from local storage', (done) => {
storage.setItem(NOTIFICATION_STORAGE, JSON.stringify([{
storage.setItem(NotificationHistoryComponent.NOTIFICATION_STORAGE, JSON.stringify([{
messages: ['My new message'],
datetime: new Date(),
type: NOTIFICATION_TYPE.RECURSIVE
Expand Down Expand Up @@ -202,30 +182,4 @@ describe('Notification History Component', () => {
});
}, 45000);
});

it('should set unreadNotifications to an empty array when there are no notifications', () => {
component.notifications = [];
fixture.detectChanges();

expect(component.unreadNotifications).toEqual([]);
});

it('should set unreadNotifications to an empty array when all notifications are read', () => {
testNotifications.forEach((notification: NotificationModel) => {
notification.read = true;
});
storage.setItem(NOTIFICATION_STORAGE, JSON.stringify(testNotifications));
fixture.detectChanges();

expect(component.unreadNotifications).toEqual([]);
});

it('should set unreadNotifications by filtering notifications where read is false', () => {
testNotifications[0].read = true;
storage.setItem(NOTIFICATION_STORAGE, JSON.stringify(testNotifications));
fixture.detectChanges();

expect(component.unreadNotifications.length).toEqual(1);
expect(component.unreadNotifications[0].read).toEqual(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { Component, Input, ViewChild, OnDestroy, OnInit, AfterViewInit, ChangeDetectorRef, ViewEncapsulation } from '@angular/core';
import { NotificationService } from '../services/notification.service';
import { NOTIFICATION_STORAGE, NotificationModel } from '../models/notification.model';
import { NotificationModel, NOTIFICATION_TYPE } from '../models/notification.model';
import { MatMenuTrigger, MenuPositionX, MenuPositionY } from '@angular/material/menu';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
Expand All @@ -33,6 +33,7 @@ import { PaginationModel } from '../../models/pagination.model';
export class NotificationHistoryComponent implements OnDestroy, OnInit, AfterViewInit {

public static MAX_NOTIFICATION_STACK_LENGTH = 100;
public static NOTIFICATION_STORAGE = 'notification-history';

@ViewChild(MatMenuTrigger, { static: true })
trigger: MatMenuTrigger;
Expand All @@ -52,17 +53,17 @@ export class NotificationHistoryComponent implements OnDestroy, OnInit, AfterVie
onDestroy$ = new Subject<boolean>();
notifications: NotificationModel[] = [];
paginatedNotifications: NotificationModel[] = [];
unreadNotifications: NotificationModel[] = [];
pagination: PaginationModel;

constructor(private notificationService: NotificationService, public storageService: StorageService, public cd: ChangeDetectorRef) {}
constructor(
private notificationService: NotificationService,
public storageService: StorageService,
public cd: ChangeDetectorRef) {

}

ngOnInit() {
this.notifications = JSON.parse(this.storageService.getItem(NOTIFICATION_STORAGE)) || [];
this.unreadNotifications =
JSON.parse(this.storageService.getItem(NOTIFICATION_STORAGE))?.filter(
(notification: NotificationModel) => !notification.read
) || [];
this.notifications = JSON.parse(this.storageService.getItem(NotificationHistoryComponent.NOTIFICATION_STORAGE)) || [];
}

ngAfterViewInit(): void {
Expand All @@ -80,22 +81,20 @@ export class NotificationHistoryComponent implements OnDestroy, OnInit, AfterVie
}

addNewNotification(notification: NotificationModel) {
this.unreadNotifications.unshift(notification);
this.notifications.unshift(notification);

if (this.unreadNotifications.length > NotificationHistoryComponent.MAX_NOTIFICATION_STACK_LENGTH) {
this.unreadNotifications.shift();
if (this.notifications.length > NotificationHistoryComponent.MAX_NOTIFICATION_STACK_LENGTH) {
this.notifications.shift();
}

this.saveNotifications();
this.createPagination();
}

saveNotifications() {
this.unreadNotifications.forEach((notification: NotificationModel) => {
this.notifications.push(notification);
});

this.storageService.setItem(NOTIFICATION_STORAGE, JSON.stringify(this.notifications));
this.storageService.setItem(NotificationHistoryComponent.NOTIFICATION_STORAGE, JSON.stringify(this.notifications.filter((notification) =>
notification.type !== NOTIFICATION_TYPE.RECURSIVE
)));
}

onMenuOpened() {
Expand All @@ -113,13 +112,9 @@ export class NotificationHistoryComponent implements OnDestroy, OnInit, AfterVie
}

markAsRead() {
this.unreadNotifications = [];
this.notifications.forEach((notification: NotificationModel) => {
notification.read = true;
});

this.storageService.setItem(NOTIFICATION_STORAGE, JSON.stringify(this.notifications));
this.notifications = [];
this.paginatedNotifications = [];
this.storageService.removeItem(NotificationHistoryComponent.NOTIFICATION_STORAGE);
this.createPagination();
this.trigger.closeMenu();
}
Expand All @@ -128,16 +123,16 @@ export class NotificationHistoryComponent implements OnDestroy, OnInit, AfterVie
this.pagination = {
skipCount: this.maxNotifications,
maxItems: this.maxNotifications,
totalItems: this.unreadNotifications.length,
hasMoreItems: this.unreadNotifications.length > this.maxNotifications
totalItems: this.notifications.length,
hasMoreItems: this.notifications.length > this.maxNotifications
};
this.paginatedNotifications = this.unreadNotifications.slice(0, this.pagination.skipCount);
this.paginatedNotifications = this.notifications.slice(0, this.pagination.skipCount);
}

loadMore() {
this.pagination.skipCount = this.pagination.maxItems + this.pagination.skipCount;
this.pagination.hasMoreItems = this.unreadNotifications.length > this.pagination.skipCount;
this.paginatedNotifications = this.unreadNotifications.slice(0, this.pagination.skipCount);
this.pagination.hasMoreItems = this.notifications.length > this.pagination.skipCount;
this.paginatedNotifications = this.notifications.slice(0, this.pagination.skipCount);
}

hasMoreNotifications(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,21 @@ export const info = (messages: string | string[], initiator: NotificationInitiat
icon: 'info',
datetime: new Date(),
initiator,
messages: [].concat(messages),
read: false
messages: [].concat(messages)
});

export const warning = (messages: string | string[], initiator: NotificationInitiator = rootInitiator): NotificationModel => ({
type: NOTIFICATION_TYPE.WARN,
icon: 'warning',
datetime: new Date(),
initiator,
messages: [].concat(messages),
read: false
messages: [].concat(messages)
});

export const error = (messages: string | string[], initiator: NotificationInitiator = rootInitiator): NotificationModel => ({
type: NOTIFICATION_TYPE.ERROR,
icon: 'error',
datetime: new Date(),
initiator,
messages: [].concat(messages),
read: false
messages: [].concat(messages)
});
3 changes: 0 additions & 3 deletions lib/core/src/lib/notifications/models/notification.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,4 @@ export interface NotificationModel {
icon?: string;
clickCallBack?: any;
args?: any;
read?: boolean;
}

export const NOTIFICATION_STORAGE = 'notification-history';
Loading