Add status property to content item structure (#44)
* Add status property to content item structure This commit introduces a new `status` property to the content item structure, allowing content items to maintain a status such as 'draft', 'published', 'review', 'pending'. This status is mapped to the corresponding status in Wordpress and Keystatic clients to ensure consistent usage across platforms. Content retrieval methods now also include a status filter. * Refactor code for style and readability improvements This commit includes changes in various files across different packages to improve code readability, including adjusting spacing and breaking down complex lines into simpler parts. In addition, the switch statement in `wp-client.ts` has been refactored and status values are now held in variables, and the CSS classes in `global-loader.tsx` have been reorganized.
This commit is contained in:
committed by
GitHub
parent
21f42f14ce
commit
3393863dd2
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
WP_Post_Status_Name,
|
||||
WP_REST_API_Category,
|
||||
WP_REST_API_Post,
|
||||
WP_REST_API_Tag,
|
||||
@@ -104,7 +105,9 @@ class WordpressClient implements CmsClient {
|
||||
const total = totalHeader ? Number(totalHeader) : 0;
|
||||
const results = (await response.json()) as WP_REST_API_Post[];
|
||||
|
||||
const posts = await Promise.all(
|
||||
const status = options.status ?? 'published';
|
||||
|
||||
const postsResults = await Promise.allSettled(
|
||||
results.map(async (item: WP_REST_API_Post) => {
|
||||
let parentId: string | undefined;
|
||||
|
||||
@@ -116,6 +119,12 @@ class WordpressClient implements CmsClient {
|
||||
parentId = item.parent.toString();
|
||||
}
|
||||
|
||||
const mappedStatus = mapToStatus(item.status as WP_Post_Status_Name);
|
||||
|
||||
if (mappedStatus !== status) {
|
||||
throw new Error('Status does not match');
|
||||
}
|
||||
|
||||
const categories = await this.getCategoriesByIds(item.categories ?? []);
|
||||
const tags = await this.getTagsByIds(item.tags ?? []);
|
||||
const image = item.featured_media ? this.getFeaturedMedia(item) : '';
|
||||
@@ -130,6 +139,7 @@ class WordpressClient implements CmsClient {
|
||||
url: item.link,
|
||||
slug: item.slug,
|
||||
publishedAt: item.date,
|
||||
status: mappedStatus ?? 'draft',
|
||||
categories: categories,
|
||||
tags: tags,
|
||||
parentId,
|
||||
@@ -139,6 +149,10 @@ class WordpressClient implements CmsClient {
|
||||
}),
|
||||
);
|
||||
|
||||
const posts = postsResults
|
||||
.filter((item) => item.status === 'fulfilled')
|
||||
.map((item) => item.value);
|
||||
|
||||
return {
|
||||
total,
|
||||
items: posts,
|
||||
@@ -148,9 +162,11 @@ class WordpressClient implements CmsClient {
|
||||
async getContentItemBySlug({
|
||||
slug,
|
||||
collection,
|
||||
status,
|
||||
}: {
|
||||
slug: string;
|
||||
collection: string;
|
||||
status?: Cms.ContentItemStatus;
|
||||
}) {
|
||||
const searchParams = new URLSearchParams({
|
||||
_embed: 'true',
|
||||
@@ -174,6 +190,15 @@ class WordpressClient implements CmsClient {
|
||||
return;
|
||||
}
|
||||
|
||||
const mappedStatus = status
|
||||
? mapToStatus(item.status as WP_Post_Status_Name)
|
||||
: undefined;
|
||||
const statusMatch = status ? mappedStatus === status : true;
|
||||
|
||||
if (!statusMatch) {
|
||||
return;
|
||||
}
|
||||
|
||||
const categories = await this.getCategoriesByIds(item.categories ?? []);
|
||||
const tags = await this.getTagsByIds(item.tags ?? []);
|
||||
const image = item.featured_media ? this.getFeaturedMedia(item) : '';
|
||||
@@ -189,6 +214,7 @@ class WordpressClient implements CmsClient {
|
||||
content: item.content.rendered,
|
||||
slug: item.slug,
|
||||
publishedAt: item.date,
|
||||
status: mappedStatus ?? 'draft',
|
||||
categories,
|
||||
tags,
|
||||
parentId: item.parent?.toString(),
|
||||
@@ -370,3 +396,23 @@ function mapSortByParam(sortBy: string) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function mapToStatus(status: WP_Post_Status_Name): Cms.ContentItemStatus {
|
||||
const Draft = 'draft' as WP_Post_Status_Name;
|
||||
const Publish = 'publish' as WP_Post_Status_Name;
|
||||
const Pending = 'pending' as WP_Post_Status_Name;
|
||||
|
||||
switch (status) {
|
||||
case Draft:
|
||||
return 'draft';
|
||||
|
||||
case Publish:
|
||||
return 'published';
|
||||
|
||||
case Pending:
|
||||
return 'pending';
|
||||
|
||||
default:
|
||||
return 'draft';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user