Spaces:
Running
Running
Update events.js
Browse files
events.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
// events.js - Manages all event listeners
|
2 |
-
|
3 |
-
import {
|
4 |
-
import { handleUpdateStock, handleRestock } from './services.js';
|
5 |
import { refreshUI, showToast, renderReport, renderCustomPOModal } from './ui.js';
|
6 |
import { generatePurchaseOrder } from './purchaseOrderService.js';
|
|
|
7 |
|
8 |
export function attachAllListeners() {
|
9 |
attachProductInputListeners();
|
@@ -13,11 +13,10 @@ export function attachAllListeners() {
|
|
13 |
}
|
14 |
|
15 |
export function attachOneTimeListeners() {
|
|
|
16 |
attachModalListeners();
|
17 |
}
|
18 |
|
19 |
-
// ... (keep attachProductInputListeners, attachCurrentStockEditListeners, attachRestockListeners) ...
|
20 |
-
|
21 |
function attachProductInputListeners() {
|
22 |
document.querySelectorAll('.update-btn').forEach(btn => {
|
23 |
if (btn.dataset.listenerAttached) return;
|
@@ -39,11 +38,10 @@ function attachCurrentStockEditListeners() {
|
|
39 |
const card = e.target.closest('.dashboard-card');
|
40 |
const valueDiv = card.querySelector('.current-stock-value');
|
41 |
const materialName = card.dataset.materialName;
|
42 |
-
const material = appState.materials.find(m => m.name === materialName);
|
43 |
|
44 |
const input = document.createElement('input');
|
45 |
input.type = 'number';
|
46 |
-
input.value =
|
47 |
input.className = 'input-field w-24 text-2xl font-bold';
|
48 |
|
49 |
valueDiv.replaceWith(input);
|
@@ -52,14 +50,7 @@ function attachCurrentStockEditListeners() {
|
|
52 |
|
53 |
const saveChange = () => {
|
54 |
const newValue = parseInt(input.value, 10);
|
55 |
-
|
56 |
-
material.currentStock = newValue;
|
57 |
-
saveState();
|
58 |
-
refreshUI();
|
59 |
-
} else {
|
60 |
-
showToast('Stock must be a valid positive number.', 'error');
|
61 |
-
refreshUI();
|
62 |
-
}
|
63 |
};
|
64 |
|
65 |
input.addEventListener('blur', saveChange);
|
@@ -94,38 +85,72 @@ function attachRestockListeners() {
|
|
94 |
}
|
95 |
|
96 |
function attachPurchaseOrderListener() {
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
106 |
}
|
107 |
});
|
108 |
}
|
109 |
}
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
function attachModalListeners() {
|
112 |
-
// Standard Modals (Reset, Reports)
|
113 |
const resetModal = document.getElementById('reset-modal');
|
114 |
const reportsModal = document.getElementById('reports-modal');
|
|
|
|
|
|
|
115 |
|
116 |
-
|
117 |
-
resetModal.addEventListener('click', (e) => {
|
118 |
const target = e.target.closest('button');
|
119 |
if ((target && target.id === 'cancel-reset-btn') || e.target === resetModal) {
|
120 |
resetModal.classList.add('hidden');
|
121 |
}
|
122 |
if (target && target.id === 'confirm-reset-btn') {
|
123 |
-
|
|
|
|
|
124 |
window.location.reload();
|
125 |
}
|
126 |
});
|
127 |
|
128 |
-
document.getElementById('show-reports-modal-btn')
|
129 |
reportsModal.addEventListener('click', (e) => {
|
130 |
const target = e.target.closest('button');
|
131 |
if ((target && target.id === 'close-reports-modal-btn') || e.target === reportsModal) {
|
@@ -134,17 +159,13 @@ function attachModalListeners() {
|
|
134 |
if (target && target.id === 'report-prod-summary') renderReport('production');
|
135 |
if (target && target.id === 'report-mat-usage') renderReport('material');
|
136 |
});
|
137 |
-
|
138 |
-
// Custom PO Modal
|
139 |
-
const customPOModal = document.getElementById('custom-po-modal');
|
140 |
customPOModal.addEventListener('click', e => {
|
141 |
const target = e.target;
|
142 |
-
|
143 |
-
if (target.id === 'custom-po-modal' || target.id === 'cancel-po-btn' || target.id === 'cancel-po-btn-footer' || target.closest('#cancel-po-btn')) {
|
144 |
customPOModal.classList.add('hidden');
|
145 |
}
|
146 |
|
147 |
-
// Generate PDF action
|
148 |
if (target.id === 'confirm-po-generation-btn') {
|
149 |
const supplierName = document.getElementById('supplier-name').value.trim();
|
150 |
const selectedItems = [];
|
|
|
1 |
// events.js - Manages all event listeners
|
2 |
+
import { appState } from './state.js';
|
3 |
+
import { handleUpdateStock, handleRestock, handleSetStock } from './services.js';
|
|
|
4 |
import { refreshUI, showToast, renderReport, renderCustomPOModal } from './ui.js';
|
5 |
import { generatePurchaseOrder } from './purchaseOrderService.js';
|
6 |
+
import { supabase } from './supabaseClient.js';
|
7 |
|
8 |
export function attachAllListeners() {
|
9 |
attachProductInputListeners();
|
|
|
13 |
}
|
14 |
|
15 |
export function attachOneTimeListeners() {
|
16 |
+
attachAuthListeners();
|
17 |
attachModalListeners();
|
18 |
}
|
19 |
|
|
|
|
|
20 |
function attachProductInputListeners() {
|
21 |
document.querySelectorAll('.update-btn').forEach(btn => {
|
22 |
if (btn.dataset.listenerAttached) return;
|
|
|
38 |
const card = e.target.closest('.dashboard-card');
|
39 |
const valueDiv = card.querySelector('.current-stock-value');
|
40 |
const materialName = card.dataset.materialName;
|
|
|
41 |
|
42 |
const input = document.createElement('input');
|
43 |
input.type = 'number';
|
44 |
+
input.value = valueDiv.textContent;
|
45 |
input.className = 'input-field w-24 text-2xl font-bold';
|
46 |
|
47 |
valueDiv.replaceWith(input);
|
|
|
50 |
|
51 |
const saveChange = () => {
|
52 |
const newValue = parseInt(input.value, 10);
|
53 |
+
handleSetStock(materialName, newValue);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
};
|
55 |
|
56 |
input.addEventListener('blur', saveChange);
|
|
|
85 |
}
|
86 |
|
87 |
function attachPurchaseOrderListener() {
|
88 |
+
// This listener is now attached dynamically in renderReorderList in ui.js
|
89 |
+
// We can use event delegation on the header to make it more robust.
|
90 |
+
const reorderHeader = document.getElementById('reorder-header');
|
91 |
+
if (reorderHeader && !reorderHeader.dataset.listenerAttached) {
|
92 |
+
reorderHeader.dataset.listenerAttached = 'true';
|
93 |
+
reorderHeader.addEventListener('click', (e) => {
|
94 |
+
if (e.target.id === 'open-po-modal-btn') {
|
95 |
+
const materialsToOrder = appState.materials.filter(m => m.currentStock <= m.reorderPoint * 1.5);
|
96 |
+
if (materialsToOrder.length > 0) {
|
97 |
+
renderCustomPOModal(materialsToOrder);
|
98 |
+
} else {
|
99 |
+
showToast('No items need reordering.', 'info');
|
100 |
+
}
|
101 |
}
|
102 |
});
|
103 |
}
|
104 |
}
|
105 |
|
106 |
+
function attachAuthListeners() {
|
107 |
+
const loginForm = document.getElementById('login-form');
|
108 |
+
const logoutBtn = document.getElementById('logout-btn');
|
109 |
+
|
110 |
+
if (loginForm) {
|
111 |
+
loginForm.addEventListener('submit', async (e) => {
|
112 |
+
e.preventDefault();
|
113 |
+
const email = document.getElementById('email').value;
|
114 |
+
const password = document.getElementById('password').value;
|
115 |
+
|
116 |
+
const { error } = await supabase.auth.signInWithPassword({ email, password });
|
117 |
+
|
118 |
+
if (error) {
|
119 |
+
showToast(`Login failed: ${error.message}`, 'error');
|
120 |
+
}
|
121 |
+
// onAuthStateChange in main.js will handle success
|
122 |
+
});
|
123 |
+
}
|
124 |
+
|
125 |
+
if (logoutBtn) {
|
126 |
+
logoutBtn.addEventListener('click', async () => {
|
127 |
+
await supabase.auth.signOut();
|
128 |
+
// onAuthStateChange in main.js will handle UI changes
|
129 |
+
});
|
130 |
+
}
|
131 |
+
}
|
132 |
+
|
133 |
function attachModalListeners() {
|
|
|
134 |
const resetModal = document.getElementById('reset-modal');
|
135 |
const reportsModal = document.getElementById('reports-modal');
|
136 |
+
const customPOModal = document.getElementById('custom-po-modal');
|
137 |
+
|
138 |
+
document.getElementById('show-reset-modal-btn')?.addEventListener('click', () => resetModal.classList.remove('hidden'));
|
139 |
|
140 |
+
resetModal.addEventListener('click', async (e) => {
|
|
|
141 |
const target = e.target.closest('button');
|
142 |
if ((target && target.id === 'cancel-reset-btn') || e.target === resetModal) {
|
143 |
resetModal.classList.add('hidden');
|
144 |
}
|
145 |
if (target && target.id === 'confirm-reset-btn') {
|
146 |
+
showToast('Resetting data... please wait.', 'info');
|
147 |
+
await supabase.from('production_log').delete().neq('id', '00000000-0000-0000-0000-000000000000');
|
148 |
+
await supabase.from('materials').delete().neq('id', '00000000-0000-0000-0000-000000000000');
|
149 |
window.location.reload();
|
150 |
}
|
151 |
});
|
152 |
|
153 |
+
document.getElementById('show-reports-modal-btn')?.addEventListener('click', () => reportsModal.classList.remove('hidden'));
|
154 |
reportsModal.addEventListener('click', (e) => {
|
155 |
const target = e.target.closest('button');
|
156 |
if ((target && target.id === 'close-reports-modal-btn') || e.target === reportsModal) {
|
|
|
159 |
if (target && target.id === 'report-prod-summary') renderReport('production');
|
160 |
if (target && target.id === 'report-mat-usage') renderReport('material');
|
161 |
});
|
162 |
+
|
|
|
|
|
163 |
customPOModal.addEventListener('click', e => {
|
164 |
const target = e.target;
|
165 |
+
if (target.id === 'custom-po-modal' || target.closest('#cancel-po-btn') || target.id === 'cancel-po-btn-footer') {
|
|
|
166 |
customPOModal.classList.add('hidden');
|
167 |
}
|
168 |
|
|
|
169 |
if (target.id === 'confirm-po-generation-btn') {
|
170 |
const supplierName = document.getElementById('supplier-name').value.trim();
|
171 |
const selectedItems = [];
|