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