2024-07-07 17:32:30 +02:00
|
|
|
import {hideElem, showElem} from '../utils/dom.ts';
|
|
|
|
import {GET, POST} from '../modules/fetch.ts';
|
2022-01-28 22:00:11 +01:00
|
|
|
|
2023-09-19 02:50:30 +02:00
|
|
|
const {appSubUrl} = window.config;
|
2021-10-16 19:28:04 +02:00
|
|
|
|
|
|
|
export function initRepoMigrationStatusChecker() {
|
2024-06-10 22:49:33 +02:00
|
|
|
const repoMigrating = document.querySelector('#repo_migrating');
|
2024-02-17 21:07:47 +01:00
|
|
|
if (!repoMigrating) return;
|
2023-05-11 10:25:46 +02:00
|
|
|
|
2024-06-10 22:49:33 +02:00
|
|
|
document.querySelector('#repo_migrating_retry').addEventListener('click', doMigrationRetry);
|
2023-08-04 04:21:32 +02:00
|
|
|
|
2024-02-17 21:07:47 +01:00
|
|
|
const task = repoMigrating.getAttribute('data-migrating-task-id');
|
2023-05-11 10:25:46 +02:00
|
|
|
|
2024-02-17 21:07:47 +01:00
|
|
|
// returns true if the refresh still needs to be called after a while
|
2023-05-11 10:25:46 +02:00
|
|
|
const refresh = async () => {
|
2023-09-19 02:50:30 +02:00
|
|
|
const res = await GET(`${appSubUrl}/user/task/${task}`);
|
2023-05-11 10:25:46 +02:00
|
|
|
if (res.status !== 200) return true; // continue to refresh if network error occurs
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
|
|
// for all status
|
|
|
|
if (data.message) {
|
2024-06-10 22:49:33 +02:00
|
|
|
document.querySelector('#repo_migrating_progress_message').textContent = data.message;
|
2023-05-11 10:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskStatusFinished
|
|
|
|
if (data.status === 4) {
|
|
|
|
window.location.reload();
|
|
|
|
return false;
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-05-11 10:25:46 +02:00
|
|
|
|
|
|
|
// TaskStatusFailed
|
|
|
|
if (data.status === 3) {
|
|
|
|
hideElem('#repo_migrating_progress');
|
|
|
|
hideElem('#repo_migrating');
|
2023-08-04 04:21:32 +02:00
|
|
|
showElem('#repo_migrating_retry');
|
2023-05-11 10:25:46 +02:00
|
|
|
showElem('#repo_migrating_failed');
|
|
|
|
showElem('#repo_migrating_failed_image');
|
2024-06-10 22:49:33 +02:00
|
|
|
document.querySelector('#repo_migrating_failed_error').textContent = data.message;
|
2023-05-11 10:25:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true; // continue to refresh
|
|
|
|
};
|
|
|
|
|
|
|
|
const syncTaskStatus = async () => {
|
|
|
|
let doNextRefresh = true;
|
|
|
|
try {
|
|
|
|
doNextRefresh = await refresh();
|
|
|
|
} finally {
|
|
|
|
if (doNextRefresh) {
|
|
|
|
setTimeout(syncTaskStatus, 2000);
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-05-11 10:25:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
syncTaskStatus(); // no await
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-08-04 04:21:32 +02:00
|
|
|
|
|
|
|
async function doMigrationRetry(e) {
|
2024-02-17 21:07:47 +01:00
|
|
|
await POST(e.target.getAttribute('data-migrating-task-retry-url'));
|
2023-08-04 04:21:32 +02:00
|
|
|
window.location.reload();
|
|
|
|
}
|