Skip to content
Snippets Groups Projects
Commit e1e7840e authored by Giuseppe Digilio's avatar Giuseppe Digilio
Browse files

Added actions to notifications state

parent cc615ac0
No related merge requests found
...@@ -10,6 +10,7 @@ import { Notification } from './models/notification.model'; ...@@ -10,6 +10,7 @@ import { Notification } from './models/notification.model';
export const NotificationsActionTypes = { export const NotificationsActionTypes = {
NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'), NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'),
NEW_NOTIFICATION_WITH_TIMER: type('dspace/notifications/NEW_NOTIFICATION_WITH_TIMER'), 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'), REMOVE_NOTIFICATION: type('dspace/notifications/REMOVE_NOTIFICATION'),
}; };
...@@ -44,14 +45,28 @@ export class NewNotificationWithTimerAction implements Action { ...@@ -44,14 +45,28 @@ export class NewNotificationWithTimerAction implements Action {
} }
/** /**
* New notification. * Remove all notifications.
* @class NewNotificationAction * @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} * @implements {Action}
*/ */
export class RemoveNotificationAction implements Action { export class RemoveNotificationAction implements Action {
public type: string = NotificationsActionTypes.REMOVE_NOTIFICATION; public type: string = NotificationsActionTypes.REMOVE_NOTIFICATION;
payload: any;
constructor(public payload?: any) { } constructor(notificationId: any) {
this.payload = notificationId;
}
} }
/* tslint:enable:max-classes-per-file */ /* tslint:enable:max-classes-per-file */
...@@ -63,4 +78,5 @@ export class RemoveNotificationAction implements Action { ...@@ -63,4 +78,5 @@ export class RemoveNotificationAction implements Action {
export type NotificationsActions export type NotificationsActions
= NewNotificationAction = NewNotificationAction
| NewNotificationWithTimerAction | NewNotificationWithTimerAction
| RemoveAllNotificationsAction
| RemoveNotificationAction; | RemoveNotificationAction;
...@@ -24,12 +24,12 @@ export class NotificationsEffects { ...@@ -24,12 +24,12 @@ export class NotificationsEffects {
* Authenticate user. * Authenticate user.
* @method authenticate * @method authenticate
*/ */
@Effect() /*@Effect()
public timer: Observable<Action> = this.actions$ public timer: Observable<Action> = this.actions$
.ofType(NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER) .ofType(NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER)
.debounceTime((action) => action.payload.options.timeOut) .debounceTime((action) => action.payload.options.timeOut as number)
.map(() => new RemoveNotificationAction()); .map(() => new RemoveNotificationAction());
/* .switchMap((action: NewNotificationWithTimerAction) => Observable .switchMap((action: NewNotificationWithTimerAction) => Observable
.timer(30000) .timer(30000)
.mapTo(() => new RemoveNotificationAction()) .mapTo(() => new RemoveNotificationAction())
);*/ );*/
......
// import actions // import actions
import { NotificationsActions, NotificationsActionTypes } from './notifications.actions'; import { NotificationsActions, NotificationsActionTypes, RemoveNotificationAction } from './notifications.actions';
// import models // import models
import { INotification } from './models/notification.model'; import { INotification } from './models/notification.model';
...@@ -8,8 +8,8 @@ import { INotification } from './models/notification.model'; ...@@ -8,8 +8,8 @@ import { INotification } from './models/notification.model';
* The auth state. * The auth state.
* @interface State * @interface State
*/ */
export interface NotificationsState { export interface NotificationsState extends Array<INotification> {
[index: number]: INotification;
} }
/** /**
...@@ -30,10 +30,18 @@ export function notificationsReducer(state: any = initialState, action: Notifica ...@@ -30,10 +30,18 @@ export function notificationsReducer(state: any = initialState, action: Notifica
case NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER: case NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER:
return [...state, action.payload]; return [...state, action.payload];
case NotificationsActionTypes.REMOVE_NOTIFICATION: case NotificationsActionTypes.REMOVE_ALL_NOTIFICATIONS:
return []; return [];
case NotificationsActionTypes.REMOVE_NOTIFICATION:
return removeNotification(state, action as RemoveNotificationAction);
default: default:
return state; return state;
} }
} }
const removeNotification = (state: NotificationsState, action: RemoveNotificationAction): NotificationsState => {
const newState = state.filter((item: INotification) => item.id !== action.payload);
return newState;
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment