Skip to content

Commit

Permalink
Contourne la pagination strapi sur la page actus du site
Browse files Browse the repository at this point in the history
  • Loading branch information
mariheck committed Aug 8, 2024
1 parent 59aa2fa commit 282448a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
23 changes: 22 additions & 1 deletion packages/site/app/actus/[id]/[slug]/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
VideoFetchedData,
} from '../../../types';

const LIMIT = 50;

export const getMetaData = async (id: number) => {
const data = await fetchItem('actualites', id, [
['populate[0]', 'seo'],
Expand Down Expand Up @@ -59,13 +61,32 @@ export const getData = async (id: number) => {
]);

if (data) {
const {data: idList} = await fetchCollection('actualites', [
const {data: ids, meta} = await fetchCollection('actualites', [
['fields[0]', 'DateCreation'],
['fields[1]', 'createdAt'],
['fields[2]', 'Epingle'],
['sort[0]', 'createdAt:desc'],
['pagination[start]', '0'],
['pagination[limit]', `${LIMIT}`],
]);

const {pagination} = meta;
let idList = ids;
let page = 1;

while (page < Math.ceil(pagination.total / pagination.limit)) {
const {data} = await fetchCollection('actualites', [
['fields[0]', 'DateCreation'],
['fields[1]', 'createdAt'],
['fields[2]', 'Epingle'],
['sort[0]', 'createdAt:desc'],
['pagination[start]', `${page * LIMIT}`],
['pagination[limit]', `${LIMIT}`],
]);
idList.push(...data);
page++;
}

const sortedIds = idList
? idList
.map(d => ({
Expand Down
25 changes: 22 additions & 3 deletions packages/site/app/actus/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Metadata} from 'next';
import {convertNameToSlug} from 'src/utils/convertNameToSlug';
import {notFound} from 'next/navigation';

const LIMIT = 50;

export async function generateMetadata(): Promise<Metadata> {
return {
title: 'Actualités',
Expand All @@ -26,13 +28,30 @@ type ActuCard = {
};

const getData = async () => {
const {data} = await fetchCollection('actualites', [
const {data, meta} = await fetchCollection('actualites', [
['populate[0]', 'Couverture'],
['sort[0]', 'createdAt:desc'],
['pagination[start]', '0'],
['pagination[limit]', `${LIMIT}`],
]);

const formattedData: ActuCard[] | null = data
? data.map(d => ({
const {pagination} = meta;
let cards = data;
let page = 1;

while (page < Math.ceil(pagination.total / pagination.limit)) {
const {data} = await fetchCollection('actualites', [
['populate[0]', 'Couverture'],
['sort[0]', 'createdAt:desc'],
['pagination[start]', `${page * LIMIT}`],
['pagination[limit]', `${LIMIT}`],
]);
cards.push(...data);
page++;
}

const formattedData: ActuCard[] | null = cards
? cards.map(d => ({
id: d.id,
titre: d.attributes.Titre as unknown as string,
dateCreation:
Expand Down

0 comments on commit 282448a

Please sign in to comment.