Skip to content

Commit

Permalink
Use the router mathFilter for validating
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm committed Oct 28, 2023
1 parent cfd668e commit 943d7a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { homeRouteData } from './state/home'
const ROUTES: RouteDefinition[] = [
{
path: '/posts/:id',
matchFilters: {
id: (id: string) => Number.isSafeInteger(parseInt(id)),
},
component: lazy(() => import('~/pages/Post')),
data: postRouteData,
},
Expand Down
12 changes: 3 additions & 9 deletions src/state/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@ type Response = {
post: Post
}

export const postRouteData = ({ params, navigate }: RouteDataArgs) => {
const parsedId = parseInt(params.id)

if (!Number.isSafeInteger(parsedId)) {
navigate('/404')
return
}

export const postRouteData = ({ params }: RouteDataArgs) => {
const id = Number(params.id)
const newQuery = createGraphQLClient(GRAPHQL_BACKEND_URL)

const [data] = newQuery<Response>(
QUERY,
{
id: parsedId,
id,
},
{
deferStream: true,
Expand Down

0 comments on commit 943d7a4

Please sign in to comment.