@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/build/adapter-nextjs/api-handlers/fetchStories.js +2 -2
- package/build/adapter-nextjs/api-handlers/fetchStories.js.map +1 -1
- package/build/adapter-nextjs/page-props/home.d.ts +1 -1
- package/build/adapter-nextjs/page-props/home.js +3 -3
- package/build/adapter-nextjs/page-props/home.js.map +1 -1
- package/build/data-fetching/api/PrezlyApi.d.ts +3 -3
- package/build/data-fetching/api/PrezlyApi.js +9 -11
- package/build/data-fetching/api/PrezlyApi.js.map +1 -1
- package/build/data-fetching/api/lib.d.ts +11 -0
- package/build/data-fetching/api/lib.js +20 -0
- package/build/data-fetching/api/lib.js.map +1 -0
- package/build/infinite-loading/types.d.ts +1 -1
- package/build/infinite-loading/useInfiniteLoading.js +2 -2
- package/build/infinite-loading/useInfiniteLoading.js.map +1 -1
- package/build/infinite-loading/useInfiniteStoriesLoading.js +4 -4
- package/build/infinite-loading/useInfiniteStoriesLoading.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/adapter-nextjs/api-handlers/fetchStories.ts +2 -2
- package/src/adapter-nextjs/page-props/home.ts +4 -4
- package/src/data-fetching/api/PrezlyApi.ts +13 -15
- package/src/data-fetching/api/lib.ts +24 -0
- package/src/infinite-loading/types.ts +1 -1
- package/src/infinite-loading/useInfiniteLoading.ts +2 -2
- package/src/infinite-loading/useInfiniteStoriesLoading.ts +4 -4
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@ export async function fetchStories(req: NextApiRequest, res: NextApiResponse) {
|
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const { page, pageSize,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
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' | '
|
|
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
|
|
185
|
-
offset
|
|
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
|
|
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
|
+
}
|
|
@@ -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,
|
|
30
|
-
const adjustedItemsTotal =
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
65
|
+
withHighlightedStory,
|
|
66
66
|
category,
|
|
67
67
|
currentLocale,
|
|
68
68
|
include,
|