Skip to content

Commit

Permalink
route level error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Oct 30, 2024
1 parent 5ca095e commit bd64614
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/manager/src/MainContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Box } from '@linode/ui';
import Grid from '@mui/material/Unstable_Grid2';
import { useQueryClient } from '@tanstack/react-query';
import { RouterProvider } from '@tanstack/react-router';
import * as React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';
Expand Down Expand Up @@ -199,6 +200,7 @@ export const MainContent = () => {
const { classes, cx } = useStyles();
const { data: preferences } = usePreferences();
const { mutateAsync: updatePreferences } = useMutatePreferences();
const queryClient = useQueryClient();

const globalErrors = useGlobalErrors();

Expand Down Expand Up @@ -370,6 +372,7 @@ export const MainContent = () => {
*/}
<Route path="*">
<RouterProvider
context={{ queryClient }}
router={migrationRouter as AnyRouter}
/>
</Route>
Expand Down
2 changes: 2 additions & 0 deletions packages/manager/src/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QueryClient } from '@tanstack/react-query';
import { RouterProvider } from '@tanstack/react-router';
import * as React from 'react';

Expand All @@ -24,6 +25,7 @@ export const Router = () => {
isACLPEnabled,
isDatabasesEnabled,
isPlacementGroupsEnabled,
queryClient: new QueryClient(),
},
});

Expand Down
9 changes: 7 additions & 2 deletions packages/manager/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QueryClient } from '@tanstack/react-query';
import { createRoute, createRouter, redirect } from '@tanstack/react-router';
import React from 'react';

Expand Down Expand Up @@ -62,7 +63,9 @@ export const routeTree = rootRoute.addChildren([
]);

export const router = createRouter({
context: {},
context: {
queryClient: new QueryClient(),
},
defaultNotFoundComponent: () => <NotFound />,
routeTree,
});
Expand All @@ -89,7 +92,9 @@ export const migrationRouter = createRouter({
Wrap: ({ children }) => {
return <div data-testid="migration-router">{children}</div>;
},
context: {},
context: {
queryClient: new QueryClient(),
},
defaultNotFoundComponent: () => <NotFound />,
routeTree: migrationRouteTree,
});
2 changes: 2 additions & 0 deletions packages/manager/src/routes/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AccountSettings } from '@linode/api-v4';
import type { QueryClient } from '@tanstack/react-query';

export type RouterContext = {
accountSettings?: AccountSettings;
Expand All @@ -8,6 +9,7 @@ export type RouterContext = {
isACLPEnabled?: boolean;
isDatabasesEnabled?: boolean;
isPlacementGroupsEnabled?: boolean;
queryClient: QueryClient;
};

export interface TableSearchParams {
Expand Down
27 changes: 26 additions & 1 deletion packages/manager/src/routes/volumes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createRoute, redirect } from '@tanstack/react-router';

import { volumeQueries } from 'src/queries/volumes/volumes';

import { rootRoute } from '../root';
import { VolumesRoot } from './VolumesRoot';

Expand Down Expand Up @@ -46,14 +48,36 @@ const volumesCreateRoute = createRoute({
);

const volumeActionRoute = createRoute({
beforeLoad: ({ params }) => {
beforeLoad: async ({ context, params, search }) => {
if (!(params.action in volumeAction)) {
throw redirect({
search: () => ({}),
to: '/volumes',
});
}

const volumes = await context.queryClient.fetchQuery(
volumeQueries.lists._ctx.paginated(
{
page: 1,
page_size: 25,
},
{
'+order': search?.order ?? 'asc',
'+order_by': search?.orderBy ?? 'label',
}
)
);

// if the volume is not found, redirect to the volumes landing page
if (!volumes.data.find((v) => v.id === params.volumeId)) {
throw redirect({
search: () => ({}),
to: '/volumes',
});
}
},

getParentRoute: () => volumesRoute,
parseParams: ({
action,
Expand All @@ -66,6 +90,7 @@ const volumeActionRoute = createRoute({
volumeId: Number(volumeId),
}),
path: '$volumeId/$action',
validateSearch: (search: VolumesSearchParams) => search,
}).lazy(() =>
import('src/routes/volumes/volumesLazyRoutes').then(
(m) => m.volumesLandingLazyRoute
Expand Down

0 comments on commit bd64614

Please sign in to comment.