This commit is contained in:
2026-01-03 18:22:36 +03:00
parent 25a92e4b98
commit cd1f3a1e87

View File

@@ -310,7 +310,9 @@ function resolveApiUrl(url) {
async function fetchJson(url, options) { async function fetchJson(url, options) {
const response = await fetch(resolveApiUrl(url), options); const response = await fetch(resolveApiUrl(url), options);
if (!response.ok) { if (!response.ok) {
const payload = await response.json().catch(() => ({ message: 'Ошибка запроса.' })); const payload = await response.json().catch(() => ({
message: 'Ошибка запроса.'
}));
throw new Error(payload.message || 'Ошибка запроса.'); throw new Error(payload.message || 'Ошибка запроса.');
} }
if (response.status === 204) { if (response.status === 204) {
@@ -433,7 +435,9 @@ function initializeApp() {
try { try {
await fetchJson('/api/orders', { await fetchJson('/api/orders', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ body: JSON.stringify({
customerName: customerNameInput.value.trim(), customerName: customerNameInput.value.trim(),
orderDate: orderDateInput.value orderDate: orderDateInput.value
@@ -449,7 +453,9 @@ function initializeApp() {
advanceButton.addEventListener('click', async () => { advanceButton.addEventListener('click', async () => {
try { try {
await fetchJson('/api/current-date/advance', { method: 'POST' }); await fetchJson('/api/current-date/advance', {
method: 'POST'
});
await refreshData(); await refreshData();
showNotification('Дата переведена. Заказы за сегодня отправлены.', 'success'); showNotification('Дата переведена. Заказы за сегодня отправлены.', 'success');
} catch (error) { } catch (error) {
@@ -467,20 +473,23 @@ function initializeApp() {
try { try {
const response = await fetchJson(`/api/orders/${orderId}/items`, { const response = await fetchJson(`/api/orders/${orderId}/items`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
body: JSON.stringify({ productId, quantity }) 'Content-Type': 'application/json'
},
body: JSON.stringify({
productId,
quantity
})
}); });
const order = orders.find((entry) => entry.id === orderId); const order = orders.find((entry) => entry.id === orderId);
const product = products.find((entry) => entry.id === productId); const product = products.find((entry) => entry.id === productId);
if (order) { if (order) {
const newItem = normalizeOrderItems([ const newItem = normalizeOrderItems([{
{
id: response?.itemId, id: response?.itemId,
product_id: productId, product_id: productId,
product_name: product?.name, product_name: product?.name,
quantity quantity
} }])[0];
])[0];
order.items = [...order.items, newItem]; order.items = [...order.items, newItem];
renderOrders(); renderOrders();
} }
@@ -493,11 +502,18 @@ function initializeApp() {
}); });
ordersContainer.addEventListener('click', async (event) => { ordersContainer.addEventListener('click', async (event) => {
const { action, id, itemId, orderId } = event.target.dataset; const {
action,
id,
itemId,
orderId
} = event.target.dataset;
try { try {
if (action === 'delete-order') { if (action === 'delete-order') {
await fetchJson(`/api/orders/${id}`, { method: 'DELETE' }); await fetchJson(`/api/orders/${id}`, {
method: 'DELETE'
});
await refreshData(); await refreshData();
return; return;
} }
@@ -514,8 +530,13 @@ function initializeApp() {
Date Date
await fetchJson(`/api/orders/${id}`, { await fetchJson(`/api/orders/${id}`, {
method: 'PUT', method: 'PUT',
headers: { 'Content-Type': 'application/json' }, headers: {
body: JSON.stringify({ customerName: newName, orderDate: newDate }) 'Content-Type': 'application/json'
},
body: JSON.stringify({
customerName: newName,
orderDate: newDate
})
}); });
await refreshData(); await refreshData();
return; return;
@@ -531,7 +552,9 @@ function initializeApp() {
await fetchJson(`/api/orders/${orderId}/items/${itemId}`, { await fetchJson(`/api/orders/${orderId}/items/${itemId}`, {
method: 'PUT', method: 'PUT',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ body: JSON.stringify({
productId: productSelect.value, productId: productSelect.value,
quantity: Number.parseInt(quantityInput.value, 10) quantity: Number.parseInt(quantityInput.value, 10)
@@ -542,7 +565,9 @@ function initializeApp() {
} }
if (action === 'delete-item') { if (action === 'delete-item') {
await fetchJson(`/api/orders/${orderId}/items/${itemId}`, { method: 'DELETE' }); await fetchJson(`/api/orders/${orderId}/items/${itemId}`, {
method: 'DELETE'
});
await refreshData(); await refreshData();
return; return;
} }
@@ -557,8 +582,12 @@ function initializeApp() {
} }
await fetchJson(`/api/order-items/${itemId}/move`, { await fetchJson(`/api/order-items/${itemId}/move`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
body: JSON.stringify({ targetOrderId: targetSelect.value }) 'Content-Type': 'application/json'
},
body: JSON.stringify({
targetOrderId: targetSelect.value
})
}); });
await refreshData(); await refreshData();
return; return;