Delete product button on expanded card, deleteProduct DB function

- "Уд." button (red) next to "Ред." on product card
- deleteProduct: nullifies food_log refs, zeros recipe_ingredients refs, deletes product
- List refreshes after deletion
This commit is contained in:
4aiman 2026-05-30 21:08:32 +03:00
commit bcb4124be0
2 changed files with 17 additions and 0 deletions

View file

@ -162,6 +162,13 @@ async function addProduct(p) {
return rows[0]?.id;
}
async function deleteProduct(id) {
// Reassign food_log and recipe_ingredients to avoid orphaned references
await execute("UPDATE food_log SET product_id = NULL WHERE product_id = ?1", [id]);
await execute("UPDATE recipe_ingredients SET product_id = 0 WHERE product_id = ?1", [id]);
await execute("DELETE FROM products WHERE id = ?1", [id]);
}
// ---------------------------------------------------------------------------
// Cooking methods & Recipes
// ---------------------------------------------------------------------------
@ -898,6 +905,7 @@ window.DB.saveSleep = saveSleep;
window.DB.searchProducts = searchProducts;
window.DB.getProduct = getProduct;
window.DB.addProduct = addProduct;
window.DB.deleteProduct = deleteProduct;
window.DB.getFoodForDate = getFoodForDate;
window.DB.addFoodManual = addFoodManual;
window.DB.addFoodEntry = addFoodEntry;

View file

@ -1295,6 +1295,7 @@ async function renderProducts(container) {
<div style="display:flex;gap:8px;margin-top:8px">
<button class="btn-primary prod-add-btn" data-pid="${p.id}" style="flex:1">Добавить в дневник</button>
<button class="btn-secondary prod-edit-btn" data-pid="${p.id}" style="flex:0">Ред.</button>
<button class="btn-secondary prod-del-btn" data-pid="${p.id}" style="flex:0;color:#cf6679">Уд.</button>
</div>
</div>`;
}
@ -1321,6 +1322,14 @@ async function renderProducts(container) {
if (p) showEditProductModal(p, () => doSearch(false));
};
});
el.querySelectorAll(".prod-del-btn").forEach(btn => {
btn.onclick = async e => {
e.stopPropagation();
const pid = parseInt(btn.dataset.pid);
await DB.deleteProduct(pid);
doSearch(false);
};
});
}
function showAddFoodFromProduct(productId) {