Add Products tab with edit, remove, and backup
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -255,6 +255,18 @@ html[lang="fa"] body {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.card-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.form-actions-inline {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
|
||||
@@ -117,51 +117,3 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Inline Product addition
|
||||
function showAddProduct() {
|
||||
document.getElementById('add-product-panel').style.display = 'block';
|
||||
}
|
||||
|
||||
function hideAddProduct() {
|
||||
document.getElementById('add-product-panel').style.display = 'none';
|
||||
}
|
||||
|
||||
async function addProduct() {
|
||||
const code = document.getElementById('new-code').value;
|
||||
const desc = document.getElementById('new-desc').value;
|
||||
const weight = parseFloat(document.getElementById('new-weight').value);
|
||||
|
||||
if (!code || !desc) {
|
||||
showToast('Code and Description are required', true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/products', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ code, description: desc, default_net_weight: weight })
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const p = await res.json();
|
||||
const select = document.getElementById('product');
|
||||
const opt = document.createElement('option');
|
||||
opt.value = p.id;
|
||||
opt.dataset.weight = p.default_net_weight;
|
||||
opt.textContent = `${p.code} — ${p.description}`;
|
||||
select.appendChild(opt);
|
||||
select.value = p.id;
|
||||
document.getElementById('canister-weight').value = p.default_net_weight;
|
||||
updatePreview();
|
||||
|
||||
hideAddProduct();
|
||||
showToast('Product added');
|
||||
} else {
|
||||
showToast('Error adding product', true);
|
||||
}
|
||||
} catch (err) {
|
||||
showToast('Network error', true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,17 @@ const translations = {
|
||||
'en': {
|
||||
'app_title': 'Lithium | Print Label',
|
||||
'data_entry': 'Data Entry',
|
||||
'products': 'Products',
|
||||
'print_queue': 'Print Queue',
|
||||
'calibrate': 'Calibrate',
|
||||
'new_order': 'New Order',
|
||||
'product': 'Product',
|
||||
'add_product': '+ Add New Product',
|
||||
'edit_product': 'Edit Product',
|
||||
'edit': 'Edit',
|
||||
'backup': 'Backup',
|
||||
'restore': 'Restore',
|
||||
'no_products': 'No products yet',
|
||||
'product_code': 'Product Code',
|
||||
'description': 'Description',
|
||||
'default_weight': 'Default Weight',
|
||||
@@ -45,11 +51,17 @@ const translations = {
|
||||
'fa': {
|
||||
'app_title': 'لیتیوم | پرینت لیبل',
|
||||
'data_entry': 'ثبت اطلاعات',
|
||||
'products': 'محصولات',
|
||||
'print_queue': 'صف چاپ',
|
||||
'calibrate': 'کالیبراسیون',
|
||||
'new_order': 'سفارش جدید',
|
||||
'product': 'محصول',
|
||||
'add_product': '+ افزودن محصول جدید',
|
||||
'edit_product': 'ویرایش محصول',
|
||||
'edit': 'ویرایش',
|
||||
'backup': 'پشتیبان',
|
||||
'restore': 'بازیابی',
|
||||
'no_products': 'محصولی ثبت نشده',
|
||||
'product_code': 'کد محصول',
|
||||
'description': 'شرح',
|
||||
'default_weight': 'وزن پیشفرض',
|
||||
|
||||
154
slips/static/slips/js/products.js
Normal file
154
slips/static/slips/js/products.js
Normal file
@@ -0,0 +1,154 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('product-form');
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const id = document.getElementById('edit-id').value;
|
||||
const payload = {
|
||||
code: document.getElementById('prod-code').value.trim(),
|
||||
description: document.getElementById('prod-desc').value.trim(),
|
||||
default_net_weight: parseFloat(document.getElementById('prod-weight').value),
|
||||
};
|
||||
if (!payload.code || !payload.description) {
|
||||
showToast(currentLang === 'fa' ? 'کد و شرح لازم است' : 'Code and Description are required', true);
|
||||
return;
|
||||
}
|
||||
|
||||
const url = id ? `/api/products/${id}` : '/api/products';
|
||||
const method = id ? 'PUT' : 'POST';
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (res.ok) {
|
||||
showToast(id
|
||||
? (currentLang === 'fa' ? 'محصول بهروز شد' : 'Product updated')
|
||||
: (currentLang === 'fa' ? 'محصول افزوده شد' : 'Product added'));
|
||||
setTimeout(() => window.location.reload(), 600);
|
||||
} else {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
showToast(err.detail || (currentLang === 'fa' ? 'خطا در ذخیره' : 'Error saving product'), true);
|
||||
}
|
||||
} catch (err) {
|
||||
showToast('Network error', true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function setFormMode(editing) {
|
||||
const title = document.getElementById('product-form-title');
|
||||
const cancel = document.getElementById('product-cancel-btn');
|
||||
const t = translations[currentLang] || translations.en;
|
||||
title.textContent = editing ? t.edit_product : t.add_product;
|
||||
title.removeAttribute('data-i18n');
|
||||
title.setAttribute('data-i18n', editing ? 'edit_product' : 'add_product');
|
||||
cancel.style.display = editing ? '' : 'none';
|
||||
}
|
||||
|
||||
function editProduct(btn) {
|
||||
document.getElementById('edit-id').value = btn.dataset.id;
|
||||
document.getElementById('prod-code').value = btn.dataset.code;
|
||||
document.getElementById('prod-desc').value = btn.dataset.desc;
|
||||
document.getElementById('prod-weight').value = btn.dataset.weight;
|
||||
setFormMode(true);
|
||||
document.getElementById('prod-code').focus();
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
document.getElementById('edit-id').value = '';
|
||||
document.getElementById('product-form').reset();
|
||||
document.getElementById('prod-weight').value = 5;
|
||||
setFormMode(false);
|
||||
}
|
||||
|
||||
async function deleteProduct(id) {
|
||||
const msg = currentLang === 'fa' ? 'این محصول حذف شود؟' : 'Remove this product?';
|
||||
if (!confirm(msg)) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/products/${id}`, { method: 'DELETE' });
|
||||
if (res.ok) {
|
||||
window.location.reload();
|
||||
} else {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
showToast(
|
||||
err.detail || (currentLang === 'fa' ? 'خطا در حذف' : 'Error removing product'),
|
||||
true
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
showToast('Network error', true);
|
||||
}
|
||||
}
|
||||
|
||||
async function backupProducts() {
|
||||
try {
|
||||
const res = await fetch('/api/products');
|
||||
if (!res.ok) {
|
||||
showToast(currentLang === 'fa' ? 'خطا در پشتیبانگیری' : 'Backup failed', true);
|
||||
return;
|
||||
}
|
||||
const products = await res.json();
|
||||
const payload = products.map((p) => ({
|
||||
code: p.code,
|
||||
description: p.description,
|
||||
default_net_weight: p.default_net_weight,
|
||||
}));
|
||||
const blob = new Blob([JSON.stringify(payload, null, 2) + '\n'], { type: 'application/json' });
|
||||
const a = document.createElement('a');
|
||||
const d = new Date();
|
||||
const stamp = [
|
||||
d.getFullYear(),
|
||||
String(d.getMonth() + 1).padStart(2, '0'),
|
||||
String(d.getDate()).padStart(2, '0'),
|
||||
].join('');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = `products-backup-${stamp}.json`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(a.href);
|
||||
showToast(currentLang === 'fa' ? 'فایل پشتیبان دانلود شد' : 'Backup downloaded');
|
||||
} catch (err) {
|
||||
showToast('Network error', true);
|
||||
}
|
||||
}
|
||||
|
||||
function parseProductsBackup(raw) {
|
||||
const data = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
||||
if (!Array.isArray(data) || !data.length) throw new Error('empty or invalid file');
|
||||
return data.map((item) => {
|
||||
if (!item || typeof item !== 'object') throw new Error('invalid item');
|
||||
const code = String(item.code || '').trim();
|
||||
const description = String(item.description || '').trim();
|
||||
const weight = Number(item.default_net_weight);
|
||||
if (!code || !description || !Number.isFinite(weight)) throw new Error('invalid product: ' + code);
|
||||
return { code, description, default_net_weight: weight };
|
||||
});
|
||||
}
|
||||
|
||||
async function restoreProducts(input) {
|
||||
const file = input.files && input.files[0];
|
||||
input.value = '';
|
||||
if (!file) return;
|
||||
try {
|
||||
const payload = parseProductsBackup(await file.text());
|
||||
const msg = currentLang === 'fa'
|
||||
? 'محصولات از این فایل بازیابی شوند؟ (بر اساس کد ادغام میشود)'
|
||||
: 'Restore products from this file? (merged by code)';
|
||||
if (!confirm(msg)) return;
|
||||
const res = await fetch('/api/products/restore', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
showToast(err.detail || (currentLang === 'fa' ? 'خطا در بازیابی' : 'Restore failed'), true);
|
||||
return;
|
||||
}
|
||||
showToast(currentLang === 'fa' ? 'بازیابی شد' : 'Restored from file');
|
||||
setTimeout(() => window.location.reload(), 600);
|
||||
} catch (err) {
|
||||
showToast((currentLang === 'fa' ? 'خطا در بازیابی: ' : 'Restore failed: ') + err.message, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user