@prezly/theme-kit-nextjs 3.2.0-2 → 3.2.0-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prezly/theme-kit-nextjs",
3
- "version": "3.2.0-2",
3
+ "version": "3.2.0-4",
4
4
  "description": "Data layer and utility library for developing Prezly themes with NextJS",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -8,7 +8,7 @@ export async function fetchStories(req: NextApiRequest, res: NextApiResponse) {
8
8
  return;
9
9
  }
10
10
 
11
- const { page, pageSize, useHighlightedStory, category, include, localeCode, pinning } =
11
+ const { page, pageSize, withHighlightedStory, category, include, localeCode, pinning } =
12
12
  req.body;
13
13
 
14
14
  try {
@@ -21,7 +21,7 @@ export async function fetchStories(req: NextApiRequest, res: NextApiResponse) {
21
21
  pageSize,
22
22
  include,
23
23
  localeCode,
24
- useHighlightedStory,
24
+ withHighlightedStory,
25
25
  pinning,
26
26
  }));
27
27
 
@@ -26,7 +26,7 @@ interface Options {
26
26
  * When set to `true`, the initial `stories` array will include one extra story to place as highlighted story.
27
27
  * This will offset each subsequent page by 1 story to account for that.
28
28
  */
29
- useHighlightedStory?: boolean;
29
+ withHighlightedStory?: boolean;
30
30
  pageSize?: number;
31
31
  pinning?: boolean;
32
32
  }
@@ -39,7 +39,7 @@ export function getHomepageServerSideProps<
39
39
  pageSize = DEFAULT_PAGE_SIZE,
40
40
  extraStoryFields,
41
41
  pinning = false,
42
- useHighlightedStory = false,
42
+ withHighlightedStory = false,
43
43
  } = options || {};
44
44
 
45
45
  return async function getServerSideProps(
@@ -60,7 +60,7 @@ export function getHomepageServerSideProps<
60
60
  pinning,
61
61
  include: extraStoryFields,
62
62
  localeCode,
63
- useHighlightedStory,
63
+ withHighlightedStory,
64
64
  });
65
65
  const { stories, storiesTotal } = storiesPaginated;
66
66
 
@@ -74,7 +74,7 @@ export function getHomepageServerSideProps<
74
74
  itemsTotal: storiesTotal,
75
75
  currentPage: page ?? 1,
76
76
  pageSize,
77
- useHighlightedStory,
77
+ withHighlightedStory,
78
78
  },
79
79
  ...(typeof customProps === 'function'
80
80
  ? await (customProps as PropsFunction<CustomProps>)(context, serverSideProps)
@@ -21,6 +21,7 @@ import type { PageProps, ServerSidePageProps } from '../../types';
21
21
  import { DEFAULT_PAGE_SIZE } from '../../utils';
22
22
  import { getAlgoliaSettings, isSdkError, isUuid } from '../lib';
23
23
 
24
+ import { toPaginationParams } from './lib';
24
25
  import {
25
26
  getChronologicalSortOrder,
26
27
  getContactsQuery,
@@ -47,7 +48,7 @@ interface GetStoriesOptions {
47
48
  * When set to `true`, the result for the first page will include one extra story to place as highlighted story.
48
49
  * This will offset each subsequent page by 1 story to account for that.
49
50
  */
50
- useHighlightedStory?: boolean;
51
+ withHighlightedStory?: boolean;
51
52
  }
52
53
 
53
54
  interface GetGalleriesOptions {
@@ -143,16 +144,12 @@ export class PrezlyApi {
143
144
  pinning = false,
144
145
  include,
145
146
  localeCode,
146
- useHighlightedStory,
147
+ withHighlightedStory,
147
148
  }: GetStoriesOptions = {}) {
148
149
  const sortOrder = getChronologicalSortOrder(order, pinning);
149
150
  const query = JSON.stringify(getStoriesQuery(this.newsroomUuid, undefined, localeCode));
150
151
 
151
- const limit = useHighlightedStory && (!page || page === 1) ? pageSize + 1 : pageSize;
152
- const baseOffset = typeof page !== 'undefined' ? (page - 1) * pageSize : undefined;
153
- const offset = useHighlightedStory && baseOffset ? baseOffset + 1 : baseOffset;
154
-
155
- console.log({ limit, offset, useHighlightedStory });
152
+ const { offset, limit } = toPaginationParams({ page, pageSize, withHighlightedStory });
156
153
 
157
154
  const { stories, pagination } = await this.searchStories({
158
155
  limit,
@@ -175,14 +172,16 @@ export class PrezlyApi {
175
172
  order = DEFAULT_SORT_ORDER,
176
173
  include,
177
174
  localeCode,
178
- }: Omit<GetStoriesOptions, 'pinning' | 'useHighlightedStory'> = {},
175
+ }: Omit<GetStoriesOptions, 'pinning' | 'withHighlightedStory'> = {},
179
176
  ) {
180
177
  const sortOrder = getChronologicalSortOrder(order);
181
178
  const query = JSON.stringify(getStoriesQuery(this.newsroomUuid, category.id, localeCode));
182
179
 
180
+ const { offset, limit } = toPaginationParams({ page, pageSize });
181
+
183
182
  const { stories, pagination } = await this.searchStories({
184
- limit: pageSize,
185
- offset: typeof page === 'undefined' ? undefined : (page - 1) * pageSize,
183
+ limit,
184
+ offset,
186
185
  sortOrder,
187
186
  query,
188
187
  include,
@@ -226,12 +225,11 @@ export class PrezlyApi {
226
225
  searchStories: Stories.Client['search'] = (options) => this.sdk.stories.search(options);
227
226
 
228
227
  async getGalleries({ page, pageSize }: GetGalleriesOptions) {
228
+ const { offset, limit } = toPaginationParams({ page, pageSize });
229
+
229
230
  return this.sdk.newsroomGalleries.search(this.newsroomUuid, {
230
- limit: pageSize,
231
- offset:
232
- typeof page === 'undefined' || typeof pageSize === 'undefined'
233
- ? undefined
234
- : (page - 1) * pageSize,
231
+ limit,
232
+ offset,
235
233
  scope: getGalleriesQuery(),
236
234
  });
237
235
  }
@@ -0,0 +1,24 @@
1
+ export function toPaginationParams(params: {
2
+ page?: number;
3
+ pageSize?: number;
4
+ withHighlightedStory?: boolean;
5
+ }) {
6
+ const { page = 1, pageSize, withHighlightedStory = false } = params;
7
+
8
+ if (!pageSize) {
9
+ return { offset: undefined, limit: undefined };
10
+ }
11
+
12
+ const offset = pageSize * (page - 1);
13
+ const limit = pageSize;
14
+
15
+ if (withHighlightedStory && page === 1) {
16
+ return { offset: 0, limit: limit + 1 };
17
+ }
18
+
19
+ if (withHighlightedStory && page > 1) {
20
+ return { offset: offset + 1, limit };
21
+ }
22
+
23
+ return { offset, limit };
24
+ }
@@ -5,5 +5,5 @@ export interface PaginationProps {
5
5
  /**
6
6
  * When set to `true`, the calculated offset will be increased by one to account for the highlighted story
7
7
  */
8
- useHighlightedStory?: boolean;
8
+ withHighlightedStory?: boolean;
9
9
  }
@@ -26,8 +26,8 @@ export function useInfiniteLoading<T>({
26
26
  const [data, setData] = useState<T[]>(initialData);
27
27
  const [currentPage, setCurrentPage] = useState(pagination.currentPage);
28
28
 
29
- const { itemsTotal, pageSize, useHighlightedStory } = pagination;
30
- const adjustedItemsTotal = useHighlightedStory ? itemsTotal : itemsTotal - 1;
29
+ const { itemsTotal, pageSize, withHighlightedStory } = pagination;
30
+ const adjustedItemsTotal = withHighlightedStory ? itemsTotal : itemsTotal - 1;
31
31
  const pagesTotal = Math.ceil(adjustedItemsTotal / pageSize);
32
32
  const canLoadMore = currentPage < pagesTotal;
33
33
 
@@ -10,7 +10,7 @@ import { useInfiniteLoading } from './useInfiniteLoading';
10
10
  async function fetchStories<T extends Story = Story>(
11
11
  page: number,
12
12
  pageSize: number,
13
- useHighlightedStory: boolean,
13
+ withHighlightedStory: boolean,
14
14
  category?: Category,
15
15
  locale?: LocaleObject,
16
16
  include?: (keyof Story.ExtraFields)[],
@@ -24,7 +24,7 @@ async function fetchStories<T extends Story = Story>(
24
24
  body: JSON.stringify({
25
25
  page,
26
26
  pageSize,
27
- useHighlightedStory,
27
+ withHighlightedStory,
28
28
  category,
29
29
  include,
30
30
  pinning,
@@ -55,14 +55,14 @@ export function useInfiniteStoriesLoading<T extends Story = Story>(
55
55
  pinning?: boolean,
56
56
  ) {
57
57
  const currentLocale = useCurrentLocale();
58
- const { useHighlightedStory = false } = pagination;
58
+ const { withHighlightedStory = false } = pagination;
59
59
 
60
60
  const { canLoadMore, data, isLoading, loadMore, resetData } = useInfiniteLoading<T>({
61
61
  fetchingFn: async (nextPage, pageSize) => {
62
62
  const { stories } = await fetchStories<T>(
63
63
  nextPage,
64
64
  pageSize,
65
- useHighlightedStory,
65
+ withHighlightedStory,
66
66
  category,
67
67
  currentLocale,
68
68
  include,