diff --git a/src/api/getRecipe.ts b/src/api/getRecipe.ts index b18aa2b..aace082 100644 --- a/src/api/getRecipe.ts +++ b/src/api/getRecipe.ts @@ -7,21 +7,23 @@ export interface GetRecipeDeps { token: string | null username: string slug: string - abortController: AbortController + abortSignal: AbortSignal } -const getRecipe = async ( - token: string | null, - ownerUsername: string, - slug: string -): Promise => { +const getRecipe = async ({ + token, + username, + slug, + abortSignal +}: GetRecipeDeps): Promise => { const headers = new Headers() if (token !== null) { headers.set('Authorization', `Bearer ${token}`) } 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, mode: 'cors' } diff --git a/src/api/getRecipeInfos.ts b/src/api/getRecipeInfos.ts index 99dd013..53fb17e 100644 --- a/src/api/getRecipeInfos.ts +++ b/src/api/getRecipeInfos.ts @@ -2,11 +2,19 @@ import { ApiError } from './ApiError' import { toImageView } from './types/ImageView' import RecipeInfosView, { RawRecipeInfosView } from './types/RecipeInfosView' -const getRecipeInfos = async ( - token: string | null, - pageNumber: number, +export interface GetRecipeInfosDeps { + abortSignal: AbortSignal + token: string | null + pageNumber: number pageSize: number -): Promise => { +} + +const getRecipeInfos = async ({ + abortSignal, + token, + pageNumber, + pageSize +}: GetRecipeInfosDeps): Promise => { const headers = new Headers() if (token !== null) { headers.set('Authorization', `Bearer ${token}`) @@ -15,6 +23,7 @@ const getRecipeInfos = async ( import.meta.env.VITE_MME_API_URL + `/recipes?page=${pageNumber}&size=${pageSize}`, { + signal: abortSignal, headers, mode: 'cors' } diff --git a/src/pages/recipes/Recipes.tsx b/src/pages/recipes/Recipes.tsx index d80d052..25528d3 100644 --- a/src/pages/recipes/Recipes.tsx +++ b/src/pages/recipes/Recipes.tsx @@ -13,7 +13,13 @@ const Recipes = () => { const { data, isPending, error } = useQuery({ queryKey: ['recipeInfos'], - queryFn: () => getRecipeInfos(token, pageNumber, pageSize) + queryFn: ({ signal }) => + getRecipeInfos({ + abortSignal: signal, + pageNumber, + pageSize, + token + }) }) if (isPending) { diff --git a/src/routes/recipes_/$username.$slug.tsx b/src/routes/recipes_/$username.$slug.tsx index 044fb76..bcc6197 100644 --- a/src/routes/recipes_/$username.$slug.tsx +++ b/src/routes/recipes_/$username.$slug.tsx @@ -12,7 +12,7 @@ export const Route = createFileRoute('/recipes/$username/$slug')({ queryKey: ['recipe', params.username, params.slug], queryFn: () => getRecipe({ - abortController, + abortSignal: abortController.signal, token: context.auth.token, username: params.username, slug: params.slug