53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
// js/controller.js
|
|
|
|
import Model from './model.js';
|
|
import View from './view.js';
|
|
|
|
export default {
|
|
async init() {
|
|
this.products = await Model.getProducts();
|
|
this.categories = await Model.getCategories();
|
|
this.brands = await Model.getBrands();
|
|
|
|
View.renderProductList(this.products, this.handleEdit.bind(this), this.handleDelete.bind(this));
|
|
|
|
// Кнопка "Добавить товар"
|
|
document.getElementById('addProductBtn').onclick = () => {
|
|
View.showProductModal(
|
|
{ categories: this.categories, brands: this.brands },
|
|
this.handleAdd.bind(this)
|
|
);
|
|
};
|
|
},
|
|
|
|
async handleAdd(productData, modal) {
|
|
await Model.addProduct(productData);
|
|
modal.hide();
|
|
await this.refresh();
|
|
},
|
|
|
|
async handleEdit(productId) {
|
|
const product = await Model.getProductById(productId);
|
|
View.showProductModal(
|
|
{ product, categories: this.categories, brands: this.brands },
|
|
async (formData, modal) => {
|
|
await Model.updateProduct(productId, formData);
|
|
modal.hide();
|
|
await this.refresh();
|
|
}
|
|
);
|
|
},
|
|
|
|
async handleDelete(productId) {
|
|
if (confirm('Удалить товар?')) {
|
|
await Model.deleteProduct(productId);
|
|
await this.refresh();
|
|
}
|
|
},
|
|
|
|
async refresh() {
|
|
this.products = await Model.getProducts();
|
|
View.renderProductList(this.products, this.handleEdit.bind(this), this.handleDelete.bind(this));
|
|
}
|
|
};
|