Basic 404 not found for Recipe.

This commit is contained in:
Jesse Brault 2024-07-31 13:09:52 -05:00
parent fa7d104bd2
commit 201ddb6656
2 changed files with 19 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import { notFound } from '@tanstack/react-router'
import { ApiError } from './ApiError' import { ApiError } from './ApiError'
import FullRecipeView, { RawFullRecipeView } from './types/FullRecipeView' import FullRecipeView, { RawFullRecipeView } from './types/FullRecipeView'
import { toImageView } from './types/ImageView' import { toImageView } from './types/ImageView'
@ -45,6 +46,8 @@ const getRecipe = async (
viewerCount, viewerCount,
mainImage: toImageView(rawMainImage) mainImage: toImageView(rawMainImage)
} }
} else if (response.status === 404) {
throw notFound()
} else { } else {
throw new ApiError(response.status, response.statusText) throw new ApiError(response.status, response.statusText)
} }

View File

@ -1,4 +1,8 @@
import { createFileRoute, useLoaderData } from '@tanstack/react-router' import {
createFileRoute,
useLoaderData,
useParams
} from '@tanstack/react-router'
import getRecipe from '../../api/getRecipe' import getRecipe from '../../api/getRecipe'
import Recipe from '../../pages/recipe/Recipe' import Recipe from '../../pages/recipe/Recipe'
@ -9,10 +13,20 @@ export const Route = createFileRoute('/recipes/$username/$slug')({
queryFn: () => queryFn: () =>
getRecipe(context.auth.token, params.username, params.slug) getRecipe(context.auth.token, params.username, params.slug)
}), }),
component: () => { component() {
const recipe = useLoaderData({ const recipe = useLoaderData({
from: '/recipes/$username/$slug' from: '/recipes/$username/$slug'
}) })
return <Recipe {...{ recipe }} /> return <Recipe {...{ recipe }} />
},
notFoundComponent() {
const { username, slug } = useParams({
from: '/recipes/$username/$slug'
})
return (
<p>
404: Could not find a Recipe for {username}/{slug}
</p>
)
} }
}) })