import { Injectable } from '@angular/core';
import * as selectors from './task-articles.selectors';
import * as actions from './task-articles.actions';
import { Store } from '@ngrx/store';
import { tap } from 'rxjs/operators';

@Injectable({ providedIn: 'root' })
export class TaskArticlesService {
  constructor(private store: Store<any>) {}

  getArticlesByTaskId$(taskId: number) {
    return this.store
      .select(selectors.selectArticlesByTaskId, taskId)
      .pipe(tap((article) => article.length === 0 && this.getArticles(taskId)));
  }

  getArticles(taskId: number) {
    this.store.dispatch(actions.getArticles({ taskId }));
  }
}
