/*
 * Lumeer: Modern Data Definition and Processing Platform
 *
 * Copyright (C) since 2017 Answer Institute, s.r.o. and/or its affiliates.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import {Injectable} from '@angular/core';
import {Actions, Effect} from '@ngrx/effects';
import {Action} from '@ngrx/store';
import {Observable} from 'rxjs/Observable';
import {tap} from 'rxjs/operators';
import {NotificationService} from '../../notifications/notification.service';
import {NotificationsAction, NotificationsActionType} from './notifications.action';

@Injectable()
export class NotificationsEffects {

  @Effect({dispatch: false})
  public error$: Observable<Action> = this.actions.ofType(NotificationsActionType.ERROR).pipe(
    tap((action: NotificationsAction.Error) => this.notificationService.error(action.payload.message))
  );

  @Effect({dispatch: false})
  public success$: Observable<Action> = this.actions.ofType(NotificationsActionType.SUCCESS).pipe(
    tap((action: NotificationsAction.Success) => this.notificationService.success(action.payload.message))
  );

  constructor(private actions: Actions,
              private notificationService: NotificationService) {
  }
}