@@ -18,6 +18,7 @@ import { useBlogPost, type BlogPost, type BlogPostInput } from "../hooks/useBlog
18
18
19
19
interface BlogPostFormProps {
20
20
previousPath ?: string ;
21
+ initialPost ?: BlogPost ;
21
22
}
22
23
23
24
const StyledBox = styled ( Box ) ( ( { theme } ) => ( {
@@ -29,16 +30,16 @@ const StyledBox = styled(Box)(({ theme }) => ({
29
30
} ,
30
31
} ) ) ;
31
32
32
- const BlogPostForm : React . FC < BlogPostFormProps > = ( { previousPath } ) => {
33
+ const BlogPostForm : React . FC < BlogPostFormProps > = ( { previousPath, initialPost } ) => {
33
34
const navigate = useNavigate ( ) ;
34
35
const { fetchBlogPost, createBlogPost, updateBlogPost } = useBlogPost ( ) ;
35
36
const { postId } = useParams ( ) ;
36
37
const { user, isAuthenticated } = useContext ( UserContext ) ;
37
38
38
- const [ post , setPost ] = useState < BlogPost | null > ( null ) ;
39
- const [ isDraft , setIsDraft ] = useState ( false ) ;
40
- const [ title , setTitle ] = useState ( "" ) ;
41
- const [ content , setContent ] = useState ( "" ) ;
39
+ const [ post , setPost ] = useState < BlogPost | null > ( initialPost || null ) ;
40
+ const [ isDraft , setIsDraft ] = useState ( initialPost ?. is_draft ?? false ) ;
41
+ const [ title , setTitle ] = useState ( initialPost ?. title ?? "" ) ;
42
+ const [ content , setContent ] = useState ( initialPost ?. content ?? "" ) ;
42
43
const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
43
44
44
45
const handleToggleDraft = ( ) => setIsDraft ( ! isDraft ) ;
@@ -98,7 +99,7 @@ const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath }) => {
98
99
99
100
useEffect ( ( ) => {
100
101
const fetchPostForEditing = async ( ) => {
101
- if ( postId ) {
102
+ if ( postId && ! initialPost ) {
102
103
try {
103
104
const fetchedPost = await fetchBlogPost ( postId ) ;
104
105
if ( ! isAuthenticated ) {
@@ -122,7 +123,7 @@ const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath }) => {
122
123
} ;
123
124
124
125
fetchPostForEditing ( ) ;
125
- } , [ postId , fetchBlogPost , user , navigate , isAuthenticated ] ) ;
126
+ } , [ postId , fetchBlogPost , user , navigate , isAuthenticated , initialPost ] ) ;
126
127
127
128
return (
128
129
< StyledBox >
0 commit comments