Multi-printers #32
@@ -7,9 +7,9 @@
|
||||
<h3>Current tasks</h3>
|
||||
<hr>
|
||||
{% if not tasks %}
|
||||
<div class="alert alert-success" role="alert">No tasks in queue</div>
|
||||
<div class="alert alert-success" role="alert" id="no-tasks">No tasks in queue</div>
|
||||
{% endif %}
|
||||
<div class="row row-cols-1 row-cols-md-5 g-4">
|
||||
<div class="row row-cols-1 row-cols-md-5 g-4" id="task-list">
|
||||
{% for task in tasks %}
|
||||
<div class="col-sm-6 mb-3 mb-sm-0">
|
||||
<div class="card">
|
||||
@@ -44,4 +44,91 @@
|
||||
{% endfor %}
|
||||
|
||||
|
||||
<script>
|
||||
const taskList = document.getElementById("task-list");
|
||||
const queueUrl = "/api/queue";
|
||||
|
||||
// Create the alert message if it doesn't exist
|
||||
// If there is a task, the element is not created
|
||||
let noTasksAlert = document.getElementById("no-tasks");
|
||||
|
||||
if (!noTasksAlert) {
|
||||
noTasksAlert = document.createElement("div");
|
||||
noTasksAlert.textContent = "No tasks in queue";
|
||||
noTasksAlert.className = "alert alert-success";
|
||||
noTasksAlert.setAttribute("role", "alert");
|
||||
noTasksAlert.id = "no-tasks";
|
||||
taskList.before(noTasksAlert);
|
||||
}
|
||||
|
||||
function renderTasks(tasks) {
|
||||
taskList.replaceChildren();
|
||||
noTasksAlert.hidden = tasks.length > 0;
|
||||
|
||||
for (const task of tasks) {
|
||||
const col = document.createElement("div");
|
||||
col.className = "col-sm-6 mb-3 mb-sm-0";
|
||||
|
||||
const card = document.createElement("div");
|
||||
card.className = "card";
|
||||
|
||||
if (task.type === "TaskType.IMAGE") {
|
||||
const img = document.createElement("img");
|
||||
good_image_url = task.content.img.split('/').pop();
|
||||
img.src = "/static/uploads/" + good_image_url;
|
||||
img.className = "card-img-top";
|
||||
img.alt = "Task image";
|
||||
card.appendChild(img);
|
||||
} else if (task.type === "TaskType.CUT") {
|
||||
const img = document.createElement("img");
|
||||
img.src = "/static/images/cut.png";
|
||||
img.className = "card-img-top";
|
||||
img.alt = "Cut task";
|
||||
card.appendChild(img);
|
||||
}
|
||||
|
||||
const body = document.createElement("div");
|
||||
body.className = "card-body";
|
||||
|
||||
if (task.type === "TaskType.TEXT" && task.content?.txt) {
|
||||
const text = document.createElement("p");
|
||||
text.className = "card-text";
|
||||
text.textContent = `Text: ${task.content.txt}`;
|
||||
body.appendChild(text);
|
||||
}
|
||||
|
||||
const code_id = document.createElement("code");
|
||||
code_id.textContent = `${task.task_id}`;
|
||||
const id = document.createElement("p");
|
||||
id.className = "card-text";
|
||||
id.textContent = "ID : "
|
||||
id.appendChild(code_id);
|
||||
body.appendChild(id);
|
||||
|
||||
card.appendChild(body);
|
||||
col.appendChild(card);
|
||||
taskList.appendChild(col);
|
||||
}
|
||||
}
|
||||
|
||||
async function updateTasks() {
|
||||
try {
|
||||
const response = await fetch(queueUrl);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const tasks = await response.json();
|
||||
console.log(tasks);
|
||||
renderTasks(Array.isArray(tasks) ? tasks : []);
|
||||
} catch (error) {
|
||||
console.error("Could not update task list:", error);
|
||||
}
|
||||
}
|
||||
|
||||
updateTasks(); // First update on page load
|
||||
setInterval(updateTasks, 3000);
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
Reference in new issue
Block a user