Skip to content

Commit 1e4acbf

Browse files
committed
Cleaning up code base and adding loading screen while fetching user details
1 parent e69f71d commit 1e4acbf

12 files changed

Lines changed: 69 additions & 260 deletions

File tree

src/app.tsx

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from 'react';
1+
import { useEffect, useState } from 'react';
22

33
import { Outlet } from '@tanstack/react-router';
44

@@ -11,9 +11,13 @@ import { useAppStore } from '@/store/store';
1111

1212
export function App() {
1313
const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth();
14-
const { mutate: fetchUserDetails } = useGetUserDetailsMutation();
14+
const { mutate: fetchUserDetails, isPending: isFetchingUserDetails } =
15+
useGetUserDetailsMutation();
1516
const { setUserDetail } = useAppStore();
1617

18+
const [userDetailsFetched, setUserDetailsFetched] = useState(false);
19+
const [fetchInitiated, setFetchInitiated] = useState(false);
20+
1721
useEffect(() => {
1822
Logger.logEvent('AppStarted', {
1923
startTime: new Date().toISOString(),
@@ -46,6 +50,7 @@ export function App() {
4650
window.removeEventListener('unhandledrejection', handleUnhandledRejection);
4751
};
4852
}, []);
53+
4954
// Handle authentication redirect
5055
useEffect(() => {
5156
if (!isLoading && !isAuthenticated) {
@@ -61,34 +66,63 @@ export function App() {
6166
returnTo: window.location.pathname + window.location.search,
6267
},
6368
});
64-
} else if (isAuthenticated && user?.email) {
69+
} else if (isAuthenticated && user?.email && !fetchInitiated) {
6570
// Log successful authentication
6671
Logger.logEvent('UserAuthenticated', {
6772
userId: user.sub,
6873
userEmail: user.email,
6974
timestamp: new Date().toISOString(),
7075
});
76+
77+
// Mark that we've initiated the fetch
78+
setFetchInitiated(true);
79+
7180
// Fetch user details
7281
fetchUserDetails(user.email, {
7382
onSuccess: userDetails => {
7483
setUserDetail({
75-
id: typeof userDetails.id === 'number' ? userDetails.id : 0,
76-
email: userDetails.email || '',
77-
username: userDetails.username || '',
78-
role: typeof userDetails.role === 'number' ? userDetails.role : 0,
79-
organization:
80-
typeof userDetails.organization === 'number' ? userDetails.organization : 0,
84+
id: userDetails.id,
85+
email: userDetails.email,
86+
username: userDetails.username,
87+
role: userDetails.role,
88+
organization: userDetails.organization,
8189
firstName: userDetails.firstName,
8290
lastName: userDetails.lastName,
8391
status: userDetails.status,
8492
});
93+
setUserDetailsFetched(true);
8594
},
8695
onError: error => {
8796
console.error('Failed to fetch user details:', error);
97+
98+
// Log the error
99+
Logger.logException(error instanceof Error ? error : new Error(String(error)), {
100+
source: 'FetchUserDetails',
101+
userEmail: user.email,
102+
});
103+
104+
// Reset states and redirect to login on error
105+
setFetchInitiated(false);
106+
setUserDetailsFetched(false);
107+
108+
// Redirect to login
109+
void loginWithRedirect({
110+
appState: {
111+
returnTo: window.location.pathname + window.location.search,
112+
},
113+
});
88114
},
89115
});
90116
}
91-
}, [isAuthenticated, isLoading, loginWithRedirect, user, fetchUserDetails, setUserDetail]);
117+
}, [
118+
isAuthenticated,
119+
isLoading,
120+
loginWithRedirect,
121+
user,
122+
fetchUserDetails,
123+
setUserDetail,
124+
fetchInitiated,
125+
]);
92126

93127
if (isLoading) {
94128
return (
@@ -116,6 +150,19 @@ export function App() {
116150
);
117151
}
118152

153+
if (isFetchingUserDetails || !userDetailsFetched) {
154+
return (
155+
<ErrorBoundary>
156+
<div className='flex h-screen items-center justify-center bg-gray-50'>
157+
<div className='text-center'>
158+
<div className='mx-auto mb-4 h-12 w-12 animate-spin rounded-full border-b-2 border-blue-600'></div>
159+
<p className='text-lg text-gray-600'>Loading user details...</p>
160+
</div>
161+
</div>
162+
</ErrorBoundary>
163+
);
164+
}
165+
119166
return (
120167
<ErrorBoundary>
121168
<div className='flex h-screen flex-col overflow-hidden'>

src/components/counter/index.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/components/counter/useCounter.ts

Whitespace-only changes.

src/components/header/UserMenu.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import { useAppStore } from '@/store/store';
88

99
import MenuItem from './MenuItem';
1010

11-
// interface User {
12-
// displayName: string;
13-
// email?: string;
14-
// }
15-
1611
interface UserMenuProps {
17-
// user: User;
1812
isOpen: boolean;
1913
onClose: () => void;
2014
onEditProfile: () => void;
@@ -26,7 +20,6 @@ const UserMenu: React.FC<UserMenuProps> = ({ isOpen, onClose, onEditProfile }) =
2620
const { userdetail } = useAppStore();
2721
const { t } = useTranslation();
2822

29-
// const [isDropdownOpen, setIsDropdownOpen] = useState(false);
3023
const menuRef = useRef<HTMLDivElement>(null);
3124

3225
useEffect(() => {

src/components/navigation.tsx

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/hooks/useUsers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const useCreateUser = () => {
7373
createUser(userData, email),
7474
onSuccess: () => {
7575
// Invalidate and refetch users list
76-
void void void void queryClient.invalidateQueries({ queryKey: ['users'] });
76+
void queryClient.invalidateQueries({ queryKey: ['users'] });
7777
},
7878
onError: error => {
7979
console.error('Error creating user:', error);

src/layouts/about/index.tsx

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/layouts/callback.tsx

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/layouts/users/UsersWrapper.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export const UsersWrapper: React.FC = () => {
1818
const handleSaveUser = async (userData: User | Omit<User, 'id'>) => {
1919
try {
2020
if (modalMode === 'create') {
21-
(userData.organization = userdetail?.organization ?? 0),
22-
(userData.createdBy = userdetail?.id ?? 0),
23-
(userData.isActive = true),
24-
// Creating new user - pass both userData and email as an object
25-
await createUserMutation.mutateAsync({
26-
userData: userData as Omit<User, 'id'>,
27-
email: userdetail ? userdetail.email : '',
28-
});
21+
userData.organization = userdetail?.organization ?? 0;
22+
userData.createdBy = userdetail?.id ?? 0;
23+
userData.isActive = true;
24+
// Creating new user - pass both userData and email as an object
25+
await createUserMutation.mutateAsync({
26+
userData: userData as Omit<User, 'id'>,
27+
email: userdetail ? userdetail.email : '',
28+
});
2929
} else {
3030
// Updating existing user - pass both userData and email as an object
3131
await updateUserMutation.mutateAsync({

src/routes/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
import { Router } from '@tanstack/react-router';
22

33
import {
4-
aboutRoute,
54
appInsightsTestRoute,
6-
callbackRoute,
75
indexRoute,
86
rootRoute,
97
tailwindTestRoute,
10-
userInfoRoute,
118
userListRoute,
129
} from './route-definitions';
1310

1411
const routeTree = rootRoute.addChildren([
1512
indexRoute,
16-
aboutRoute,
1713
tailwindTestRoute,
1814
appInsightsTestRoute,
19-
callbackRoute,
20-
userInfoRoute,
2115
userListRoute,
2216
]);
2317

0 commit comments

Comments
 (0)