mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #104 from LD-Reborn/100-feature-add-the-ability-to-create-an-asset
Removed stray comments from Assets.cshtml, Fixed success toast not sh…
This commit is contained in:
@@ -80,6 +80,32 @@
|
|||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
function createToastContainer() {
|
||||||
|
const container = document.createElement('div');
|
||||||
|
container.id = 'toastContainer';
|
||||||
|
container.className = 'toast-container position-fixed bottom-0 end-0 p-3';
|
||||||
|
document.body.appendChild(container);
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple toast helper
|
||||||
|
function showToast(message, type) {
|
||||||
|
const toastContainer = document.getElementById('toastContainer') || createToastContainer();
|
||||||
|
const toast = document.createElement('div');
|
||||||
|
toast.className = `toast align-items-center text-white bg-${type} border-0`;
|
||||||
|
toast.role = 'alert';
|
||||||
|
toast.innerHTML = `
|
||||||
|
<div class="d-flex">
|
||||||
|
<div class="toast-body">${message}</div>
|
||||||
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
toastContainer.appendChild(toast);
|
||||||
|
const bsToast = new bootstrap.Toast(toast, { delay: 3000 });
|
||||||
|
bsToast.show();
|
||||||
|
toast.addEventListener('hidden.bs.toast', () => toast.remove());
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const deleteModal = document.getElementById('deleteModal');
|
const deleteModal = document.getElementById('deleteModal');
|
||||||
let currentButton = null; // The delete button that opened the modal
|
let currentButton = null; // The delete button that opened the modal
|
||||||
@@ -128,39 +154,13 @@
|
|||||||
|
|
||||||
showToast('Asset deleted successfully', 'success');
|
showToast('Asset deleted successfully', 'success');
|
||||||
} else {
|
} else {
|
||||||
showToast(`❌ ${result.reason}: ${result.exception || 'Unknown error'}`, 'danger');
|
showToast(`${result.reason}: ${result.exception || 'Unknown error'}`, 'danger');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
showToast('Error contacting server', 'danger');
|
showToast('Error contacting server', 'danger');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Simple toast helper
|
|
||||||
function showToast(message, type) {
|
|
||||||
const toastContainer = document.getElementById('toastContainer') || createToastContainer();
|
|
||||||
const toast = document.createElement('div');
|
|
||||||
toast.className = `toast align-items-center text-white bg-${type} border-0`;
|
|
||||||
toast.role = 'alert';
|
|
||||||
toast.innerHTML = `
|
|
||||||
<div class="d-flex">
|
|
||||||
<div class="toast-body">${message}</div>
|
|
||||||
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
toastContainer.appendChild(toast);
|
|
||||||
const bsToast = new bootstrap.Toast(toast, { delay: 3000 });
|
|
||||||
bsToast.show();
|
|
||||||
toast.addEventListener('hidden.bs.toast', () => toast.remove());
|
|
||||||
}
|
|
||||||
|
|
||||||
function createToastContainer() {
|
|
||||||
const container = document.createElement('div');
|
|
||||||
container.id = 'toastContainer';
|
|
||||||
container.className = 'toast-container position-fixed bottom-0 end-0 p-3';
|
|
||||||
document.body.appendChild(container);
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -222,13 +222,13 @@
|
|||||||
<div class="col-12 mt-3">
|
<div class="col-12 mt-3">
|
||||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||||
<h6 class="fw-bold mb-0">@T["Attributes"]</h6>
|
<h6 class="fw-bold mb-0">@T["Attributes"]</h6>
|
||||||
<button type="button" class="btn btn-sm btn-outline-success" id="addAttributeBtn">
|
|
||||||
➕ @T["Add Attribute"]
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
<div id="attributesContainer" class="d-flex flex-column gap-2">
|
<div id="attributesContainer" class="d-flex flex-column gap-2">
|
||||||
<!-- Dynamic attribute rows will appear here -->
|
<!-- Dynamic attribute rows will appear here -->
|
||||||
</div>
|
</div>
|
||||||
|
<button type="button" class="btn btn-sm btn-outline-success mt-3" id="addAttributeBtn">
|
||||||
|
➕ @T["Add Attribute"]
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr class="my-3" />
|
<hr class="my-3" />
|
||||||
@@ -269,7 +269,6 @@
|
|||||||
const attributesContainer = document.getElementById('attributesContainer');
|
const attributesContainer = document.getElementById('attributesContainer');
|
||||||
const addAttributeBtn = document.getElementById('addAttributeBtn');
|
const addAttributeBtn = document.getElementById('addAttributeBtn');
|
||||||
|
|
||||||
// Add a new attribute row
|
|
||||||
addAttributeBtn.addEventListener('click', () => {
|
addAttributeBtn.addEventListener('click', () => {
|
||||||
const row = document.createElement('div');
|
const row = document.createElement('div');
|
||||||
row.className = 'd-flex gap-2 align-items-center attribute-row';
|
row.className = 'd-flex gap-2 align-items-center attribute-row';
|
||||||
@@ -281,18 +280,15 @@
|
|||||||
attributesContainer.appendChild(row);
|
attributesContainer.appendChild(row);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delegate delete button click
|
|
||||||
attributesContainer.addEventListener('click', (e) => {
|
attributesContainer.addEventListener('click', (e) => {
|
||||||
if (e.target.classList.contains('btn-remove-attribute')) {
|
if (e.target.classList.contains('btn-remove-attribute')) {
|
||||||
e.target.closest('.attribute-row').remove();
|
e.target.closest('.attribute-row').remove();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 🔧 Hook into the existing submit handler to include attributes
|
|
||||||
const createForm = document.getElementById('createAssetForm');
|
const createForm = document.getElementById('createAssetForm');
|
||||||
const originalHandler = createForm.onsubmit;
|
const originalHandler = createForm.onsubmit;
|
||||||
createForm.addEventListener('submit', (e) => {
|
createForm.addEventListener('submit', (e) => {
|
||||||
// Let’s patch the JSON generation just before sending
|
|
||||||
const formData = new FormData(createForm);
|
const formData = new FormData(createForm);
|
||||||
const jsonData = {};
|
const jsonData = {};
|
||||||
|
|
||||||
@@ -307,7 +303,6 @@
|
|||||||
target[keys[keys.length - 1]] = value;
|
target[keys[keys.length - 1]] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add Attributes as Dictionary<string,string>
|
|
||||||
const attributes = {};
|
const attributes = {};
|
||||||
document.querySelectorAll('#attributesContainer .attribute-row').forEach(row => {
|
document.querySelectorAll('#attributesContainer .attribute-row').forEach(row => {
|
||||||
const name = row.querySelector('[data-attr-name]').value.trim();
|
const name = row.querySelector('[data-attr-name]').value.trim();
|
||||||
@@ -320,8 +315,7 @@
|
|||||||
jsonData.Description.Attributes = attributes;
|
jsonData.Description.Attributes = attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the body before sending
|
e.preventDefault();
|
||||||
e.preventDefault(); // stop default form submit
|
|
||||||
|
|
||||||
fetch('/Assets/Create', {
|
fetch('/Assets/Create', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -338,9 +332,9 @@
|
|||||||
bootstrap.Modal.getInstance(createModalEl).hide();
|
bootstrap.Modal.getInstance(createModalEl).hide();
|
||||||
createForm.reset();
|
createForm.reset();
|
||||||
attributesContainer.innerHTML = '';
|
attributesContainer.innerHTML = '';
|
||||||
showToast('✅ Asset created successfully', 'success');
|
showToast('Asset created successfully', 'success');
|
||||||
} else {
|
} else {
|
||||||
showToast(`❌ ${result.reason || 'Error creating asset'}`, 'danger');
|
showToast(`${result.reason || 'Error creating asset'}`, 'danger');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|||||||
Reference in New Issue
Block a user