Now using rawText from recipe api.

This commit is contained in:
Jesse Brault 2024-08-15 14:19:22 -05:00
parent 8ce916731f
commit 56dffa2ee8
4 changed files with 77 additions and 10 deletions

View File

@ -1,33 +1,62 @@
import { AuthContextType } from '../auth' import { AuthContextType } from '../auth'
import { ApiError } from './ApiError' import { ApiError } from './ApiError'
import ExpiredTokenError from './ExpiredTokenError' import ExpiredTokenError from './ExpiredTokenError'
import FullRecipeView, { RawFullRecipeView, toFullRecipeView } from './types/FullRecipeView' import GetRecipeView, {
import GetRecipeView, { RawGetRecipeView, toGetRecipeView } from './types/GetRecipeView' GetRecipeViewWithRawText,
RawGetRecipeView,
RawGetRecipeViewWithRawText,
toGetRecipeView,
toGetRecipeViewWithRawText
} from './types/GetRecipeView'
export interface GetRecipeDeps { export interface GetRecipeCommonDeps {
authContext: AuthContextType authContext: AuthContextType
username: string username: string
slug: string slug: string
abortSignal: AbortSignal abortSignal: AbortSignal
} }
const getRecipe = async ({ authContext, username, slug, abortSignal }: GetRecipeDeps): Promise<GetRecipeView> => { export interface GetRecipeDeps extends GetRecipeCommonDeps {
includeRawText?: false
}
export interface GetRecipeDepsIncludeRawText extends GetRecipeCommonDeps {
includeRawText: true
}
export interface GetRecipe {
(deps: GetRecipeDeps): Promise<GetRecipeView>
(deps: GetRecipeDepsIncludeRawText): Promise<GetRecipeViewWithRawText>
}
const getRecipe = (async ({
authContext,
username,
slug,
abortSignal,
includeRawText
}: GetRecipeDeps | GetRecipeDepsIncludeRawText): Promise<GetRecipeView | GetRecipeViewWithRawText> => {
const headers = new Headers() const headers = new Headers()
if (authContext.token !== null) { if (authContext.token !== null) {
headers.set('Authorization', `Bearer ${authContext.token}`) headers.set('Authorization', `Bearer ${authContext.token}`)
} }
const response = await fetch(import.meta.env.VITE_MME_API_URL + `/recipes/${username}/${slug}`, { const query = includeRawText ? '?includeRawText=true' : ''
const response = await fetch(import.meta.env.VITE_MME_API_URL + `/recipes/${username}/${slug}${query}`, {
signal: abortSignal, signal: abortSignal,
headers, headers,
mode: 'cors' mode: 'cors'
}) })
if (response.ok) { if (response.ok) {
return toGetRecipeView((await response.json()) as RawGetRecipeView) if (includeRawText) {
return toGetRecipeViewWithRawText((await response.json()) as RawGetRecipeViewWithRawText)
} else {
return toGetRecipeView((await response.json()) as RawGetRecipeView)
}
} else if (response.status === 401) { } else if (response.status === 401) {
throw new ExpiredTokenError() throw new ExpiredTokenError()
} else { } else {
throw new ApiError(response.status, response.statusText) throw new ApiError(response.status, response.statusText)
} }
} }) as GetRecipe
export default getRecipe export default getRecipe

View File

@ -18,6 +18,10 @@ export interface RawFullRecipeView {
isPublic: boolean isPublic: boolean
} }
export interface RawFullRecipeViewWithRawText extends RawFullRecipeView {
rawText: string
}
interface FullRecipeView { interface FullRecipeView {
id: number id: number
created: Date created: Date
@ -35,6 +39,10 @@ interface FullRecipeView {
isPublic: boolean isPublic: boolean
} }
export interface FullRecipeViewWithRawText extends FullRecipeView {
rawText: string
}
export const toFullRecipeView = ({ export const toFullRecipeView = ({
id, id,
created: rawCreated, created: rawCreated,
@ -67,4 +75,9 @@ export const toFullRecipeView = ({
isPublic isPublic
}) })
export const toFullRecipeViewWithRawText = (raw: RawFullRecipeViewWithRawText): FullRecipeViewWithRawText => ({
rawText: raw.rawText,
...toFullRecipeView(raw)
})
export default FullRecipeView export default FullRecipeView

View File

@ -1,4 +1,10 @@
import FullRecipeView, { RawFullRecipeView, toFullRecipeView } from './FullRecipeView' import FullRecipeView, {
FullRecipeViewWithRawText,
RawFullRecipeView,
RawFullRecipeViewWithRawText,
toFullRecipeView,
toFullRecipeViewWithRawText
} from './FullRecipeView'
export interface RawGetRecipeView { export interface RawGetRecipeView {
recipe: RawFullRecipeView recipe: RawFullRecipeView
@ -6,16 +12,34 @@ export interface RawGetRecipeView {
isOwner: boolean | null isOwner: boolean | null
} }
export interface RawGetRecipeViewWithRawText extends RawGetRecipeView {
recipe: RawFullRecipeViewWithRawText
}
interface GetRecipeView { interface GetRecipeView {
recipe: FullRecipeView recipe: FullRecipeView
isStarred: boolean | null isStarred: boolean | null
isOwner: boolean | null isOwner: boolean | null
} }
export interface GetRecipeViewWithRawText extends GetRecipeView {
recipe: FullRecipeViewWithRawText
}
export const toGetRecipeView = ({ recipe, isStarred, isOwner }: RawGetRecipeView): GetRecipeView => ({ export const toGetRecipeView = ({ recipe, isStarred, isOwner }: RawGetRecipeView): GetRecipeView => ({
recipe: toFullRecipeView(recipe), recipe: toFullRecipeView(recipe),
isStarred, isStarred,
isOwner isOwner
}) })
export const toGetRecipeViewWithRawText = ({
recipe,
isStarred,
isOwner
}: RawGetRecipeViewWithRawText): GetRecipeViewWithRawText => ({
recipe: toFullRecipeViewWithRawText(recipe),
isStarred,
isOwner
})
export default GetRecipeView export default GetRecipeView

View File

@ -101,7 +101,8 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
authContext: auth, authContext: auth,
username, username,
slug, slug,
abortSignal: signal abortSignal: signal,
includeRawText: true
}) })
}, },
queryClient queryClient
@ -127,7 +128,7 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
setPreparationTime(recipe.preparationTime) setPreparationTime(recipe.preparationTime)
setCookingTime(recipe.cookingTime) setCookingTime(recipe.cookingTime)
setTotalTime(recipe.totalTime) setTotalTime(recipe.totalTime)
setRecipeText(recipe.text) setRecipeText(recipe.rawText)
} }
} }
}, [recipeQuery.isSuccess, recipeQuery.data]) }, [recipeQuery.isSuccess, recipeQuery.data])