Hkoradiya123 commited on
Commit
f46e5bb
·
1 Parent(s): 742f041

feat: Implement loading skeletons for various pages and add skeleton styles; enhance authentication state initialization in App component

Browse files
frontend/src/App.jsx CHANGED
@@ -11,9 +11,25 @@ import { AdminPage } from './pages/Admin';
11
  import { MatchesPage } from './pages/Matches';
12
 
13
  function App() {
14
- const [isAuthenticated, setIsAuthenticated] = useState(false);
15
- const [user, setUser] = useState(null);
16
- const [isLoading, setIsLoading] = useState(true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  const isAdmin = user?.role === 'admin';
18
 
19
  const updateAuthState = () => {
@@ -22,8 +38,13 @@ function App() {
22
  const userData = localStorage.getItem('user');
23
 
24
  if (token && userData) {
25
- setIsAuthenticated(true);
26
- setUser(JSON.parse(userData));
 
 
 
 
 
27
  } else {
28
  setIsAuthenticated(false);
29
  setUser(null);
@@ -31,10 +52,6 @@ function App() {
31
  };
32
 
33
  useEffect(() => {
34
- // Initial auth check from localStorage
35
- updateAuthState();
36
- setIsLoading(false);
37
-
38
  // Listen for auth changes (login, logout)
39
  const handleAuthChange = () => {
40
  updateAuthState();
 
11
  import { MatchesPage } from './pages/Matches';
12
 
13
  function App() {
14
+ // Initialize auth state synchronously from localStorage
15
+ const initializeAuth = () => {
16
+ const token = localStorage.getItem('token');
17
+ const userData = localStorage.getItem('user');
18
+
19
+ if (token && userData) {
20
+ try {
21
+ return { isAuthenticated: true, user: JSON.parse(userData) };
22
+ } catch {
23
+ return { isAuthenticated: false, user: null };
24
+ }
25
+ }
26
+ return { isAuthenticated: false, user: null };
27
+ };
28
+
29
+ const initialAuth = initializeAuth();
30
+ const [isAuthenticated, setIsAuthenticated] = useState(initialAuth.isAuthenticated);
31
+ const [user, setUser] = useState(initialAuth.user);
32
+ const [isLoading, setIsLoading] = useState(false);
33
  const isAdmin = user?.role === 'admin';
34
 
35
  const updateAuthState = () => {
 
38
  const userData = localStorage.getItem('user');
39
 
40
  if (token && userData) {
41
+ try {
42
+ setIsAuthenticated(true);
43
+ setUser(JSON.parse(userData));
44
+ } catch {
45
+ setIsAuthenticated(false);
46
+ setUser(null);
47
+ }
48
  } else {
49
  setIsAuthenticated(false);
50
  setUser(null);
 
52
  };
53
 
54
  useEffect(() => {
 
 
 
 
55
  // Listen for auth changes (login, logout)
56
  const handleAuthChange = () => {
57
  updateAuthState();
frontend/src/components/Navbar.jsx CHANGED
@@ -15,11 +15,21 @@ export function Navbar({ isAuthenticated, user }) {
15
 
16
  try {
17
  setIsLoggingOut(true);
18
- authService.logout();
19
  localStorage.removeItem('token');
20
  localStorage.removeItem('user');
21
  sessionStorage.setItem('suppressSocialAutoLogin', '1');
22
- window.location.replace('/login');
 
 
 
 
 
 
 
 
 
 
23
  } finally {
24
  setIsLoggingOut(false);
25
  }
 
15
 
16
  try {
17
  setIsLoggingOut(true);
18
+ // Clear auth data first
19
  localStorage.removeItem('token');
20
  localStorage.removeItem('user');
21
  sessionStorage.setItem('suppressSocialAutoLogin', '1');
22
+
23
+ // Call logout API
24
+ authService.logout().catch(() => null); // Ignore errors
25
+
26
+ // Dispatch auth change event to notify App component
27
+ window.dispatchEvent(new Event('authchange'));
28
+
29
+ // Small delay to ensure state updates before redirect
30
+ setTimeout(() => {
31
+ window.location.replace('/login');
32
+ }, 100);
33
  } finally {
34
  setIsLoggingOut(false);
35
  }
frontend/src/components/Skeleton.jsx ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import styles from './skeleton.module.css';
3
+
4
+ export function SkeletonBox({ width = '100%', height = '20px', marginBottom = '12px', borderRadius = '8px' }) {
5
+ return (
6
+ <div
7
+ className={styles.skeleton}
8
+ style={{
9
+ width,
10
+ height,
11
+ marginBottom,
12
+ borderRadius,
13
+ }}
14
+ />
15
+ );
16
+ }
17
+
18
+ export function SkeletonCard({ className = '', children = null }) {
19
+ return (
20
+ <div className={`${styles.skeletonCard} ${className}`}>
21
+ {children || (
22
+ <>
23
+ <SkeletonBox height="24px" marginBottom="16px" />
24
+ <SkeletonBox height="16px" marginBottom="8px" />
25
+ <SkeletonBox height="16px" marginBottom="8px" width="80%" />
26
+ </>
27
+ )}
28
+ </div>
29
+ );
30
+ }
31
+
32
+ export function SkeletonText({ lines = 3, width = '100%' }) {
33
+ return (
34
+ <>
35
+ {Array(lines).fill(null).map((_, i) => (
36
+ <SkeletonBox
37
+ key={i}
38
+ width={i === lines - 1 ? '80%' : width}
39
+ height="16px"
40
+ marginBottom="8px"
41
+ />
42
+ ))}
43
+ </>
44
+ );
45
+ }
46
+
47
+ export function DashboardPageSkeleton() {
48
+ return (
49
+ <div style={{ padding: '24px' }}>
50
+ {/* Overview Cards */}
51
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))', gap: '16px', marginBottom: '32px' }}>
52
+ {Array(4).fill(null).map((_, i) => (
53
+ <SkeletonCard key={i}>
54
+ <SkeletonBox height="16px" marginBottom="12px" width="60%" />
55
+ <SkeletonBox height="32px" marginBottom="8px" />
56
+ <SkeletonBox height="12px" width="70%" />
57
+ </SkeletonCard>
58
+ ))}
59
+ </div>
60
+
61
+ {/* Featured Players */}
62
+ <div style={{ marginBottom: '32px' }}>
63
+ <SkeletonBox height="24px" marginBottom="16px" width="30%" />
64
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: '16px' }}>
65
+ {Array(3).fill(null).map((_, i) => (
66
+ <SkeletonCard key={i} />
67
+ ))}
68
+ </div>
69
+ </div>
70
+
71
+ {/* Charts */}
72
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(300px, 1fr))', gap: '16px' }}>
73
+ {Array(2).fill(null).map((_, i) => (
74
+ <SkeletonCard key={i}>
75
+ <SkeletonBox height="300px" marginBottom="0" />
76
+ </SkeletonCard>
77
+ ))}
78
+ </div>
79
+ </div>
80
+ );
81
+ }
82
+
83
+ export function PlayersPageSkeleton() {
84
+ return (
85
+ <div style={{ padding: '24px' }}>
86
+ {/* Filters and Search */}
87
+ <div style={{ marginBottom: '24px', display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(150px, 1fr))', gap: '12px' }}>
88
+ {Array(4).fill(null).map((_, i) => (
89
+ <SkeletonBox key={i} height="40px" marginBottom="0" />
90
+ ))}
91
+ </div>
92
+
93
+ {/* Players List */}
94
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))', gap: '16px' }}>
95
+ {Array(8).fill(null).map((_, i) => (
96
+ <SkeletonCard key={i} />
97
+ ))}
98
+ </div>
99
+ </div>
100
+ );
101
+ }
102
+
103
+ export function FinancePageSkeleton() {
104
+ return (
105
+ <div style={{ padding: '24px' }}>
106
+ {/* Summary Cards */}
107
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: '16px', marginBottom: '32px' }}>
108
+ {Array(3).fill(null).map((_, i) => (
109
+ <SkeletonCard key={i}>
110
+ <SkeletonBox height="16px" marginBottom="12px" width="60%" />
111
+ <SkeletonBox height="32px" marginBottom="8px" />
112
+ <SkeletonBox height="12px" width="70%" />
113
+ </SkeletonCard>
114
+ ))}
115
+ </div>
116
+
117
+ {/* Transactions List */}
118
+ <div>
119
+ <SkeletonBox height="24px" marginBottom="16px" width="30%" />
120
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
121
+ {Array(5).fill(null).map((_, i) => (
122
+ <SkeletonBox key={i} height="60px" marginBottom="0" />
123
+ ))}
124
+ </div>
125
+ </div>
126
+ </div>
127
+ );
128
+ }
129
+
130
+ export function AdminPageSkeleton() {
131
+ return (
132
+ <div style={{ padding: '24px' }}>
133
+ {/* Stats Cards */}
134
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(150px, 1fr))', gap: '16px', marginBottom: '32px' }}>
135
+ {Array(4).fill(null).map((_, i) => (
136
+ <SkeletonCard key={i} />
137
+ ))}
138
+ </div>
139
+
140
+ {/* Users Table */}
141
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
142
+ <SkeletonBox height="20px" marginBottom="16px" width="30%" />
143
+ {Array(10).fill(null).map((_, i) => (
144
+ <SkeletonBox key={i} height="50px" marginBottom="0" />
145
+ ))}
146
+ </div>
147
+ </div>
148
+ );
149
+ }
150
+
151
+ export function MatchesPageSkeleton() {
152
+ return (
153
+ <div style={{ padding: '24px' }}>
154
+ {/* Matches List */}
155
+ <SkeletonBox height="24px" marginBottom="16px" width="30%" />
156
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '12px', marginBottom: '32px' }}>
157
+ {Array(5).fill(null).map((_, i) => (
158
+ <SkeletonBox key={i} height="80px" marginBottom="0" />
159
+ ))}
160
+ </div>
161
+
162
+ {/* Scoreboard */}
163
+ <div>
164
+ <SkeletonBox height="24px" marginBottom="16px" width="30%" />
165
+ <SkeletonCard>
166
+ <SkeletonBox height="300px" marginBottom="0" />
167
+ </SkeletonCard>
168
+ </div>
169
+ </div>
170
+ );
171
+ }
172
+
173
+ export function ProfilePageSkeleton() {
174
+ return (
175
+ <div style={{ padding: '24px' }}>
176
+ {/* Profile Header */}
177
+ <div style={{ display: 'flex', gap: '24px', marginBottom: '32px', alignItems: 'center' }}>
178
+ <SkeletonBox width="120px" height="120px" borderRadius="50%" marginBottom="0" />
179
+ <div style={{ flex: 1 }}>
180
+ <SkeletonBox height="32px" marginBottom="12px" width="50%" />
181
+ <SkeletonBox height="16px" marginBottom="8px" width="70%" />
182
+ <SkeletonBox height="16px" marginBottom="0" width="40%" />
183
+ </div>
184
+ </div>
185
+
186
+ {/* Stats Grid */}
187
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(150px, 1fr))', gap: '16px', marginBottom: '32px' }}>
188
+ {Array(6).fill(null).map((_, i) => (
189
+ <SkeletonCard key={i} />
190
+ ))}
191
+ </div>
192
+
193
+ {/* Tabs Content */}
194
+ <div>
195
+ <SkeletonBox height="20px" marginBottom="16px" width="30%" />
196
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
197
+ {Array(3).fill(null).map((_, i) => (
198
+ <SkeletonBox key={i} height="60px" marginBottom="0" />
199
+ ))}
200
+ </div>
201
+ </div>
202
+ </div>
203
+ );
204
+ }
frontend/src/components/skeleton.module.css ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .skeleton {
2
+ background: linear-gradient(
3
+ 90deg,
4
+ #e0e0e0 0%,
5
+ #f0f0f0 50%,
6
+ #e0e0e0 100%
7
+ );
8
+ background-size: 200% 100%;
9
+ animation: shimmer 1.5s infinite;
10
+ }
11
+
12
+ @keyframes shimmer {
13
+ 0% {
14
+ background-position: 200% 0;
15
+ }
16
+ 100% {
17
+ background-position: -200% 0;
18
+ }
19
+ }
20
+
21
+ .skeletonCard {
22
+ background-color: white;
23
+ border-radius: 8px;
24
+ padding: 16px;
25
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
26
+ }
frontend/src/pages/Admin.jsx CHANGED
@@ -1,5 +1,6 @@
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { adminService } from '../utils/api';
 
3
  import styles from './admin.module.css';
4
 
5
  const USERS_PER_PAGE = 10;
@@ -257,7 +258,7 @@ export function AdminPage() {
257
  }
258
  };
259
 
260
- if (loading) return <div className={styles.loading}>Loading admin dashboard...</div>;
261
 
262
  if (!isAdmin) {
263
  return (
 
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { adminService } from '../utils/api';
3
+ import { AdminPageSkeleton } from '../components/Skeleton';
4
  import styles from './admin.module.css';
5
 
6
  const USERS_PER_PAGE = 10;
 
258
  }
259
  };
260
 
261
+ if (loading) return <AdminPageSkeleton />;
262
 
263
  if (!isAdmin) {
264
  return (
frontend/src/pages/Dashboard.jsx CHANGED
@@ -1,5 +1,6 @@
1
  import React, { useState, useEffect } from 'react';
2
  import { dashboardService, notificationService } from '../utils/api';
 
3
  import styles from './dashboard.module.css';
4
 
5
  export function DashboardPage() {
@@ -93,7 +94,7 @@ export function DashboardPage() {
93
  fetchData();
94
  }, []);
95
 
96
- if (loading) return <div className={styles.loading}>Loading...</div>;
97
  if (error) return <div className={styles.error}>{error}</div>;
98
 
99
  const topRuns = chartData.top_players_runs || [];
 
1
  import React, { useState, useEffect } from 'react';
2
  import { dashboardService, notificationService } from '../utils/api';
3
+ import { DashboardPageSkeleton } from '../components/Skeleton';
4
  import styles from './dashboard.module.css';
5
 
6
  export function DashboardPage() {
 
94
  fetchData();
95
  }, []);
96
 
97
+ if (loading) return <DashboardPageSkeleton />;
98
  if (error) return <div className={styles.error}>{error}</div>;
99
 
100
  const topRuns = chartData.top_players_runs || [];
frontend/src/pages/Finance.jsx CHANGED
@@ -1,5 +1,6 @@
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { financeService } from '../utils/api';
 
3
  import styles from './finance.module.css';
4
 
5
  export function FinancePage() {
@@ -82,7 +83,7 @@ export function FinancePage() {
82
  }
83
  };
84
 
85
- if (loading) return <div className={styles.loading}>Loading finance dashboard...</div>;
86
  if (error) return <div className={styles.error}>{error}</div>;
87
 
88
  return (
 
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { financeService } from '../utils/api';
3
+ import { FinancePageSkeleton } from '../components/Skeleton';
4
  import styles from './finance.module.css';
5
 
6
  export function FinancePage() {
 
83
  }
84
  };
85
 
86
+ if (loading) return <FinancePageSkeleton />;
87
  if (error) return <div className={styles.error}>{error}</div>;
88
 
89
  return (
frontend/src/pages/Matches.jsx CHANGED
@@ -1,5 +1,6 @@
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { matchesService, playerService } from '../utils/api';
 
3
  import styles from './matches.module.css';
4
 
5
  export function MatchesPage() {
@@ -47,6 +48,7 @@ export function MatchesPage() {
47
  });
48
 
49
  const [isLoading, setIsLoading] = useState(false);
 
50
  const [showUmpireGuide, setShowUmpireGuide] = useState(true);
51
  const [showMatchHistory, setShowMatchHistory] = useState(false);
52
  const [matchHistoryData, setMatchHistoryData] = useState([]);
@@ -103,6 +105,7 @@ export function MatchesPage() {
103
 
104
  const fetchBase = async () => {
105
  try {
 
106
  const [matchRes, playerRes] = await Promise.all([
107
  matchesService.listMatches(),
108
  playerService.getAllPlayers(0, 200),
@@ -111,6 +114,8 @@ export function MatchesPage() {
111
  setPlayers((playerRes.data || []).filter((p) => p.role === 'player'));
112
  } catch (err) {
113
  setError(err.response?.data?.detail || 'Failed to load match data');
 
 
114
  }
115
  };
116
 
@@ -413,7 +418,10 @@ export function MatchesPage() {
413
  };
414
 
415
  return (
416
- <div className={styles.page}>
 
 
 
417
  <div className={styles.container}>
418
  <h1>Live Match Center</h1>
419
  <p className={styles.subtitle}>Premium creators can set teams and track ball-by-ball scoring in real time.</p>
@@ -1174,5 +1182,7 @@ export function MatchesPage() {
1174
  )}
1175
  </div>
1176
  </div>
 
 
1177
  );
1178
  }
 
1
  import React, { useEffect, useMemo, useState } from 'react';
2
  import { matchesService, playerService } from '../utils/api';
3
+ import { MatchesPageSkeleton } from '../components/Skeleton';
4
  import styles from './matches.module.css';
5
 
6
  export function MatchesPage() {
 
48
  });
49
 
50
  const [isLoading, setIsLoading] = useState(false);
51
+ const [initialLoading, setInitialLoading] = useState(true);
52
  const [showUmpireGuide, setShowUmpireGuide] = useState(true);
53
  const [showMatchHistory, setShowMatchHistory] = useState(false);
54
  const [matchHistoryData, setMatchHistoryData] = useState([]);
 
105
 
106
  const fetchBase = async () => {
107
  try {
108
+ setInitialLoading(true);
109
  const [matchRes, playerRes] = await Promise.all([
110
  matchesService.listMatches(),
111
  playerService.getAllPlayers(0, 200),
 
114
  setPlayers((playerRes.data || []).filter((p) => p.role === 'player'));
115
  } catch (err) {
116
  setError(err.response?.data?.detail || 'Failed to load match data');
117
+ } finally {
118
+ setInitialLoading(false);
119
  }
120
  };
121
 
 
418
  };
419
 
420
  return (
421
+ <>
422
+ {initialLoading && <MatchesPageSkeleton />}
423
+ {!initialLoading && (
424
+ <div className={styles.page}>
425
  <div className={styles.container}>
426
  <h1>Live Match Center</h1>
427
  <p className={styles.subtitle}>Premium creators can set teams and track ball-by-ball scoring in real time.</p>
 
1182
  )}
1183
  </div>
1184
  </div>
1185
+ )}
1186
+ </>
1187
  );
1188
  }
frontend/src/pages/Players.jsx CHANGED
@@ -1,6 +1,7 @@
1
  import React, { useState, useEffect } from 'react';
2
  import { playerService } from '../utils/api';
3
  import { Link } from 'react-router-dom';
 
4
  import styles from './players.module.css';
5
 
6
  export function PlayersPage() {
@@ -45,7 +46,7 @@ export function PlayersPage() {
45
  return b.runs - a.runs;
46
  });
47
 
48
- if (loading) return <div className={styles.loading}>Loading players...</div>;
49
 
50
  return (
51
  <div className={styles.playersPage}>
 
1
  import React, { useState, useEffect } from 'react';
2
  import { playerService } from '../utils/api';
3
  import { Link } from 'react-router-dom';
4
+ import { PlayersPageSkeleton } from '../components/Skeleton';
5
  import styles from './players.module.css';
6
 
7
  export function PlayersPage() {
 
46
  return b.runs - a.runs;
47
  });
48
 
49
+ if (loading) return <PlayersPageSkeleton />;
50
 
51
  return (
52
  <div className={styles.playersPage}>
frontend/src/pages/Profile.jsx CHANGED
@@ -1,6 +1,7 @@
1
  import React, { useState, useEffect } from 'react';
2
  import { playerService, performanceService, premiumService, adminService } from '../utils/api';
3
  import { useParams } from 'react-router-dom';
 
4
  import styles from './profile.module.css';
5
 
6
  export function ProfilePage() {
@@ -138,7 +139,7 @@ export function ProfilePage() {
138
  }
139
  };
140
 
141
- if (loading) return <div className={styles.loading}>Loading profile...</div>;
142
 
143
  return (
144
  <div className={styles.profilePage}>
 
1
  import React, { useState, useEffect } from 'react';
2
  import { playerService, performanceService, premiumService, adminService } from '../utils/api';
3
  import { useParams } from 'react-router-dom';
4
+ import { ProfilePageSkeleton } from '../components/Skeleton';
5
  import styles from './profile.module.css';
6
 
7
  export function ProfilePage() {
 
139
  }
140
  };
141
 
142
+ if (loading) return <ProfilePageSkeleton />;
143
 
144
  return (
145
  <div className={styles.profilePage}>