assetflow/static/js/editItems.js

89 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-06-06 23:49:16 +02:00
function primeCreateNew() {
// Clear the form
$('.form-control').val('');
const form = document.getElementById('ItemModalForm');
document.getElementById('itemModifyModalLabel').innerText= "Create a new item";
form.setAttribute('method', 'POST');
return true;
}
function primeEdit() {
const form = document.getElementById('ItemModalForm');
document.getElementById('itemModifyModalLabel').innerText = 'Edit an item';
form.setAttribute('method', 'PATCH');
return true;
}
function getDataForEdit(id) {
$.ajax({
type: 'get',
url: `/api/v1/items?id=${id}`,
success: function (result) {
2023-06-06 23:49:16 +02:00
// Get elements inside the editCategoryModal
const modal_itemName = document.getElementById('itemModifyModalName');
const modal_itemComment = document.getElementById('itemModifyModalComment');
const modal_itemAmount = document.getElementById('itemModifyModalAmount');
const modal_itemSKU = document.getElementById('itemModifyModalSKU');
const modal_itemStorageLocation = document.getElementById('itemModifyModalStorageLocation');
const modal_itemManufacturer = document.getElementById('itemModifyModalManuf');
const modal_itemCategory = document.getElementById('itemModifyModalCategory');
const modal_itemStatus = document.getElementById('itemModifyModalStatus');
const modal_itemid = document.getElementById('itemModifyModalId');
2023-06-27 23:47:00 +02:00
const modal_userinfo = document.getElementById('itemModifyModalContact');
2023-06-06 23:49:16 +02:00
modal_itemName.value = result.name;
modal_itemComment.value = result.comment;
modal_itemAmount.value = result.amount;
modal_itemSKU.value = result.SKU;
modal_itemManufacturer.value = result.manufacturer;
// Select the correct option in the dropdown
const modal_itemCategoryOptions = modal_itemCategory.options;
2023-07-09 18:20:27 +02:00
modal_itemCategoryOptions[0].selected = true;
2023-06-06 23:49:16 +02:00
for (let i = 0; i < modal_itemCategoryOptions.length; i++) {
2023-06-27 23:47:00 +02:00
if (modal_itemCategoryOptions[i].value == result.categoryId) {
2023-06-06 23:49:16 +02:00
modal_itemCategoryOptions[i].selected = true;
}
}
// Select the correct option in the dropdown
const modal_itemStatusOptions = modal_itemStatus.options;
2023-07-09 18:20:27 +02:00
modal_itemStatusOptions[0].selected = true;
2023-06-06 23:49:16 +02:00
for (let i = 0; i < modal_itemStatusOptions.length; i++) {
2023-06-27 23:47:00 +02:00
if (modal_itemStatusOptions[i].value == result.statusId) {
2023-06-06 23:49:16 +02:00
modal_itemStatusOptions[i].selected = true;
}
}
// Select the correct option in the dropdown
const modal_itemStorageLocationOptions = modal_itemStorageLocation.options;
2023-07-09 18:20:27 +02:00
modal_itemStorageLocationOptions[0].selected = true;
2023-06-06 23:49:16 +02:00
for (let i = 0; i < modal_itemStorageLocationOptions.length; i++) {
2023-06-27 23:47:00 +02:00
if (modal_itemStorageLocationOptions[i].value == result.storageLocationId) {
2023-06-06 23:49:16 +02:00
modal_itemStorageLocationOptions[i].selected = true;
}
}
2023-06-27 23:47:00 +02:00
modal_userinfo.selectedIndex = 0;
// Select the correct option in the dropdown
const modal_userInfoOptions = modal_userinfo.options;
for (let i = 0; i < modal_userInfoOptions.length; i++) {
if (modal_userInfoOptions[i].value == result.contactInfoId) {
modal_userInfoOptions[i].selected = true;
}
}
2023-06-06 23:49:16 +02:00
modal_itemid.value = result.id;
},
error: function (data) {
console.log('!!!! ERROR !!!!', data);
// Hide overlay with spinner
$('.loader-overlay').removeClass('active');
// Close the modal
$('.modal').modal('hide');
createNewToast('<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The category does no longer exist.', "text-bg-danger")
}
});
}