Skip to content

Commit

Permalink
Merge pull request #364 from prezly/feature/dev-11239-implement-cta-b…
Browse files Browse the repository at this point in the history
…utton-rendering-in-standard-nextjs-themes

[DEV-11239] Adds new optional `formats` param to `GetStoryPageServerSideProps`
  • Loading branch information
fgyimah authored Aug 11, 2023
2 parents b83e6bd + e84d2b2 commit 2560bf2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "theme-kit",
"version": "0.0.0",
"private": true,
"workspaces": [
"packages/*"
],
"workspaces": ["packages/*"],
"scripts": {
"build": "npx lerna run build",
"dev:core": "npx lerna run dev --scope=@prezly/theme-kit-core",
Expand All @@ -30,7 +28,7 @@
"devDependencies": {
"@playwright/test": "1.36.2",
"@prezly/eslint-config": "4.5.0",
"@prezly/sdk": "16.5.0",
"@prezly/sdk": "16.7.0",
"@types/jest": "29.5.3",
"@types/node": "17.0.45",
"@types/react": "18.2.18",
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/data-fetching/api/PrezlyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ export class PrezlyApi {
this.themeUuid = themeUuid;
}

async getStory(uuid: string) {
async getStory(uuid: string, formats = [Story.FormatVersion.SLATEJS_V4]) {
if (!isUuid(uuid)) {
return undefined;
}

try {
const story = await this.sdk.stories.get(uuid, {
formats: [Story.FormatVersion.SLATEJS_V4],
formats,
});
return story;
} catch (error) {
Expand Down Expand Up @@ -200,11 +200,12 @@ export class PrezlyApi {
return { stories, storiesTotal };
}

async getStoryBySlug(slug: string) {
async getStoryBySlug(slug: string, formats = [Story.FormatVersion.SLATEJS_V4]) {
const query = JSON.stringify(getSlugQuery(this.newsroomUuid, slug));
const { stories } = await this.searchStories({
limit: 1,
query,
formats,
});

if (stories[0]) {
Expand All @@ -230,8 +231,11 @@ export class PrezlyApi {
);
}

searchStories: Stories.Client['search'] = (options) =>
this.sdk.stories.search({ formats: [Story.FormatVersion.SLATEJS_V4], ...options });
searchStories: Stories.Client['search'] = (options) => {
const formats = options?.formats ?? [Story.FormatVersion.SLATEJS_V4];

return this.sdk.stories.search({ ...options, formats });
};

async getGalleries({ page, pageSize, type }: GetGalleriesOptions) {
const { offset, limit } = toPaginationParams({ page, pageSize });
Expand Down
7 changes: 5 additions & 2 deletions packages/nextjs/src/adapter-nextjs/page-props/story.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Story } from '@prezly/sdk';
import type {
GetServerSidePropsContext,
GetServerSidePropsResult,
Expand All @@ -16,14 +17,15 @@ import type { PropsFunction } from './lib/types';

export function getStoryPageServerSideProps<CustomProps extends Record<string, any>>(
customProps: CustomProps | PropsFunction<CustomProps>,
formats?: Story.FormatVersion[],
) {
return async function getServerSideProps(
context: GetServerSidePropsContext,
): Promise<GetServerSidePropsResult<CustomProps>> {
const api = getNextPrezlyApi(context.req);

const { slug } = context.params as { slug?: string };
const story = slug ? await api.getStoryBySlug(slug) : null;
const story = slug ? await api.getStoryBySlug(slug, formats) : null;
if (!story) {
return { notFound: true };
}
Expand Down Expand Up @@ -53,14 +55,15 @@ export function getStoryPageServerSideProps<CustomProps extends Record<string, a

export function getStoryPageStaticProps<CustomProps extends Record<string, any>>(
customProps: CustomProps | PropsFunction<CustomProps, GetStaticPropsContext>,
formats?: Story.FormatVersion[],
) {
return async function getStaticProps(
context: GetStaticPropsContext,
): Promise<GetStaticPropsResult<CustomProps>> {
const api = getNextPrezlyApi();

const { slug } = context.params as { slug?: string };
const story = slug ? await api.getStoryBySlug(slug) : null;
const story = slug ? await api.getStoryBySlug(slug, formats) : null;
if (!story) {
return { notFound: true };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Story } from '@prezly/sdk';
import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';

import { getNextPrezlyApi } from '../../data-fetching';
Expand All @@ -11,10 +12,11 @@ export function getStoryPreviewPageServerSideProps<CustomProps extends Record<st
) {
return async function getServerSideProps(
context: GetServerSidePropsContext,
formats?: Story.FormatVersion[],
): Promise<GetServerSidePropsResult<CustomProps>> {
const api = getNextPrezlyApi(context.req);
const { uuid } = context.params as { uuid: string };
const story = await api.getStory(uuid);
const story = await api.getStory(uuid, formats);
if (!story) {
return { notFound: true };
}
Expand Down

0 comments on commit 2560bf2

Please sign in to comment.