summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/import_entities/import_groups/graphql/services/status_poller.js
blob: 63cd6b48fc4661d74bf08e3f7cbd8a85e3a93a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Visibility from 'visibilityjs';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import Poll from '~/lib/utils/poll';
import { s__ } from '~/locale';
import { SourceGroupsManager } from './source_groups_manager';

export class StatusPoller {
  constructor({ client, pollPath }) {
    this.client = client;

    this.eTagPoll = new Poll({
      resource: {
        fetchJobs: () => axios.get(pollPath),
      },
      method: 'fetchJobs',
      successCallback: ({ data }) => this.updateImportsStatuses(data),
      errorCallback: () =>
        createFlash({
          message: s__('BulkImport|Update of import statuses with realtime changes failed'),
        }),
    });

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

    this.groupManager = new SourceGroupsManager({ client });
  }

  startPolling() {
    this.eTagPoll.makeRequest();
  }

  async updateImportsStatuses(importStatuses) {
    importStatuses.forEach(({ id, status_name: statusName }) => {
      const group = this.groupManager.findByImportId(id);
      if (group.id) {
        this.groupManager.setImportStatus(group, statusName);
      }
    });
  }
}