Fix button logic
This commit is contained in:
@@ -121,7 +121,7 @@ class OrderCard {
|
||||
idCell.textContent = item.product_id ?? item.id;
|
||||
|
||||
const productCell = document.createElement('td');
|
||||
const productSelect = this.buildProductSelect(item);
|
||||
const productSelect = this.buildProductSelect(item.product_id);
|
||||
productSelect.classList.add('form-select-sm');
|
||||
productSelect.dataset.action = 'item-product';
|
||||
productSelect.dataset.itemId = item.id;
|
||||
@@ -220,7 +220,12 @@ class OrderCard {
|
||||
const productLabel = document.createElement('label');
|
||||
productLabel.className = 'form-label';
|
||||
productLabel.textContent = 'Товар';
|
||||
const productSelect = this.buildProductSelect();
|
||||
const usedProductIds = new Set(
|
||||
(this.order.items || [])
|
||||
.map((item) => item.product_id)
|
||||
.filter((value) => value !== null && value !== undefined)
|
||||
);
|
||||
const productSelect = this.buildProductSelect(undefined, usedProductIds);
|
||||
const productSelectId = `add-item-product-${this.order.id}`;
|
||||
productSelect.name = 'productId';
|
||||
productSelect.id = productSelectId;
|
||||
@@ -253,6 +258,16 @@ class OrderCard {
|
||||
addButton.textContent = 'Добавить позицию';
|
||||
actionCol.appendChild(addButton);
|
||||
|
||||
if (productSelect.options.length === 0) {
|
||||
const placeholder = document.createElement('option');
|
||||
placeholder.value = '';
|
||||
placeholder.textContent = 'Нет доступных позиций';
|
||||
productSelect.appendChild(placeholder);
|
||||
productSelect.disabled = true;
|
||||
quantityInput.disabled = true;
|
||||
addButton.disabled = true;
|
||||
}
|
||||
|
||||
form.appendChild(productCol);
|
||||
form.appendChild(quantityCol);
|
||||
form.appendChild(actionCol);
|
||||
@@ -260,10 +275,13 @@ class OrderCard {
|
||||
return form;
|
||||
}
|
||||
|
||||
buildProductSelect(selectedId) {
|
||||
buildProductSelect(selectedId, excludedIds) {
|
||||
const select = document.createElement('select');
|
||||
select.className = 'form-select';
|
||||
this.products.forEach((product) => {
|
||||
if (excludedIds && excludedIds.has(product.id)) {
|
||||
return;
|
||||
}
|
||||
const option = document.createElement('option');
|
||||
option.value = product.id;
|
||||
option.textContent = product.name;
|
||||
@@ -313,7 +331,7 @@ async function fetchJson(url, options) {
|
||||
const payload = await response.json().catch(() => ({
|
||||
message: 'Ошибка запроса.'
|
||||
}));
|
||||
throw new Error(payload.message || 'Ошибка запроса.');
|
||||
throw new Error(payload.error || payload.message || 'Error');
|
||||
}
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
@@ -549,18 +567,24 @@ function initializeApp() {
|
||||
`[data-action="item-product"][data-item-id="${itemId}"]`
|
||||
);
|
||||
|
||||
await fetchJson(`/api/orders/${orderId}/items/${itemId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
productId: productSelect.value,
|
||||
quantity: Number.parseInt(quantityInput.value, 10)
|
||||
})
|
||||
});
|
||||
await refreshData();
|
||||
return;
|
||||
try {
|
||||
await fetchJson(`/api/orders/${orderId}/items/${itemId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
productId: productSelect.value,
|
||||
quantity: Number.parseInt(quantityInput.value, 10)
|
||||
})
|
||||
});
|
||||
await refreshData();
|
||||
return;
|
||||
} catch (error) {
|
||||
showNotification(error.message, 'warning');
|
||||
await refreshData();
|
||||
return;
|
||||
}
|
||||
}
|
||||
case 'delete-item': {
|
||||
await fetchJson(`/api/orders/${orderId}/items/${itemId}`, {
|
||||
|
||||
Reference in New Issue
Block a user