From f06f83c45b681f84aa06278f3e504d0ca9f22fe2 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 13 Sep 2023 17:51:00 +0100 Subject: [PATCH] Updated collection card post fetching to use Content API (#18121) closes https://github.com/TryGhost/Product/issues/3874 - fetch Content API key if we don't already have it - use the `frontend` service to fetch posts via the Content API - uses same ordering and published-only filtering as default front-end requests --- .../app/components/koenig-lexical-editor.js | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ghost/admin/app/components/koenig-lexical-editor.js b/ghost/admin/app/components/koenig-lexical-editor.js index 04134c5db4..e0b00e0227 100644 --- a/ghost/admin/app/components/koenig-lexical-editor.js +++ b/ghost/admin/app/components/koenig-lexical-editor.js @@ -131,6 +131,7 @@ const WordCountPlugin = (props) => { export default class KoenigLexicalEditor extends Component { @service ajax; @service feature; + @service frontend; @service ghostPaths; @service session; @service store; @@ -138,7 +139,9 @@ export default class KoenigLexicalEditor extends Component { @service membersUtils; @inject config; + offers = null; + contentKey = null; get pinturaJsUrl() { if (!this.settings.pintura) { @@ -241,13 +244,20 @@ export default class KoenigLexicalEditor extends Component { }; const fetchCollectionPosts = async (collectionSlug) => { - const collectionPostsEndpoint = this.ghostPaths.url.api('posts'); - const {posts} = await this.ajax.request(collectionPostsEndpoint, { - data: { - collection: collectionSlug, - limit: 12 - } - }); + if (!this.contentKey) { + const integrations = await this.store.findAll('integration'); + const contentIntegration = integrations.findBy('slug', 'ghost-core-content'); + this.contentKey = contentIntegration?.contentKey.secret; + } + + const postsUrl = new URL(this.frontend.getUrl('/ghost/api/content/posts/')); + postsUrl.searchParams.append('key', this.contentKey); + postsUrl.searchParams.append('collection', collectionSlug); + postsUrl.searchParams.append('limit', 12); + + const response = await this.frontend.fetch(postsUrl.toString()); + const {posts} = await response.json(); + return posts; };