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

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

  getFilesByTaskId$(taskId: number) {
    return this.store.select(selectors.selectFilesByTaskId, taskId).pipe(tap((files) => files.length === 0 && this.getFiles(taskId)));
  }

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