BigW Consortium Gitlab

importer_status.js 2.46 KB
Newer Older
Filipa Lacerda committed
1 2 3 4 5 6 7
class ImporterStatus {
  constructor(jobsUrl, importUrl) {
    this.jobsUrl = jobsUrl;
    this.importUrl = importUrl;
    this.initStatusPage();
    this.setAutoUpdate();
  }
8

Filipa Lacerda committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  initStatusPage() {
    $('.js-add-to-import')
      .off('click')
      .on('click', (event) => {
        const $btn = $(event.currentTarget);
        const $tr = $btn.closest('tr');
        const $targetField = $tr.find('.import-target');
        const $namespaceInput = $targetField.find('.js-select-namespace option:selected');
        const id = $tr.attr('id').replace('repo_', '');
        let targetNamespace;
        let newName;
        if ($namespaceInput.length > 0) {
          targetNamespace = $namespaceInput[0].innerHTML;
          newName = $targetField.find('#path').prop('value');
          $targetField.empty().append(`${targetNamespace}/${newName}`);
        }
        $btn.disable().addClass('is-loading');
Fatih Acet committed
26

Filipa Lacerda committed
27 28 29 30 31 32 33 34 35 36 37 38 39
        return $.post(this.importUrl, {
          repo_id: id,
          target_namespace: targetNamespace,
          new_name: newName,
        }, {
          dataType: 'script',
        });
      });

    $('.js-import-all')
      .off('click')
      .on('click', function onClickImportAll() {
        const $btn = $(this);
Fatih Acet committed
40
        $btn.disable().addClass('is-loading');
Filipa Lacerda committed
41
        return $('.js-add-to-import').each(function triggerAddImport() {
Fatih Acet committed
42 43 44
          return $(this).trigger('click');
        });
      });
Filipa Lacerda committed
45 46 47 48 49 50
  }

  setAutoUpdate() {
    return setInterval(() => $.get(this.jobsUrl, data => $.each(data, (i, job) => {
      const jobItem = $(`#project_${job.id}`);
      const statusField = jobItem.find('.job-status');
Fatih Acet committed
51

Filipa Lacerda committed
52
      const spinner = '<i class="fa fa-spinner fa-spin"></i>';
Fatih Acet committed
53

Filipa Lacerda committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      switch (job.import_status) {
        case 'finished':
          jobItem.removeClass('active').addClass('success');
          statusField.html('<span><i class="fa fa-check"></i> done</span>');
          break;
        case 'scheduled':
          statusField.html(`${spinner} scheduled`);
          break;
        case 'started':
          statusField.html(`${spinner} started`);
          break;
        default:
          statusField.html(job.import_status);
          break;
      }
    })), 4000);
  }
}
Fatih Acet committed
72

Filipa Lacerda committed
73 74
// eslint-disable-next-line consistent-return
export default function initImporterStatus() {
Filipa Lacerda committed
75
  const importerStatus = document.querySelector('.js-importer-status');
76

Filipa Lacerda committed
77 78
  if (importerStatus) {
    const data = importerStatus.dataset;
Filipa Lacerda committed
79 80 81
    return new ImporterStatus(data.jobsImportPath, data.importPath);
  }
}