Simplified EditRecipe page but still cannot figure out re-render bug.
This commit is contained in:
parent
5d1def13db
commit
21c154ae47
@ -1,13 +1,12 @@
|
|||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { useNavigate } from '@tanstack/react-router'
|
|
||||||
import { ChangeEventHandler, FormEventHandler, PropsWithChildren, useEffect, useState } from 'react'
|
import { ChangeEventHandler, FormEventHandler, PropsWithChildren, useEffect, useState } from 'react'
|
||||||
import { ApiError } from '../../api/ApiError'
|
import { ApiError } from '../../api/ApiError'
|
||||||
import getRecipe from '../../api/getRecipe'
|
import getRecipe from '../../api/getRecipe'
|
||||||
import UpdateRecipeSpec, { fromFullRecipeView } from '../../api/types/UpdateRecipeSpec'
|
import UpdateRecipeSpec, { fromFullRecipeView } from '../../api/types/UpdateRecipeSpec'
|
||||||
import updateRecipe from '../../api/updateRecipe'
|
import updateRecipe from '../../api/updateRecipe'
|
||||||
import { useAuth } from '../../AuthProvider'
|
import { useAuth } from '../../AuthProvider'
|
||||||
import classes from './edit-recipe.module.css'
|
|
||||||
import { useRefresh } from '../../RefreshProvider'
|
import { useRefresh } from '../../RefreshProvider'
|
||||||
|
import classes from './edit-recipe.module.css'
|
||||||
|
|
||||||
interface ControlProps {
|
interface ControlProps {
|
||||||
id: string
|
id: string
|
||||||
@ -88,19 +87,17 @@ export interface EditRecipeProps {
|
|||||||
|
|
||||||
const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
||||||
const { accessToken } = useAuth()
|
const { accessToken } = useAuth()
|
||||||
const navigate = useNavigate()
|
|
||||||
const refresh = useRefresh()
|
const refresh = useRefresh()
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// if (auth.token === null) {
|
|
||||||
// navigate({
|
|
||||||
// to: '/login',
|
|
||||||
// search: { reason: 'NOT_LOGGED_IN', redirect: `/recipes/${username}/${slug}/edit` }
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// }, [auth.token, navigate, username, slug])
|
|
||||||
|
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
const [spec, setSpec] = useState<UpdateRecipeSpec>({
|
||||||
|
title: '',
|
||||||
|
preparationTime: null,
|
||||||
|
cookingTime: null,
|
||||||
|
totalTime: null,
|
||||||
|
rawText: '',
|
||||||
|
mainImage: null,
|
||||||
|
isPublic: false
|
||||||
|
})
|
||||||
|
|
||||||
const recipeQuery = useQuery(
|
const recipeQuery = useQuery(
|
||||||
{
|
{
|
||||||
@ -118,25 +115,9 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
|||||||
queryClient
|
queryClient
|
||||||
)
|
)
|
||||||
|
|
||||||
const [isOwner, setIsOwner] = useState(false)
|
|
||||||
|
|
||||||
const [spec, setSpec] = useState<UpdateRecipeSpec>({
|
|
||||||
title: '',
|
|
||||||
preparationTime: null,
|
|
||||||
cookingTime: null,
|
|
||||||
totalTime: null,
|
|
||||||
rawText: '',
|
|
||||||
mainImage: null,
|
|
||||||
isPublic: false
|
|
||||||
})
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (recipeQuery.isSuccess) {
|
if (recipeQuery.isSuccess) {
|
||||||
const { isOwner, recipe } = recipeQuery.data
|
setSpec(fromFullRecipeView(recipeQuery.data.recipe))
|
||||||
setIsOwner(isOwner ?? false)
|
|
||||||
if (isOwner) {
|
|
||||||
setSpec(fromFullRecipeView(recipe))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [recipeQuery.isSuccess, recipeQuery.data])
|
}, [recipeQuery.isSuccess, recipeQuery.data])
|
||||||
|
|
||||||
@ -156,11 +137,9 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSuccess: data => {
|
onSuccess: data => {
|
||||||
setIsOwner(data.isOwner ?? false)
|
console.log(data)
|
||||||
setSpec(fromFullRecipeView(data.recipe))
|
setSpec(fromFullRecipeView(data.recipe))
|
||||||
queryClient.invalidateQueries({
|
queryClient.setQueryData(['recipes', username, slug], data)
|
||||||
queryKey: ['recipes', username, slug]
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
queryClient
|
queryClient
|
||||||
@ -196,9 +175,11 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
|||||||
setSpec(next)
|
setSpec(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recipeQuery.isLoading) {
|
if (recipeQuery.isPending) {
|
||||||
|
console.log('we are pending')
|
||||||
return 'Loading...'
|
return 'Loading...'
|
||||||
} else if (recipeQuery.isError) {
|
} else if (recipeQuery.isError) {
|
||||||
|
console.log('we had an error')
|
||||||
const { error } = recipeQuery
|
const { error } = recipeQuery
|
||||||
if (error instanceof ApiError) {
|
if (error instanceof ApiError) {
|
||||||
if (error.status === 404) {
|
if (error.status === 404) {
|
||||||
@ -209,9 +190,11 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
|||||||
} else {
|
} else {
|
||||||
return `Error: ${error.name} ${error.message}`
|
return `Error: ${error.name} ${error.message}`
|
||||||
}
|
}
|
||||||
} else if (!isOwner) {
|
} else if (recipeQuery.isSuccess && !recipeQuery.data.isOwner) {
|
||||||
|
console.log('we are not the owner')
|
||||||
return 'You do not have permission to edit this recipe.'
|
return 'You do not have permission to edit this recipe.'
|
||||||
} else {
|
} else {
|
||||||
|
console.log('doing whole page')
|
||||||
return (
|
return (
|
||||||
<div className={classes.articleContainer}>
|
<div className={classes.articleContainer}>
|
||||||
<article>
|
<article>
|
||||||
@ -242,7 +225,11 @@ const EditRecipe = ({ username, slug }: EditRecipeProps) => {
|
|||||||
</Control>
|
</Control>
|
||||||
|
|
||||||
<Control id="recipe-text" displayName="Recipe Text">
|
<Control id="recipe-text" displayName="Recipe Text">
|
||||||
<textarea value={spec.rawText} onChange={getSetSpecTextAsHandler('rawText')} />
|
<textarea
|
||||||
|
id="recipe-text"
|
||||||
|
value={spec.rawText}
|
||||||
|
onChange={getSetSpecTextAsHandler('rawText')}
|
||||||
|
/>
|
||||||
</Control>
|
</Control>
|
||||||
|
|
||||||
<div className={classes.submitContainer}>
|
<div className={classes.submitContainer}>
|
||||||
|
Loading…
Reference in New Issue
Block a user