Ultronprime commited on
Commit
2f6fc2d
·
verified ·
1 Parent(s): 8b41798

Update main.js

Browse files
Files changed (1) hide show
  1. main.js +37 -8
main.js CHANGED
@@ -1,17 +1,46 @@
1
  // main.js - Application Entry Point
2
-
3
- import { loadState } from './state.js';
4
  import { refreshUI } from './ui.js';
5
  import { attachOneTimeListeners } from './events.js';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  // Initializes the application when the page content is loaded.
8
  function init() {
9
- loadState();
10
- refreshUI();
11
- attachOneTimeListeners(); // Attach persistent listeners once
12
-
13
- // Trigger fade-in animation for main content
14
- document.querySelector('main').classList.remove('opacity-0');
15
  }
16
 
17
  document.addEventListener('DOMContentLoaded', init);
 
1
  // main.js - Application Entry Point
2
+ import { loadInitialAppState, appState } from './state.js';
 
3
  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
+ // This function will run whenever the user's login state changes
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
+ mainContent.classList.remove('hidden');
20
+ userInfo.classList.remove('hidden');
21
+ logoutBtn.classList.remove('hidden');
22
+ reportsBtn.classList.remove('hidden');
23
+ userEmailSpan.textContent = session.user.email;
24
+
25
+ await loadInitialAppState();
26
+ refreshUI();
27
+ } else if (event === 'SIGNED_OUT') {
28
+ appState.user = null;
29
+ appState.materials = [];
30
+ appState.productionLog = [];
31
+ loginScreen.classList.remove('hidden');
32
+ mainContent.classList.add('hidden');
33
+ userInfo.classList.add('hidden');
34
+ logoutBtn.classList.add('hidden');
35
+ reportsBtn.classList.add('hidden');
36
+ refreshUI(); // Re-render to clear out old data from the UI
37
+ }
38
+ });
39
 
40
  // Initializes the application when the page content is loaded.
41
  function init() {
42
+ attachOneTimeListeners();
43
+ mainContent.classList.remove('opacity-0');
 
 
 
 
44
  }
45
 
46
  document.addEventListener('DOMContentLoaded', init);