Skip to content

Commit 2c1745c

Browse files
committed
Refactor checkJobStatus() to use API URLs provided via context
1 parent 950ce94 commit 2c1745c

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

netbox/project-static/dist/jobs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/dist/jobs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/src/jobs.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { apiGetBase, hasError, getNetboxData } from './util';
44
let timeout: number = 1000;
55

66
interface JobInfo {
7-
id: Nullable<string>;
7+
url: Nullable<string>;
88
complete: boolean;
99
}
1010

@@ -23,15 +23,16 @@ function asyncTimeout(ms: number) {
2323
function getJobInfo(): JobInfo {
2424
let complete = false;
2525

26-
const id = getNetboxData('data-job-id');
27-
const jobComplete = getNetboxData('data-job-complete');
26+
// Determine the API URL for the job status
27+
const url = getNetboxData('data-job-url');
2828

2929
// Determine the job completion status, if present. If the job is not complete, the value will be
3030
// "None". Otherwise, it will be a stringified date.
31+
const jobComplete = getNetboxData('data-job-complete');
3132
if (typeof jobComplete === 'string' && jobComplete.toLowerCase() !== 'none') {
3233
complete = true;
3334
}
34-
return { id, complete };
35+
return { url, complete };
3536
}
3637

3738
/**
@@ -59,10 +60,10 @@ function updateLabel(status: JobStatus) {
5960

6061
/**
6162
* Recursively check the job's status.
62-
* @param id Job ID
63+
* @param url API URL for job result
6364
*/
64-
async function checkJobStatus(id: string) {
65-
const res = await apiGetBase<APIJobResult>(`/api/extras/job-results/${id}/`);
65+
async function checkJobStatus(url: string) {
66+
const res = await apiGetBase<APIJobResult>(url);
6667
if (hasError(res)) {
6768
// If the response is an API error, display an error message and stop checking for job status.
6869
const toast = createToast('danger', 'Error', res.error);
@@ -82,17 +83,17 @@ async function checkJobStatus(id: string) {
8283
if (timeout < 10000) {
8384
timeout += 1000;
8485
}
85-
await Promise.all([checkJobStatus(id), asyncTimeout(timeout)]);
86+
await Promise.all([checkJobStatus(url), asyncTimeout(timeout)]);
8687
}
8788
}
8889
}
8990

9091
function initJobs() {
91-
const { id, complete } = getJobInfo();
92+
const { url, complete } = getJobInfo();
9293

93-
if (id !== null && !complete) {
94+
if (url !== null && !complete) {
9495
// If there is a job ID and it is not completed, check for the job's status.
95-
Promise.resolve(checkJobStatus(id));
96+
Promise.resolve(checkJobStatus(url));
9697
}
9798
}
9899

netbox/templates/extras/report_result.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ <h5 class="card-header">
9696
{% endblock %}
9797

9898
{% block data %}
99-
<span data-job-id="{{ result.pk }}"></span>
99+
<span data-job-url="{% url 'extras-api:jobresult-detail' pk=result.pk %}"></span>
100100
<span data-job-complete="{{ result.completed }}"></span>
101101
{% endblock %}

netbox/templates/extras/script_result.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ <h5 class="card-header">
112112
{% endblock content-wrapper %}
113113

114114
{% block data %}
115-
<span data-job-id="{{ result.pk }}"></span>
115+
<span data-job-url="{% url 'extras-api:jobresult-detail' pk=result.pk %}"></span>
116116
<span data-job-complete="{{ result.completed }}"></span>
117117
{% endblock %}

0 commit comments

Comments
 (0)