Add ImageWidget for image upload and deletion functionality with toast notifications
This commit is contained in:
parent
8710c09917
commit
d488324c50
4 changed files with 56 additions and 56 deletions
52
inventory/static/js/image.js
Normal file
52
inventory/static/js/image.js
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
const ImageWidget = (() => {
|
||||||
|
function submitImageUpload(id) {
|
||||||
|
const form = document.getElementById(`image-upload-form-${id}`);
|
||||||
|
const formData = new FormData(form);
|
||||||
|
|
||||||
|
fetch("/api/images", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData
|
||||||
|
}).then(async response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
// Try to parse JSON, fallback to text
|
||||||
|
const contentType = response.headers.get("Content-Type") || "";
|
||||||
|
let errorDetails;
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
errorDetails = await response.json();
|
||||||
|
} else {
|
||||||
|
errorDetails = { error: await response.text() };
|
||||||
|
}
|
||||||
|
throw errorDetails;
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}).then(data => {
|
||||||
|
Toast.renderToast({ message: `Image uploaded.`, type: "success" });
|
||||||
|
location.reload();
|
||||||
|
}).catch(err => {
|
||||||
|
const msg = typeof err === "object" && err.error ? err.error : err.toString();
|
||||||
|
Toast.renderToast({ message: `Upload failed: ${msg}`, type: "danger" });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteImage(inventoryId, imageId) {
|
||||||
|
if (!confirm("Are you sure you want to delete this image?")) return;
|
||||||
|
|
||||||
|
fetch(`/api/images/${imageId}`, {
|
||||||
|
method: "DELETE"
|
||||||
|
}).then(response => response.json()).then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
Toast.renderToast({ message: "Image deleted.", type: "success" });
|
||||||
|
location.reload(); // Update view
|
||||||
|
} else {
|
||||||
|
Toast.renderToast({ message: `Failed to delete: ${data.error}`, type: "danger" });
|
||||||
|
}
|
||||||
|
}).catch(err => {
|
||||||
|
Toast.renderToast({ message: `Error deleting image: ${err}`, type: "danger" });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
submitImageUpload,
|
||||||
|
deleteImage
|
||||||
|
}
|
||||||
|
})();
|
|
@ -1,56 +1,3 @@
|
||||||
const ImageWidget = (() => {
|
|
||||||
function submitImageUpload(id) {
|
|
||||||
const form = document.getElementById(`image-upload-form-${id}`);
|
|
||||||
const formData = new FormData(form);
|
|
||||||
|
|
||||||
fetch("/api/images", {
|
|
||||||
method: "POST",
|
|
||||||
body: formData
|
|
||||||
}).then(async response => {
|
|
||||||
if (!response.ok) {
|
|
||||||
// Try to parse JSON, fallback to text
|
|
||||||
const contentType = response.headers.get("Content-Type") || "";
|
|
||||||
let errorDetails;
|
|
||||||
if (contentType.includes("application/json")) {
|
|
||||||
errorDetails = await response.json();
|
|
||||||
} else {
|
|
||||||
errorDetails = { error: await response.text() };
|
|
||||||
}
|
|
||||||
throw errorDetails;
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
}).then(data => {
|
|
||||||
Toast.renderToast({ message: `Image uploaded.`, type: "success" });
|
|
||||||
location.reload();
|
|
||||||
}).catch(err => {
|
|
||||||
const msg = typeof err === "object" && err.error ? err.error : err.toString();
|
|
||||||
Toast.renderToast({ message: `Upload failed: ${msg}`, type: "danger" });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function deleteImage(inventoryId, imageId) {
|
|
||||||
if (!confirm("Are you sure you want to delete this image?")) return;
|
|
||||||
|
|
||||||
fetch(`/api/images/${imageId}`, {
|
|
||||||
method: "DELETE"
|
|
||||||
}).then(response => response.json()).then(data => {
|
|
||||||
if (data.success) {
|
|
||||||
Toast.renderToast({ message: "Image deleted.", type: "success" });
|
|
||||||
location.reload(); // Update view
|
|
||||||
} else {
|
|
||||||
Toast.renderToast({ message: `Failed to delete: ${data.error}`, type: "danger" });
|
|
||||||
}
|
|
||||||
}).catch(err => {
|
|
||||||
Toast.renderToast({ message: `Error deleting image: ${err}`, type: "danger" });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
submitImageUpload,
|
|
||||||
deleteImage
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
const EditorWidget = (() => {
|
const EditorWidget = (() => {
|
||||||
let tempIdCounter = 1;
|
let tempIdCounter = 1;
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
{% if image %}
|
{% if image %}
|
||||||
<img src="{{ url_for('static', filename=image.filename) }}" alt="Image of ID {{ id }}" class="img-thumbnail w-100"
|
<img src="{{ url_for('static', filename=image.filename) }}" alt="Image of ID {{ id }}" class="img-thumbnail w-100"
|
||||||
style="height: auto;" data-bs-toggle="modal" data-bs-target="#imageModal-{{ id }}">
|
style="height: auto;" data-bs-toggle="modal" data-bs-target="#imageModal-{{ id }}">
|
||||||
<div class="modal fade" id="imageModal-{{ id }}" tabindex="-1">
|
<div class="modal fade" id="imageModal-{{ id }}" tabindex="-1" style="z-index: 9999;">
|
||||||
<div class="modal-dialog modal-dialog-centered modal-lg">
|
<div class="modal-dialog modal-dialog-centered modal-lg">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-body text-center">
|
<div class="modal-body text-center">
|
||||||
|
|
|
@ -76,6 +76,7 @@
|
||||||
crossorigin="anonymous"></script>
|
crossorigin="anonymous"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
|
||||||
<script src="{{ url_for('static', filename='js/csv.js') }}"></script>
|
<script src="{{ url_for('static', filename='js/csv.js') }}"></script>
|
||||||
|
<script src="{{ url_for('static', filename='js/image.js') }}"></script>
|
||||||
<script src="{{ url_for('static', filename='js/toast.js') }}" defer></script>
|
<script src="{{ url_for('static', filename='js/toast.js') }}" defer></script>
|
||||||
<script src="{{ url_for('static', filename='js/widget.js') }}"></script>
|
<script src="{{ url_for('static', filename='js/widget.js') }}"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue