From e1e7840ec78e1bda734b471f441c671336496eca Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Mon, 5 Mar 2018 13:01:54 +0100 Subject: [PATCH] Added actions to notifications state --- .../notifications/notifications.actions.ts | 22 ++++++++++++++++--- .../notifications/notifications.effects.ts | 6 ++--- .../notifications/notifications.reducers.ts | 16 ++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/app/shared/notifications/notifications.actions.ts b/src/app/shared/notifications/notifications.actions.ts index e086e409e5..6bd76a920d 100644 --- a/src/app/shared/notifications/notifications.actions.ts +++ b/src/app/shared/notifications/notifications.actions.ts @@ -10,6 +10,7 @@ import { Notification } from './models/notification.model'; export const NotificationsActionTypes = { NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'), NEW_NOTIFICATION_WITH_TIMER: type('dspace/notifications/NEW_NOTIFICATION_WITH_TIMER'), + REMOVE_ALL_NOTIFICATIONS: type('dspace/notifications/REMOVE_ALL_NOTIFICATIONS'), REMOVE_NOTIFICATION: type('dspace/notifications/REMOVE_NOTIFICATION'), }; @@ -44,14 +45,28 @@ export class NewNotificationWithTimerAction implements Action { } /** - * New notification. - * @class NewNotificationAction + * Remove all notifications. + * @class RemoveAllNotificationsAction + * @implements {Action} + */ +export class RemoveAllNotificationsAction implements Action { + public type: string = NotificationsActionTypes.REMOVE_ALL_NOTIFICATIONS; + + constructor(public payload?: any) { } +} + +/** + * Remove a notification. + * @class RemoveNotificationAction * @implements {Action} */ export class RemoveNotificationAction implements Action { public type: string = NotificationsActionTypes.REMOVE_NOTIFICATION; + payload: any; - constructor(public payload?: any) { } + constructor(notificationId: any) { + this.payload = notificationId; + } } /* tslint:enable:max-classes-per-file */ @@ -63,4 +78,5 @@ export class RemoveNotificationAction implements Action { export type NotificationsActions = NewNotificationAction | NewNotificationWithTimerAction + | RemoveAllNotificationsAction | RemoveNotificationAction; diff --git a/src/app/shared/notifications/notifications.effects.ts b/src/app/shared/notifications/notifications.effects.ts index 75fd319ba2..fe6637b561 100644 --- a/src/app/shared/notifications/notifications.effects.ts +++ b/src/app/shared/notifications/notifications.effects.ts @@ -24,12 +24,12 @@ export class NotificationsEffects { * Authenticate user. * @method authenticate */ - @Effect() + /*@Effect() public timer: Observable = this.actions$ .ofType(NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER) - .debounceTime((action) => action.payload.options.timeOut) + .debounceTime((action) => action.payload.options.timeOut as number) .map(() => new RemoveNotificationAction()); - /* .switchMap((action: NewNotificationWithTimerAction) => Observable + .switchMap((action: NewNotificationWithTimerAction) => Observable .timer(30000) .mapTo(() => new RemoveNotificationAction()) );*/ diff --git a/src/app/shared/notifications/notifications.reducers.ts b/src/app/shared/notifications/notifications.reducers.ts index 4b02ee905d..246009fdfa 100644 --- a/src/app/shared/notifications/notifications.reducers.ts +++ b/src/app/shared/notifications/notifications.reducers.ts @@ -1,5 +1,5 @@ // import actions -import { NotificationsActions, NotificationsActionTypes } from './notifications.actions'; +import { NotificationsActions, NotificationsActionTypes, RemoveNotificationAction } from './notifications.actions'; // import models import { INotification } from './models/notification.model'; @@ -8,8 +8,8 @@ import { INotification } from './models/notification.model'; * The auth state. * @interface State */ -export interface NotificationsState { - [index: number]: INotification; +export interface NotificationsState extends Array { + } /** @@ -30,10 +30,18 @@ export function notificationsReducer(state: any = initialState, action: Notifica case NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER: return [...state, action.payload]; - case NotificationsActionTypes.REMOVE_NOTIFICATION: + case NotificationsActionTypes.REMOVE_ALL_NOTIFICATIONS: return []; + case NotificationsActionTypes.REMOVE_NOTIFICATION: + return removeNotification(state, action as RemoveNotificationAction); + default: return state; } } + +const removeNotification = (state: NotificationsState, action: RemoveNotificationAction): NotificationsState => { + const newState = state.filter((item: INotification) => item.id !== action.payload); + return newState; +};