Ultronprime commited on
Commit
f080a44
·
verified ·
1 Parent(s): 5ec8a84

Update events.js

Browse files
Files changed (1) hide show
  1. events.js +7 -9
events.js CHANGED
@@ -1,6 +1,6 @@
1
  // events.js - Manages all event listeners
2
 
3
- import { appState, saveState, loadState } from './state.js';
4
  import { handleUpdateStock, handleRestock } from './services.js';
5
  import { refreshUI, showToast, renderReport } from './ui.js';
6
 
@@ -12,12 +12,12 @@ export function attachAllListeners() {
12
  }
13
 
14
  function attachProductInputListeners() {
15
- document.querySelectorAll('.product-card .update-btn').forEach(btn => {
16
  if (btn.dataset.listenerAttached) return;
17
  btn.dataset.listenerAttached = true;
18
  btn.addEventListener('click', (e) => {
19
- const card = e.target.closest('.product-card');
20
- const productName = card.dataset.productName;
21
  const quantity = parseInt(card.querySelector('input').value, 10);
22
  handleUpdateStock(productName, quantity);
23
  });
@@ -30,12 +30,12 @@ function attachMaxStockEditListeners() {
30
  icon.dataset.listenerAttached = true;
31
  icon.addEventListener('click', (e) => {
32
  const valueSpan = e.target.closest('.flex').querySelector('.max-stock-value');
33
- const materialName = e.target.closest('.material-card').dataset.materialName;
34
  const material = appState.materials.find(m => m.name === materialName);
35
  const input = document.createElement('input');
36
  input.type = 'number';
37
  input.value = material.maxStock;
38
- input.className = 'w-20 text-right font-semibold border rounded px-1';
39
  valueSpan.replaceWith(input);
40
  input.focus();
41
  input.select();
@@ -65,7 +65,7 @@ function attachRestockListeners() {
65
  if (formContainer.classList.contains('hidden')) {
66
  document.querySelectorAll('.restock-form').forEach(f => { f.classList.add('hidden'); f.innerHTML = ''; });
67
  formContainer.classList.remove('hidden');
68
- formContainer.innerHTML = `<div class="flex items-center gap-2"><input type="number" placeholder="Qty" class="input-number w-20 px-2 py-1 border border-gray-300 rounded"><button class="confirm-restock-btn px-3 py-1 bg-green-500 text-white text-xs rounded hover:bg-green-600">Add</button></div>`;
69
  const input = formContainer.querySelector('input');
70
  input.focus();
71
  formContainer.querySelector('.confirm-restock-btn').addEventListener('click', () => { handleRestock(materialName, parseInt(input.value, 10)); });
@@ -86,7 +86,6 @@ function attachModalListeners() {
86
  const resetModal = document.getElementById('reset-modal');
87
  const reportsModal = document.getElementById('reports-modal');
88
 
89
- // Reset Modal
90
  const showResetModalBtn = document.getElementById('show-reset-modal-btn');
91
  if (!showResetModalBtn.dataset.listenerAttachedForReset) {
92
  showResetModalBtn.addEventListener('click', () => resetModal.classList.remove('hidden'));
@@ -102,7 +101,6 @@ function attachModalListeners() {
102
  showResetModalBtn.dataset.listenerAttachedForReset = true;
103
  }
104
 
105
- // Reports Modal
106
  const showReportsModalBtn = document.getElementById('show-reports-modal-btn');
107
  if (!showReportsModalBtn.dataset.listenerAttachedForReports) {
108
  showReportsModalBtn.addEventListener('click', () => reportsModal.classList.remove('hidden'));
 
1
  // events.js - Manages all event listeners
2
 
3
+ import { appState, saveState } from './state.js';
4
  import { handleUpdateStock, handleRestock } from './services.js';
5
  import { refreshUI, showToast, renderReport } from './ui.js';
6
 
 
12
  }
13
 
14
  function attachProductInputListeners() {
15
+ document.querySelectorAll('.update-btn').forEach(btn => {
16
  if (btn.dataset.listenerAttached) return;
17
  btn.dataset.listenerAttached = true;
18
  btn.addEventListener('click', (e) => {
19
+ const card = e.target.closest('.dashboard-card');
20
+ const productName = Object.keys(appState.productRecipes).find(name => card.innerHTML.includes(name));
21
  const quantity = parseInt(card.querySelector('input').value, 10);
22
  handleUpdateStock(productName, quantity);
23
  });
 
30
  icon.dataset.listenerAttached = true;
31
  icon.addEventListener('click', (e) => {
32
  const valueSpan = e.target.closest('.flex').querySelector('.max-stock-value');
33
+ const materialName = e.target.closest('.dashboard-card').dataset.materialName;
34
  const material = appState.materials.find(m => m.name === materialName);
35
  const input = document.createElement('input');
36
  input.type = 'number';
37
  input.value = material.maxStock;
38
+ input.className = 'input-field w-20 text-right font-semibold';
39
  valueSpan.replaceWith(input);
40
  input.focus();
41
  input.select();
 
65
  if (formContainer.classList.contains('hidden')) {
66
  document.querySelectorAll('.restock-form').forEach(f => { f.classList.add('hidden'); f.innerHTML = ''; });
67
  formContainer.classList.remove('hidden');
68
+ formContainer.innerHTML = `<div class="flex items-center gap-2"><input type="number" placeholder="Qty" class="input-field w-20"><button class="btn btn-primary text-xs confirm-restock-btn">Add</button></div>`;
69
  const input = formContainer.querySelector('input');
70
  input.focus();
71
  formContainer.querySelector('.confirm-restock-btn').addEventListener('click', () => { handleRestock(materialName, parseInt(input.value, 10)); });
 
86
  const resetModal = document.getElementById('reset-modal');
87
  const reportsModal = document.getElementById('reports-modal');
88
 
 
89
  const showResetModalBtn = document.getElementById('show-reset-modal-btn');
90
  if (!showResetModalBtn.dataset.listenerAttachedForReset) {
91
  showResetModalBtn.addEventListener('click', () => resetModal.classList.remove('hidden'));
 
101
  showResetModalBtn.dataset.listenerAttachedForReset = true;
102
  }
103
 
 
104
  const showReportsModalBtn = document.getElementById('show-reports-modal-btn');
105
  if (!showReportsModalBtn.dataset.listenerAttachedForReports) {
106
  showReportsModalBtn.addEventListener('click', () => reportsModal.classList.remove('hidden'));