Skip to content

Commit 62c940e

Browse files
committed
asdsa
1 parent fd40f4f commit 62c940e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

frontend/app/components/BlogPostForm.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useBlogPost, type BlogPost, type BlogPostInput } from "../hooks/useBlog
1818

1919
interface BlogPostFormProps {
2020
previousPath?: string;
21+
initialPost?: BlogPost;
2122
}
2223

2324
const StyledBox = styled(Box)(({ theme }) => ({
@@ -29,16 +30,16 @@ const StyledBox = styled(Box)(({ theme }) => ({
2930
},
3031
}));
3132

32-
const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath }) => {
33+
const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath, initialPost }) => {
3334
const navigate = useNavigate();
3435
const { fetchBlogPost, createBlogPost, updateBlogPost } = useBlogPost();
3536
const { postId } = useParams();
3637
const { user, isAuthenticated } = useContext(UserContext);
3738

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 ?? "");
4243
const [isSubmitting, setIsSubmitting] = useState(false);
4344

4445
const handleToggleDraft = () => setIsDraft(!isDraft);
@@ -98,7 +99,7 @@ const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath }) => {
9899

99100
useEffect(() => {
100101
const fetchPostForEditing = async () => {
101-
if (postId) {
102+
if (postId && !initialPost) {
102103
try {
103104
const fetchedPost = await fetchBlogPost(postId);
104105
if (!isAuthenticated) {
@@ -122,7 +123,7 @@ const BlogPostForm: React.FC<BlogPostFormProps> = ({ previousPath }) => {
122123
};
123124

124125
fetchPostForEditing();
125-
}, [postId, fetchBlogPost, user, navigate, isAuthenticated]);
126+
}, [postId, fetchBlogPost, user, navigate, isAuthenticated, initialPost]);
126127

127128
return (
128129
<StyledBox>

frontend/app/routes/_app.blog.edit.$postId.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ export const meta: MetaFunction<typeof loader, { 'routes/_app': LoaderData }> =
4747

4848
export default function EditBlogPost() {
4949
const { post } = useLoaderData<typeof loader>();
50-
return <BlogPostForm />;
50+
return <BlogPostForm initialPost={post} />;
5151
}

0 commit comments

Comments
 (0)