Ultronprime commited on
Commit
e6e9d7a
·
verified ·
1 Parent(s): 0c9f258

Update state.js

Browse files
Files changed (1) hide show
  1. state.js +70 -15
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
- export function saveState() {
8
- localStorage.setItem('antennaTrackerState', JSON.stringify(appState));
 
 
 
 
 
 
 
9
  }
10
 
11
- export function loadState() {
12
- const savedState = localStorage.getItem('antennaTrackerState');
13
- if (savedState) {
14
- Object.assign(appState, JSON.parse(savedState));
15
- appState.productRecipes = RECIPES_CONFIG; // Always use latest recipes from config
16
- } else {
17
- // First time load: create a deep copy from config
18
- appState.materials = JSON.parse(JSON.stringify(MATERIALS_CONFIG));
19
- appState.productRecipes = RECIPES_CONFIG;
20
- appState.productionLog = [];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  }