Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
@@ -4,43 +4,74 @@ import { refreshUI } from './ui.js';
|
|
4 |
import { attachOneTimeListeners } from './events.js';
|
5 |
import { supabase } from './supabaseClient.js';
|
6 |
|
|
|
7 |
const loginScreen = document.getElementById('login-screen');
|
8 |
const mainContent = document.getElementById('main-content');
|
|
|
9 |
const userInfo = document.getElementById('user-info');
|
10 |
const userEmailSpan = document.getElementById('user-email');
|
11 |
const logoutBtn = document.getElementById('logout-btn');
|
12 |
const reportsBtn = document.getElementById('show-reports-modal-btn');
|
|
|
13 |
|
14 |
-
//
|
15 |
supabase.auth.onAuthStateChange(async (event, session) => {
|
|
|
16 |
if (event === 'SIGNED_IN' || (event === 'INITIAL_SESSION' && session)) {
|
17 |
appState.user = session.user;
|
|
|
|
|
18 |
loginScreen.classList.add('hidden');
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
await loadInitialAppState();
|
26 |
-
refreshUI();
|
27 |
} else if (event === 'SIGNED_OUT') {
|
28 |
appState.user = null;
|
29 |
appState.materials = [];
|
30 |
appState.productionLog = [];
|
31 |
-
|
|
|
32 |
mainContent.classList.add('hidden');
|
|
|
|
|
33 |
userInfo.classList.add('hidden');
|
34 |
logoutBtn.classList.add('hidden');
|
35 |
reportsBtn.classList.add('hidden');
|
36 |
-
|
|
|
|
|
37 |
}
|
38 |
});
|
39 |
|
40 |
// Initializes the application when the page content is loaded.
|
41 |
function init() {
|
42 |
attachOneTimeListeners();
|
43 |
-
|
44 |
}
|
45 |
|
46 |
document.addEventListener('DOMContentLoaded', init);
|
|
|
4 |
import { attachOneTimeListeners } from './events.js';
|
5 |
import { supabase } from './supabaseClient.js';
|
6 |
|
7 |
+
// UI Elements
|
8 |
const loginScreen = document.getElementById('login-screen');
|
9 |
const mainContent = document.getElementById('main-content');
|
10 |
+
const loader = document.getElementById('loader');
|
11 |
const userInfo = document.getElementById('user-info');
|
12 |
const userEmailSpan = document.getElementById('user-email');
|
13 |
const logoutBtn = document.getElementById('logout-btn');
|
14 |
const reportsBtn = document.getElementById('show-reports-modal-btn');
|
15 |
+
const footer = document.getElementById('footer');
|
16 |
|
17 |
+
// --- MODIFICATION: Robust Auth State Change Handler ---
|
18 |
supabase.auth.onAuthStateChange(async (event, session) => {
|
19 |
+
// This handles both manual sign-in and automatic session restoration
|
20 |
if (event === 'SIGNED_IN' || (event === 'INITIAL_SESSION' && session)) {
|
21 |
appState.user = session.user;
|
22 |
+
|
23 |
+
// 1. Show loader, hide login screen
|
24 |
loginScreen.classList.add('hidden');
|
25 |
+
loader.classList.remove('hidden');
|
26 |
+
loader.classList.add('flex'); // Use flex to center content
|
27 |
+
|
28 |
+
// 2. Load all data from the database
|
29 |
+
const loadedSuccessfully = await loadInitialAppState();
|
30 |
+
|
31 |
+
// 3. Once data is loaded, update the UI
|
32 |
+
if (loadedSuccessfully) {
|
33 |
+
refreshUI();
|
34 |
+
|
35 |
+
// 4. Show the main content and hide the loader
|
36 |
+
mainContent.classList.remove('hidden');
|
37 |
+
mainContent.classList.remove('opacity-0');
|
38 |
+
footer.classList.remove('hidden');
|
39 |
+
userInfo.classList.remove('hidden');
|
40 |
+
logoutBtn.classList.remove('hidden');
|
41 |
+
reportsBtn.classList.remove('hidden');
|
42 |
+
userEmailSpan.textContent = session.user.email;
|
43 |
+
} else {
|
44 |
+
// Handle case where data loading fails
|
45 |
+
// Maybe show an error and a retry button, or force logout
|
46 |
+
alert('Failed to load factory data. Please try again.');
|
47 |
+
await supabase.auth.signOut();
|
48 |
+
}
|
49 |
+
|
50 |
+
loader.classList.add('hidden');
|
51 |
+
loader.classList.remove('flex');
|
52 |
|
|
|
|
|
53 |
} else if (event === 'SIGNED_OUT') {
|
54 |
appState.user = null;
|
55 |
appState.materials = [];
|
56 |
appState.productionLog = [];
|
57 |
+
|
58 |
+
// Ensure a clean UI state on logout
|
59 |
mainContent.classList.add('hidden');
|
60 |
+
mainContent.classList.add('opacity-0');
|
61 |
+
footer.classList.add('hidden');
|
62 |
userInfo.classList.add('hidden');
|
63 |
logoutBtn.classList.add('hidden');
|
64 |
reportsBtn.classList.add('hidden');
|
65 |
+
loader.classList.add('hidden');
|
66 |
+
loader.classList.remove('flex');
|
67 |
+
loginScreen.classList.remove('hidden');
|
68 |
}
|
69 |
});
|
70 |
|
71 |
// Initializes the application when the page content is loaded.
|
72 |
function init() {
|
73 |
attachOneTimeListeners();
|
74 |
+
// The opacity transition is now handled by the auth listener
|
75 |
}
|
76 |
|
77 |
document.addEventListener('DOMContentLoaded', init);
|