Skip to content

Commit ba687c1

Browse files
authored
Feature/ns to gw redirect (#1162)
* fix some capitlization * fix no gw redirect handling and add toast
1 parent 43994fa commit ba687c1

File tree

5 files changed

+82
-13
lines changed

5 files changed

+82
-13
lines changed

e2e/cypress/tests/20-gateways/01-list.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ describe('My Gateways list page', () => {
4747
});
4848
})
4949

50+
it('Verify redirect to My Gateways page if no gateway selected', () => {
51+
cy.visit(ns.detailPath)
52+
cy.wait(2000)
53+
cy.verifyToastMessage('First select a Gateway to view that page')
54+
})
55+
5056
it('Check Gateway link goes to details page', () => {
5157
cy.visit(ns.listPath)
5258
cy.get(`[data-testid="ns-list-activate-link-${gateways["namespace1"].gatewayId + '-' + customId}"]`).click()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as React from 'react';
2+
import { useToast } from '@chakra-ui/react';
3+
import { useRouter } from 'next/router';
4+
5+
const GatewayToastHandler = () => {
6+
const toast = useToast();
7+
const router = useRouter();
8+
9+
React.useEffect(() => {
10+
const handleRouteChange = () => {
11+
const showToast = localStorage.getItem('showNoGatewayToast');
12+
if (showToast === 'true') {
13+
toast.closeAll();
14+
toast({
15+
title: `First select a Gateway to view that page`,
16+
status: 'error',
17+
isClosable: true,
18+
duration: 5000,
19+
});
20+
localStorage.removeItem('showNoGatewayToast');
21+
}
22+
};
23+
24+
router.events.on('routeChangeComplete', handleRouteChange);
25+
26+
return () => {
27+
router.events.off('routeChangeComplete', handleRouteChange);
28+
};
29+
}, [toast, router]);
30+
31+
return null;
32+
};
33+
34+
export default GatewayToastHandler;

src/nextapp/components/no-gateway-redirect/no-gateway-redirect.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,41 @@ import { useAuth } from '@/shared/services/auth';
55
const NoGatewayRedirect = () => {
66
const router = useRouter();
77
const { user } = useAuth();
8-
const hasNamespace = !!user?.namespace;
8+
const [isChecking, setIsChecking] = React.useState(true);
99

1010
React.useEffect(() => {
11-
if (!hasNamespace) {
12-
router.push('/manager/gateways/list');
13-
}
14-
}, [hasNamespace]);
15-
return null
11+
const checkNamespaceAndRedirect = async () => {
12+
// Wait for a short period to ensure user data is loaded
13+
await new Promise(resolve => setTimeout(resolve, 2000));
14+
15+
setIsChecking(false);
16+
17+
if (!user?.namespace) {
18+
try {
19+
// Set localStorage item to show toast
20+
await new Promise<void>((resolve, reject) => {
21+
try {
22+
localStorage.setItem('showNoGatewayToast', 'true');
23+
resolve();
24+
} catch (error) {
25+
reject(error);
26+
}
27+
});
28+
await router.push('/manager/gateways/list');
29+
} catch (error) {
30+
console.error('Error during redirect process:', error);
31+
}
32+
}
33+
};
34+
35+
checkNamespaceAndRedirect();
36+
}, [user, router]);
37+
38+
if (isChecking) {
39+
return null; // could return a loading indicator
40+
}
41+
42+
return null;
1643
};
1744

18-
export default NoGatewayRedirect;
45+
export default NoGatewayRedirect;

src/nextapp/pages/_app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import '@/shared/styles/global.css';
3131
import { AppWrapper } from './context';
3232
import '../../mocks';
3333
import CompleteProfile from '@/components/complete-profile';
34+
import GatewayToastHandler from '@/components/no-gateway-redirect/gateway-toast-handler';
3435

3536
const footerItems = [
3637
{ href: 'http://www2.gov.bc.ca/gov/content/home', text: 'Home' },
@@ -122,6 +123,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
122123
}}
123124
>
124125
<AppWrapper router={router}>
126+
<GatewayToastHandler />
125127
<Component {...pageProps} />
126128
</AppWrapper>
127129
</Box>

src/nextapp/pages/manager/gateways/list.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const MyGatewaysPage: React.FC = () => {
106106
const handleNamespaceChange = React.useCallback(
107107
(namespace: Namespace) => async () => {
108108
toast({
109-
title: `Switching to gateway: ${namespace.displayName}`,
109+
title: `Switching to Gateway: ${namespace.displayName}`,
110110
status: 'info',
111111
isClosable: true,
112112
});
@@ -116,15 +116,15 @@ const MyGatewaysPage: React.FC = () => {
116116
toast.closeAll();
117117
client.invalidateQueries();
118118
toast({
119-
title: `Switched to gateway: ${namespace.displayName}`,
119+
title: `Switched to Gateway: ${namespace.displayName}`,
120120
status: 'success',
121121
isClosable: true,
122122
});
123123
await router.push('/manager/gateways/detail');
124124
} catch (err) {
125125
toast.closeAll();
126126
toast({
127-
title: 'Unable to switch gateways',
127+
title: 'Unable to switch Gateways',
128128
status: 'error',
129129
isClosable: true,
130130
});
@@ -264,11 +264,11 @@ const MyGatewaysPage: React.FC = () => {
264264
</Flex>
265265
{isSuccess &&
266266
(namespaceSearchResults.length === 1 ? (
267-
<Text mb={4}>{namespaceSearchResults.length} gateway</Text>
267+
<Text mb={4}>{namespaceSearchResults.length} Gateway</Text>
268268
) : (
269-
<Text mb={4}>{namespaceSearchResults.length} gateways</Text>
269+
<Text mb={4}>{namespaceSearchResults.length} Gateways</Text>
270270
))}
271-
{isLoading && <Text mb={4}>Loading gateways...</Text>}
271+
{isLoading && <Text mb={4}>Loading Gateways...</Text>}
272272
{isError && <Text mb={4}>Gateways failed to load</Text>}
273273
{isSuccess && (
274274
<>

0 commit comments

Comments
 (0)