Now using rawText from recipe api.
This commit is contained in:
parent
8ce916731f
commit
56dffa2ee8
@ -1,33 +1,62 @@
|
||||
import { AuthContextType } from '../auth'
|
||||
import { ApiError } from './ApiError'
|
||||
import ExpiredTokenError from './ExpiredTokenError'
|
||||
import FullRecipeView, { RawFullRecipeView, toFullRecipeView } from './types/FullRecipeView'
|
||||
import GetRecipeView, { RawGetRecipeView, toGetRecipeView } from './types/GetRecipeView'
|
||||
import GetRecipeView, {
|
||||
GetRecipeViewWithRawText,
|
||||
RawGetRecipeView,
|
||||
RawGetRecipeViewWithRawText,
|
||||
toGetRecipeView,
|
||||
toGetRecipeViewWithRawText
|
||||
} from './types/GetRecipeView'
|
||||
|
||||
export interface GetRecipeDeps {
|
||||
export interface GetRecipeCommonDeps {
|
||||
authContext: AuthContextType
|
||||
username: string
|
||||
slug: string
|
||||
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()
|
||||
if (authContext.token !== null) {
|
||||
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,
|
||||
headers,
|
||||
mode: 'cors'
|
||||
})
|
||||
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) {
|
||||
throw new ExpiredTokenError()
|
||||
} else {
|
||||
throw new ApiError(response.status, response.statusText)
|
||||
}
|
||||
}
|
||||
}) as GetRecipe
|
||||
|
||||
export default getRecipe
|
||||
|
@ -18,6 +18,10 @@ export interface RawFullRecipeView {
|
||||
isPublic: boolean
|
||||
}
|
||||
|
||||
export interface RawFullRecipeViewWithRawText extends RawFullRecipeView {
|
||||
rawText: string
|
||||
}
|
||||
|
||||
interface FullRecipeView {
|
||||
id: number
|
||||
created: Date
|
||||
@ -35,6 +39,10 @@ interface FullRecipeView {
|
||||
isPublic: boolean
|
||||
}
|
||||
|
||||
export interface FullRecipeViewWithRawText extends FullRecipeView {
|
||||
rawText: string
|
||||
}
|
||||
|
||||
export const toFullRecipeView = ({
|
||||
id,
|
||||
created: rawCreated,
|
||||
@ -67,4 +75,9 @@ export const toFullRecipeView = ({
|
||||
isPublic
|
||||
})
|
||||
|
||||
export const toFullRecipeViewWithRawText = (raw: RawFullRecipeViewWithRawText): FullRecipeViewWithRawText => ({
|
||||
rawText: raw.rawText,
|
||||
...toFullRecipeView(raw)
|
||||
})
|
||||
|
||||
export default FullRecipeView
|
||||
|
@ -1,4 +1,10 @@
|
||||
import FullRecipeView, { RawFullRecipeView, toFullRecipeView } from './FullRecipeView'
|
||||
import FullRecipeView, {
|
||||
FullRecipeViewWithRawText,
|
||||
RawFullRecipeView,
|
||||
RawFullRecipeViewWithRawText,
|
||||
toFullRecipeView,
|
||||
toFullRecipeViewWithRawText
|
||||
} from './FullRecipeView'
|
||||
|
||||
export interface RawGetRecipeView {
|
||||
recipe: RawFullRecipeView
|
||||
@ -6,16 +12,34 @@ export interface RawGetRecipeView {
|
||||
isOwner: boolean | null
|
||||
}
|
||||
|
||||
export interface RawGetRecipeViewWithRawText extends RawGetRecipeView {
|
||||
recipe: RawFullRecipeViewWithRawText
|
||||
}
|
||||
|
||||
interface GetRecipeView {
|
||||
recipe: FullRecipeView
|
||||
isStarred: boolean | null
|
||||
isOwner: boolean | null
|
||||
}
|
||||
|
||||
export interface GetRecipeViewWithRawText extends GetRecipeView {
|
||||
recipe: FullRecipeViewWithRawText
|
||||
}
|
||||
|
||||
export const toGetRecipeView = ({ recipe, isStarred, isOwner }: RawGetRecipeView): GetRecipeView => ({
|
||||
recipe: toFullRecipeView(recipe),
|
||||
isStarred,
|
||||
isOwner
|
||||
})
|
||||
|
||||
export const toGetRecipeViewWithRawText = ({
|
||||
recipe,
|
||||
isStarred,
|
||||
isOwner
|
||||
}: RawGetRecipeViewWithRawText): GetRecipeViewWithRawText => ({
|
||||
recipe: toFullRecipeViewWithRawText(recipe),
|
||||
isStarred,
|
||||
isOwner
|
||||
})
|
||||
|
||||
export default GetRecipeView
|
||||
|
@ -101,7 +101,8 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
||||
authContext: auth,
|
||||
username,
|
||||
slug,
|
||||
abortSignal: signal
|
||||
abortSignal: signal,
|
||||
includeRawText: true
|
||||
})
|
||||
},
|
||||
queryClient
|
||||
@ -127,7 +128,7 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
||||
setPreparationTime(recipe.preparationTime)
|
||||
setCookingTime(recipe.cookingTime)
|
||||
setTotalTime(recipe.totalTime)
|
||||
setRecipeText(recipe.text)
|
||||
setRecipeText(recipe.rawText)
|
||||
}
|
||||
}
|
||||
}, [recipeQuery.isSuccess, recipeQuery.data])
|
||||
|
Loading…
Reference in New Issue
Block a user