pipeline_details_mediator.js 1.47 KB
Newer Older
1
import Visibility from 'visibilityjs';
Phil Hughes's avatar
Phil Hughes committed
2
import Flash from '../flash';
3
import Poll from '../lib/utils/poll';
4
import { __ } from '../locale';
5 6 7 8
import PipelineStore from './stores/pipeline_store';
import PipelineService from './services/pipeline_service';

export default class pipelinesMediator {
9 10
  constructor(options = {}) {
    this.options = options;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    this.store = new PipelineStore();
    this.service = new PipelineService(options.endpoint);

    this.state = {};
    this.state.isLoading = false;
  }

  fetchPipeline() {
    this.poll = new Poll({
      resource: this.service,
      method: 'getPipeline',
      successCallback: this.successCallback.bind(this),
      errorCallback: this.errorCallback.bind(this),
    });

    if (!Visibility.hidden()) {
      this.state.isLoading = true;
      this.poll.makeRequest();
29 30
    } else {
      this.refreshPipeline();
31 32 33 34 35 36 37 38 39 40 41 42
    }

    Visibility.change(() => {
      if (!Visibility.hidden()) {
        this.poll.restart();
      } else {
        this.poll.stop();
      }
    });
  }

  successCallback(response) {
43 44
    this.state.isLoading = false;
    this.store.storePipeline(response.data);
45 46 47 48
  }

  errorCallback() {
    this.state.isLoading = false;
49
    Flash(__('An error occurred while fetching the pipeline.'));
50
  }
51 52

  refreshPipeline() {
53 54 55
    this.poll.stop();

    return this.service.getPipeline()
56
      .then(response => this.successCallback(response))
57 58
      .catch(() => this.errorCallback())
      .finally(() => this.poll.restart());
59
  }
60
}