-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
201 lines (180 loc) · 8 KB
/
App.tsx
File metadata and controls
201 lines (180 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { useState, useEffect } from 'react';
import HomePage from './components/HomePage';
import WeeklyMarksPage from './components/WeeklyMarksPage';
import BoyMarksPage from './components/BoyMarksPage';
import Header from './components/Header';
import LoginPage from './components/LoginPage';
import DashboardPage from './components/DashboardPage';
import SettingsPage from './components/SettingsPage';
import SectionSelectPage from './components/SectionSelectPage';
import AccountSettingsPage from './components/AccountSettingsPage';
import Toast from './components/Toast';
import { HomePageSkeleton } from './components/SkeletonLoaders';
import { View, BoyMarksPageView } from './types';
import Modal from './components/Modal';
// Import custom hooks
import { useToastNotifications } from '@/hooks/useToastNotifications';
import { useAuthAndRole } from '@/hooks/useAuthAndRole';
import { useSectionManagement } from '@/hooks/useSectionManagement';
import { useAppData } from '@/hooks/useAppData';
import { useUnsavedChangesProtection } from '@/hooks/useUnsavedChangesProtection';
const App: React.FC = () => {
// Use toast notifications hook
const { toasts, showToast, removeToast } = useToastNotifications();
// Use auth and role hook
const {
currentUser,
userRole,
noRoleError,
authLoading,
performSignOut,
setCurrentUser,
setUserRole,
} = useAuthAndRole();
// State for unsaved changes protection
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
const [view, setView] = useState<View>({ page: 'home' });
// Use section management hook
const { activeSection, setActiveSection, handleSelectSection, performSwitchSection } = useSectionManagement(setView);
// Use app data hook
const { boys, settings, dataLoading, dataError, refreshData, setSettings } = useAppData(
activeSection,
showToast,
currentUser
);
// Use unsaved changes protection hook
const {
setView: navigateWithProtection,
confirmModalType,
confirmAction,
cancelAction,
handleSwitchSection: handleSwitchSectionWithProtection,
handleSignOut: handleSignOutWithProtection,
} = useUnsavedChangesProtection(
view,
setView, // Pass internal setView
hasUnsavedChanges,
setHasUnsavedChanges,
performSwitchSection,
performSignOut
);
// Handle no role error: if noRoleError is set, force sign out and clear section
useEffect(() => {
if (noRoleError) {
// Clear any active section and user role if there's a no-role error
setActiveSection(null);
setUserRole(null);
localStorage.removeItem('activeSection');
}
}, [noRoleError, setActiveSection, setUserRole]);
const renderMainContent = () => {
if (!activeSection) return null; // Should not happen if logic below is correct
switch (view.page) {
case 'home':
return <HomePage boys={boys} setView={navigateWithProtection} refreshData={refreshData} activeSection={activeSection!} showToast={showToast} />;
case 'weeklyMarks':
return <WeeklyMarksPage boys={boys} refreshData={refreshData} setHasUnsavedChanges={setHasUnsavedChanges} activeSection={activeSection!} settings={settings} showToast={showToast} />;
case 'dashboard':
return <DashboardPage boys={boys} activeSection={activeSection!} />;
case 'settings': // Section-specific settings
return <SettingsPage activeSection={activeSection!} currentSettings={settings} onSettingsSaved={setSettings} showToast={showToast} userRole={userRole} onNavigateToAccountSettings={() => navigateWithProtection({ page: 'accountSettings' })} />;
case 'accountSettings': // New: Account settings
return <AccountSettingsPage showToast={showToast} activeSection={activeSection!} />;
case 'boyMarks':
const boyMarksView = view as BoyMarksPageView;
return <BoyMarksPage boyId={boyMarksView.boyId} refreshData={refreshData} setHasUnsavedChanges={setHasUnsavedChanges} activeSection={activeSection!} showToast={showToast} />;
default:
return <HomePage boys={boys} setView={navigateWithProtection} refreshData={refreshData} activeSection={activeSection!} showToast={showToast} />;
}
};
const renderApp = () => {
// Handle loading state first
if (authLoading) {
return <HomePageSkeleton />;
}
if (currentUser && dataLoading) {
return <HomePageSkeleton />;
}
// Handle no role error
if (noRoleError) {
return (
<div className="flex items-center justify-center min-h-screen bg-slate-200 p-4">
<div className="w-full max-w-md p-8 space-y-6 bg-white rounded-lg shadow-md text-center">
<img src="https://i.postimg.cc/FHrS3pzD/full-colour-boxed-logo.png" alt="The Boys' Brigade Logo" className="w-48 mx-auto mb-4" />
<h2 className="2xl font-bold text-red-600">Access Denied</h2>
<p className="text-slate-700">{noRoleError}</p>
<p className="text-slate-500">Please ensure your email address is registered with an administrator.</p>
<button
onClick={() => setCurrentUser(null)} // Reset currentUser to trigger login page
className="mt-6 group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-junior-blue hover:brightness-90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-junior-blue"
>
Return to Login
</button>
</div>
</div>
);
}
// Handle unauthenticated user
if (!currentUser) {
return <LoginPage />;
}
// Handle authenticated user, but no active section selected yet
if (!activeSection) {
return <SectionSelectPage onSelectSection={handleSelectSection} onSignOut={handleSignOutWithProtection} />;
}
// Handle general errors
if (dataError) {
return <div className="text-center p-8 text-red-500">{dataError}</div>;
}
// Render main app content with header
return (
<>
<Header
setView={navigateWithProtection}
onSignOut={handleSignOutWithProtection}
activeSection={activeSection}
onSwitchSection={handleSwitchSectionWithProtection}
currentUser={currentUser}
userRole={userRole}
/>
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{renderMainContent()}
</main>
</>
);
}
return (
<div className="min-h-screen bg-slate-200 text-slate-800">
<div
aria-live="assertive"
className="fixed bottom-0 w-full max-w-sm mx-auto flex flex-col-reverse items-center p-4 space-y-2 space-y-reverse pointer-events-none z-[100]"
>
{toasts.map((toast) => (
<Toast key={toast.id} toast={toast} removeToast={removeToast} />
))}
</div>
{renderApp()}
<Modal isOpen={!!confirmModalType} onClose={cancelAction} title="Unsaved Changes">
<div className="space-y-4">
<p className="text-slate-600">You have unsaved changes. Are you sure you want to leave? Your changes will be lost.</p>
<div className="flex justify-end space-x-3 pt-4">
<button
type="button"
onClick={cancelAction}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-slate-100 rounded-md hover:bg-slate-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-slate-400"
>
Stay
</button>
<button
onClick={confirmAction}
className="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-md hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
>
Leave
</button>
</div>
</div>
</Modal>
</div>
);
};
export default App;