Skip to content

Commit e7b4e5c

Browse files
authored
Improve loading behavior for Sidebar (#49)
* add a loading skeleton to the sidebar while the permissions load * update event creation text
1 parent 4cdd1c9 commit e7b4e5c

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/ui/components/AppShell/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Group,
55
LoadingOverlay,
66
NavLink,
7+
Skeleton,
78
Text,
89
useMantineColorScheme,
910
} from '@mantine/core';
@@ -142,6 +143,7 @@ export const renderNavItems = (
142143
resourceDef={{ service: 'core', validRoles: item.validRoles }}
143144
isAppShell={false}
144145
key={`${item.name}-wrap`}
146+
loadingSkeleton={<Skeleton h={48} style={{ borderRadius: 5 }} mt="sm"></Skeleton>}
145147
>
146148
{link}
147149
</AuthGuard>

src/ui/components/AuthGuard/index.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type ResourceDefinition = {
2323
const getAuthCacheKey = (service: ValidService, route: string) =>
2424
`${CACHE_KEY_PREFIX}${service}_${route}`;
2525

26-
const getCachedResponse = (service: ValidService, route: string): CacheData | null => {
26+
export const getCachedResponse = (service: ValidService, route: string): CacheData | null => {
2727
const cached = sessionStorage.getItem(getAuthCacheKey(service, route));
2828
if (!cached) return null;
2929

@@ -65,14 +65,16 @@ export const AuthGuard: React.FC<
6565
resourceDef: ResourceDefinition;
6666
children: ReactNode;
6767
isAppShell?: boolean;
68+
loadingSkeleton?: ReactNode;
6869
} & AcmAppShellProps
69-
> = ({ resourceDef, children, isAppShell = true, ...appShellProps }) => {
70+
> = ({ resourceDef, children, isAppShell = true, loadingSkeleton, ...appShellProps }) => {
7071
const { service, validRoles } = resourceDef;
7172
const { baseEndpoint, authCheckRoute, friendlyName } =
7273
getRunEnvironmentConfig().ServiceConfiguration[service];
7374
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
7475
const [username, setUsername] = useState<string | null>(null);
7576
const [roles, setRoles] = useState<string[] | null>(null);
77+
const [isLoading, setIsLoading] = useState<boolean>(true);
7678
const api = useApi(service);
7779

7880
useEffect(() => {
@@ -88,6 +90,7 @@ export const AuthGuard: React.FC<
8890
}
8991

9092
// Check for cached response first
93+
setIsLoading(true);
9194
const cachedData = getCachedResponse(service, authCheckRoute);
9295
if (cachedData !== null) {
9396
const userRoles = cachedData.data.roles;
@@ -101,6 +104,7 @@ export const AuthGuard: React.FC<
101104
setUsername(cachedData.data.username);
102105
setRoles(cachedData.data.roles);
103106
setIsAuthenticated(authenticated);
107+
setIsLoading(false);
104108
return;
105109
}
106110

@@ -120,15 +124,19 @@ export const AuthGuard: React.FC<
120124
setIsAuthenticated(authenticated);
121125
setRoles(result.data.roles);
122126
setUsername(result.data.username);
127+
setIsLoading(false);
123128
} catch (e) {
124129
setIsAuthenticated(false);
130+
setIsLoading(false);
125131
console.error(e);
126132
}
127133
}
128134

129135
getAuth();
130136
}, [baseEndpoint, authCheckRoute, service]);
131-
137+
if (isLoading && loadingSkeleton) {
138+
return loadingSkeleton;
139+
}
132140
if (isAuthenticated === null) {
133141
if (isAppShell) {
134142
return <FullScreenLoader />;

src/ui/pages/events/ManageEvent.page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ export const ManageEventPage: React.FC = () => {
155155

156156
return (
157157
<AuthGuard resourceDef={{ service: 'core', validRoles: [AppRoles.EVENTS_MANAGER] }}>
158-
<Title order={2}>{isEditing ? `Edit` : `Add`} Event</Title>
159158
<Box maw={400} mx="auto" mt="xl">
159+
<Title mb="sm" order={2}>
160+
{isEditing ? `Edit` : `Create`} Event
161+
</Title>
160162
<form onSubmit={form.onSubmit(handleSubmit)}>
161163
<TextInput
162164
label="Event Title"

0 commit comments

Comments
 (0)