Added actions to notifications state

This commit is contained in:
Giuseppe Digilio
2018-03-05 13:01:54 +01:00
parent cc615ac0bf
commit e1e7840ec7
3 changed files with 34 additions and 10 deletions

View File

@@ -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;

View File

@@ -24,12 +24,12 @@ export class NotificationsEffects {
* Authenticate user.
* @method authenticate
*/
@Effect()
/*@Effect()
public timer: Observable<Action> = 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())
);*/

View File

@@ -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<INotification> {
}
/**
@@ -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;
};