summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/repo-migrate.js
blob: e57348d31b4d9d340a2ce0a30b8cf3e1f7b2a289 (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
48
49
50
51
52
53
54
55
import $ from 'jquery';
import {hideElem, showElem} from '../utils/dom.js';

const {appSubUrl} = window.config;

export function initRepoMigrationStatusChecker() {
  const $repoMigrating = $('#repo_migrating');
  if (!$repoMigrating.length) return;

  const task = $repoMigrating.attr('data-migrating-task-id');

  // returns true if the refresh still need to be called after a while
  const refresh = async () => {
    const res = await fetch(`${appSubUrl}/user/task/${task}`);
    if (res.status !== 200) return true; // continue to refresh if network error occurs

    const data = await res.json();

    // for all status
    if (data.message) {
      $('#repo_migrating_progress_message').text(data.message);
    }

    // TaskStatusFinished
    if (data.status === 4) {
      window.location.reload();
      return false;
    }

    // TaskStatusFailed
    if (data.status === 3) {
      hideElem('#repo_migrating_progress');
      hideElem('#repo_migrating');
      showElem('#repo_migrating_failed');
      showElem('#repo_migrating_failed_image');
      $('#repo_migrating_failed_error').text(data.message);
      return false;
    }

    return true; // continue to refresh
  };

  const syncTaskStatus = async () => {
    let doNextRefresh = true;
    try {
      doNextRefresh = await refresh();
    } finally {
      if (doNextRefresh) {
        setTimeout(syncTaskStatus, 2000);
      }
    }
  };

  syncTaskStatus(); // no await
}