generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from alleyinteractive/feature/issue-218/cache…
…-initial-query-api-calls Feature: Issue-218: Cache Initial Post API Calls
- Loading branch information
Showing
8 changed files
with
112 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
interface PostsApiPathProps { | ||
search: string, | ||
offset: number, | ||
postType: string, | ||
status: 'publish', | ||
perPage: 20, | ||
orderBy: string, | ||
currentPostId: number | ||
} | ||
|
||
export default function buildPostsApiPath(pathProps: PostsApiPathProps) { | ||
return addQueryArgs('/wp-curate/v1/posts', { | ||
search: pathProps.search, | ||
offset: pathProps.offset, | ||
post_type: pathProps.postType, | ||
status: pathProps.status, | ||
per_page: pathProps.perPage, | ||
orderby: pathProps.orderBy, | ||
current_post_id: pathProps.currentPostId, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
type FetcherProps = [string | undefined, number]; | ||
type PostResponse = number[] | { id: number }; | ||
|
||
/** | ||
* Fetches the post data from the API. | ||
* @param url The API url. | ||
* @param currentPostId The current post ID. | ||
*/ | ||
const queryBlockPostFetcher = ( | ||
[url, currentPostId]: FetcherProps, | ||
) => apiFetch<PostResponse>({ path: url }) | ||
.then((response) => { | ||
let revisedResponse; | ||
// If the response is an array, filter out the current post. | ||
if (Array.isArray(response)) { | ||
revisedResponse = response.filter((item) => item !== currentPostId); | ||
} else if (response.id === currentPostId) { | ||
// Response is an object, if id is the current post, nullify it. | ||
revisedResponse = null; | ||
} else { | ||
revisedResponse = response; | ||
} | ||
return revisedResponse; | ||
}); | ||
|
||
export default queryBlockPostFetcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters