Skip to content

Commit

Permalink
Load the post in EditPost
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Oct 1, 2024
1 parent f7f61e8 commit 888ddfb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/model/content/Post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum PostType {
BlogPost = 'blog-post',
BlogPost = 'liberated_post',
}

export const humanReadablePostType: Map< PostType, string > = new Map( [
Expand Down
33 changes: 22 additions & 11 deletions src/ui/posts/EditPost.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useParams } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { useSessionContext } from '@/ui/session/SessionProvider';
import { BlogPost } from '@/model/content/BlogPost';
import { PostEditor } from '@/ui/posts/PostEditor';
import { ContentBus } from '@/bus/ContentBus';
import { Toolbar } from '@/ui/posts/Toolbar';
Expand All @@ -10,25 +9,37 @@ import {
parsePostDate,
parsePostTitle,
} from '@/parser/blog-post';
import { Post, PostType } from '@/model/content/Post';

export function EditPost() {
const [ post, setPost ] = useState< BlogPost >();
const [ post, setPost ] = useState< Post >();
const { postId } = useParams();
const { apiClient, playgroundClient } = useSessionContext();

useEffect( () => {
if ( ! apiClient ) {
return;
}
apiClient.blogPosts
.findById( postId! )
.then( ( p: BlogPost | null ) => {
if ( ! p ) {
throw Error( `post with id ${ postId } not found` );
}
setPost( p );
playgroundClient.goTo( p.url );
} );
async function loadPost() {
const genericPost = await apiClient!.posts.findById( postId! );
if ( ! genericPost ) {
throw Error( `post with id ${ postId } not found` );
}
let p: Post | null;
switch ( genericPost.type ) {
case PostType.BlogPost:
p = await apiClient!.blogPosts.findById( postId! );
break;
default:
throw Error( `unknown post type ${ genericPost.type }` );
}
if ( ! p ) {
throw Error( `post with id ${ postId } not found` );
}
setPost( p );
playgroundClient.goTo( p.url );
}
loadPost().catch( console.error );
}, [ apiClient, postId ] );

Check warning on line 43 in src/ui/posts/EditPost.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'playgroundClient'. Either include it or remove the dependency array

const isValid =
Expand Down
5 changes: 2 additions & 3 deletions src/ui/posts/PostEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { ReactElement, useEffect, useState } from 'react';
import { AppBus } from '@/bus/AppBus';
import { Message } from '@/bus/Message';
import { ContentBus } from '@/bus/ContentBus';
import { BlogPost } from '@/model/content/BlogPost';
import { PostField } from '@/model/content/Post';
import { Post, PostField } from '@/model/content/Post';
import { FieldEditor } from '@/ui/posts/FieldEditor';

interface Props {
post: BlogPost;
post: Post;
fieldOrder: Record< string, number >;
onFieldChanged: ( name: string, field: PostField ) => void;
}
Expand Down

0 comments on commit 888ddfb

Please sign in to comment.