Switched to using AbortSignal in api deps. Recipes page updated.

This commit is contained in:
Jesse Brault 2024-07-31 13:18:11 -05:00
parent 647a4be383
commit 8a5fd0a466
4 changed files with 30 additions and 13 deletions

View File

@ -7,21 +7,23 @@ export interface GetRecipeDeps {
token: string | null token: string | null
username: string username: string
slug: string slug: string
abortController: AbortController abortSignal: AbortSignal
} }
const getRecipe = async ( const getRecipe = async ({
token: string | null, token,
ownerUsername: string, username,
slug: string slug,
): Promise<FullRecipeView> => { abortSignal
}: GetRecipeDeps): Promise<FullRecipeView> => {
const headers = new Headers() const headers = new Headers()
if (token !== null) { if (token !== null) {
headers.set('Authorization', `Bearer ${token}`) headers.set('Authorization', `Bearer ${token}`)
} }
const response = await fetch( const response = await fetch(
import.meta.env.VITE_MME_API_URL + `/recipes/${ownerUsername}/${slug}`, import.meta.env.VITE_MME_API_URL + `/recipes/${username}/${slug}`,
{ {
signal: abortSignal,
headers, headers,
mode: 'cors' mode: 'cors'
} }

View File

@ -2,11 +2,19 @@ import { ApiError } from './ApiError'
import { toImageView } from './types/ImageView' import { toImageView } from './types/ImageView'
import RecipeInfosView, { RawRecipeInfosView } from './types/RecipeInfosView' import RecipeInfosView, { RawRecipeInfosView } from './types/RecipeInfosView'
const getRecipeInfos = async ( export interface GetRecipeInfosDeps {
token: string | null, abortSignal: AbortSignal
pageNumber: number, token: string | null
pageNumber: number
pageSize: number pageSize: number
): Promise<RecipeInfosView> => { }
const getRecipeInfos = async ({
abortSignal,
token,
pageNumber,
pageSize
}: GetRecipeInfosDeps): Promise<RecipeInfosView> => {
const headers = new Headers() const headers = new Headers()
if (token !== null) { if (token !== null) {
headers.set('Authorization', `Bearer ${token}`) headers.set('Authorization', `Bearer ${token}`)
@ -15,6 +23,7 @@ const getRecipeInfos = async (
import.meta.env.VITE_MME_API_URL + import.meta.env.VITE_MME_API_URL +
`/recipes?page=${pageNumber}&size=${pageSize}`, `/recipes?page=${pageNumber}&size=${pageSize}`,
{ {
signal: abortSignal,
headers, headers,
mode: 'cors' mode: 'cors'
} }

View File

@ -13,7 +13,13 @@ const Recipes = () => {
const { data, isPending, error } = useQuery({ const { data, isPending, error } = useQuery({
queryKey: ['recipeInfos'], queryKey: ['recipeInfos'],
queryFn: () => getRecipeInfos(token, pageNumber, pageSize) queryFn: ({ signal }) =>
getRecipeInfos({
abortSignal: signal,
pageNumber,
pageSize,
token
})
}) })
if (isPending) { if (isPending) {

View File

@ -12,7 +12,7 @@ export const Route = createFileRoute('/recipes/$username/$slug')({
queryKey: ['recipe', params.username, params.slug], queryKey: ['recipe', params.username, params.slug],
queryFn: () => queryFn: () =>
getRecipe({ getRecipe({
abortController, abortSignal: abortController.signal,
token: context.auth.token, token: context.auth.token,
username: params.username, username: params.username,
slug: params.slug slug: params.slug