Skip to content

Commit

Permalink
Simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Sep 23, 2024
1 parent 4d7a01a commit fca40d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
23 changes: 16 additions & 7 deletions src/api/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface CreatePostBody {
}

export interface UpdatePostBody {
title?: { clean: string; raw: string };
content?: { clean: string; raw: string };
title?: string;
content?: { cleanHtml: string; originalHtml: string };
}

export class ApiClient {
Expand All @@ -34,18 +34,27 @@ export class ApiClient {
}

async updatePost( id: number, body: UpdatePostBody ): Promise< Post > {
return ( await this.post( `/liberated_posts/${ id }`, body ) ) as Post;
const actualBody: any = {};
if ( body.title ) {
actualBody.title = body.title;
}
if ( body.content ) {
actualBody.content = body.content.cleanHtml;
actualBody.meta = {
raw_content: body.content.originalHtml,
};
}
return ( await this.post(
`/liberated_posts/${ id }`,
actualBody
) ) as Post;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getPostByGuid( guid: string ): Promise< Post | null > {
return null;
}

async getPosts(): Promise< Post[] > {
return ( await this.get( '/posts' ) ) as Post[];
}

private async get( route: string ): Promise< object > {
const response = await this.playgroundClient.request( {
url: `/index.php?rest_route=/wp/v2${ route }`,
Expand Down
42 changes: 14 additions & 28 deletions src/ui/flows/blog-post/SelectContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ContentBus } from '@/bus/ContentBus';
import { cleanHtml } from '@/parser/cleanHtml';
import { Post } from '@/api/Post';
import { useSessionContext } from '@/ui/session/SessionProvider';
import { UpdatePostBody } from '@/api/ApiClient';

enum section {
title = 1,
Expand Down Expand Up @@ -77,39 +76,26 @@ export function SelectContent( props: { post: Post; onExit: () => void } ) {
setLastClickedElement( undefined );
}, [ waitingForSelection, lastClickedElement ] );

const saveField = async (
field: string,
clean?: string,
original?: string
) => {
if ( ! clean || ! original || ! apiClient ) {
return;
}
const body: UpdatePostBody = {};
if ( field === 'content' ) {
body.content = { clean, raw: original };
}
if ( field === 'title' ) {
body.title = { clean, raw: original };
}
apiClient.updatePost( post.id, body ).then( () => {
playgroundClient.goTo( post.link );
} );
};

// Save the post when selections happen.
useEffect(
() => void saveField( 'title', title?.cleanHtml, title?.originalHtml ),
() => {
if ( apiClient && title ) {
apiClient
.updatePost( post.id, { title: title.cleanHtml } )
.then( () => playgroundClient.goTo( post.link ) );
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[ title ]
);
useEffect(
() =>
void saveField(
'content',
content?.cleanHtml,
content?.originalHtml
),
() => {
if ( apiClient && content ) {
apiClient
.updatePost( post.id, { content } )
.then( () => playgroundClient.goTo( post.link ) );
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[ content ]
);
Expand Down

0 comments on commit fca40d8

Please sign in to comment.