Spaces:
Running
Running
Update state.js
Browse files
state.js
CHANGED
|
@@ -1,22 +1,77 @@
|
|
| 1 |
-
// state.js - Manages Application State
|
| 2 |
-
|
| 3 |
import { MATERIALS_CONFIG, RECIPES_CONFIG } from './config.js';
|
|
|
|
| 4 |
|
| 5 |
-
export let appState = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
}
|
|
|
|
| 1 |
+
// state.js - Manages Application State with Supabase
|
| 2 |
+
import { supabase } from './supabaseClient.js';
|
| 3 |
import { MATERIALS_CONFIG, RECIPES_CONFIG } from './config.js';
|
| 4 |
+
import { showToast } from './ui.js';
|
| 5 |
|
| 6 |
+
export let appState = {
|
| 7 |
+
user: null, // To store the logged-in user
|
| 8 |
+
materials: [],
|
| 9 |
+
productRecipes: RECIPES_CONFIG,
|
| 10 |
+
productionLog: []
|
| 11 |
+
};
|
| 12 |
|
| 13 |
+
// Helper function to convert database material format to app format
|
| 14 |
+
function dbMaterialToAppMaterial(dbMaterial) {
|
| 15 |
+
return {
|
| 16 |
+
id: dbMaterial.id,
|
| 17 |
+
name: dbMaterial.name,
|
| 18 |
+
unit: dbMaterial.unit,
|
| 19 |
+
currentStock: dbMaterial.current_stock,
|
| 20 |
+
reorderPoint: dbMaterial.reorder_point
|
| 21 |
+
};
|
| 22 |
}
|
| 23 |
|
| 24 |
+
// Function to load initial application state from Supabase
|
| 25 |
+
export async function loadInitialAppState() {
|
| 26 |
+
try {
|
| 27 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 28 |
+
if (!user) throw new Error("User not authenticated.");
|
| 29 |
+
appState.user = user;
|
| 30 |
+
|
| 31 |
+
const { data: materials, error: materialsError } = await supabase
|
| 32 |
+
.from('materials')
|
| 33 |
+
.select('*')
|
| 34 |
+
.order('name', { ascending: true });
|
| 35 |
+
if (materialsError) throw materialsError;
|
| 36 |
+
|
| 37 |
+
const { data: productionLog, error: logError } = await supabase
|
| 38 |
+
.from('production_log')
|
| 39 |
+
.select('*')
|
| 40 |
+
.order('produced_at', { ascending: false })
|
| 41 |
+
.limit(100); // Get latest 100 logs
|
| 42 |
+
if (logError) throw logError;
|
| 43 |
+
|
| 44 |
+
// If no materials in DB, initialize from config (first time setup)
|
| 45 |
+
if (materials.length === 0 && MATERIALS_CONFIG.length > 0) {
|
| 46 |
+
showToast('No materials found. Initializing from config...', 'info');
|
| 47 |
+
const { data: insertedMaterials, error: insertError } = await supabase
|
| 48 |
+
.from('materials')
|
| 49 |
+
.insert(MATERIALS_CONFIG.map(m => ({
|
| 50 |
+
name: m.name,
|
| 51 |
+
unit: m.unit,
|
| 52 |
+
current_stock: m.currentStock,
|
| 53 |
+
reorder_point: m.reorderPoint
|
| 54 |
+
})))
|
| 55 |
+
.select();
|
| 56 |
+
|
| 57 |
+
if (insertError) throw insertError;
|
| 58 |
+
appState.materials = insertedMaterials.map(dbMaterialToAppMaterial);
|
| 59 |
+
} else {
|
| 60 |
+
appState.materials = materials.map(dbMaterialToAppMaterial);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
appState.productionLog = productionLog.map(log => ({
|
| 64 |
+
id: log.id,
|
| 65 |
+
productName: log.product_name,
|
| 66 |
+
quantity: log.quantity,
|
| 67 |
+
date: log.produced_at
|
| 68 |
+
}));
|
| 69 |
+
|
| 70 |
+
console.log('App state loaded successfully from Supabase.');
|
| 71 |
+
return true;
|
| 72 |
+
} catch (error) {
|
| 73 |
+
console.error('Error loading app state:', error.message);
|
| 74 |
+
showToast(`Failed to load data: ${error.message}`, 'error');
|
| 75 |
+
return false;
|
| 76 |
}
|
| 77 |
}
|