Skip to content

Commit

Permalink
fix no gw redirect handling and add toast
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyjux committed Aug 30, 2024
1 parent 413257c commit fe30562
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
6 changes: 6 additions & 0 deletions e2e/cypress/tests/20-gateways/01-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('My Gateways list page', () => {
});
})

it('Verify redirect to My Gateways page if no gateway selected', () => {
cy.visit(ns.detailPath)
cy.wait(2000)
cy.verifyToastMessage('First select a Gateway to view that page')
})

it('Check Gateway link goes to details page', () => {
cy.visit(ns.listPath)
cy.get(`[data-testid="ns-list-activate-link-${gateways["namespace1"].gatewayId + '-' + customId}"]`).click()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from 'react';
import { useToast } from '@chakra-ui/react';
import { useRouter } from 'next/router';

const GatewayToastHandler = () => {
const toast = useToast();
const router = useRouter();

React.useEffect(() => {
const handleRouteChange = () => {
const showToast = localStorage.getItem('showNoGatewayToast');
if (showToast === 'true') {
toast.closeAll();
toast({
title: `First select a Gateway to view that page`,
status: 'error',
isClosable: true,
duration: 5000,
});
localStorage.removeItem('showNoGatewayToast');
}
};

router.events.on('routeChangeComplete', handleRouteChange);

return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [toast, router]);

return null;
};

export default GatewayToastHandler;
41 changes: 34 additions & 7 deletions src/nextapp/components/no-gateway-redirect/no-gateway-redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,41 @@ import { useAuth } from '@/shared/services/auth';
const NoGatewayRedirect = () => {
const router = useRouter();
const { user } = useAuth();
const hasNamespace = !!user?.namespace;
const [isChecking, setIsChecking] = React.useState(true);

React.useEffect(() => {
if (!hasNamespace) {
router.push('/manager/gateways/list');
}
}, [hasNamespace]);
return null
const checkNamespaceAndRedirect = async () => {
// Wait for a short period to ensure user data is loaded
await new Promise(resolve => setTimeout(resolve, 2000));

setIsChecking(false);

if (!user?.namespace) {
try {
// Set localStorage item to show toast
await new Promise<void>((resolve, reject) => {
try {
localStorage.setItem('showNoGatewayToast', 'true');
resolve();
} catch (error) {
reject(error);
}
});
await router.push('/manager/gateways/list');
} catch (error) {
console.error('Error during redirect process:', error);
}
}
};

checkNamespaceAndRedirect();
}, [user, router]);

if (isChecking) {
return null; // could return a loading indicator
}

return null;
};

export default NoGatewayRedirect;
export default NoGatewayRedirect;
2 changes: 2 additions & 0 deletions src/nextapp/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import '@/shared/styles/global.css';
import { AppWrapper } from './context';
import '../../mocks';
import CompleteProfile from '@/components/complete-profile';
import GatewayToastHandler from '@/components/no-gateway-redirect/gateway-toast-handler';

const footerItems = [
{ href: 'http://www2.gov.bc.ca/gov/content/home', text: 'Home' },
Expand Down Expand Up @@ -122,6 +123,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
}}
>
<AppWrapper router={router}>
<GatewayToastHandler />
<Component {...pageProps} />
</AppWrapper>
</Box>
Expand Down

0 comments on commit fe30562

Please sign in to comment.