From 01f8351088d6482fb2b631a28928b6d40d491c11 Mon Sep 17 00:00:00 2001 From: LAU-MG <90914885+LAU-MG@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:38:14 +0200 Subject: [PATCH] fix: restaured Sortarticle file (#27) --- src/functions/sortarticles.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/functions/sortarticles.ts b/src/functions/sortarticles.ts index 7c00ec0..399614e 100644 --- a/src/functions/sortarticles.ts +++ b/src/functions/sortarticles.ts @@ -1,22 +1,37 @@ -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck +'use client'; -export const sortArticles = (articles, exclude) => { - let datesList = []; - const mostRecentArticles = []; +type ArticleAttributes = { + updatedAt: string; +}; + +type Article = { + attributes: ArticleAttributes; +}; + +type Category = { + data: Article[]; +}; + +export const sortArticles = (articles: Category[], exclude: number[]): ArticleAttributes[] => { + let datesList: string[] = []; + const mostRecentArticles: ArticleAttributes[] = []; articles.forEach((category, index) => { category.data.forEach((data) => { - if (exclude.indexOf(index) == -1) datesList.push(data.attributes.updatedAt); + if (!exclude.includes(index)) { + datesList.push(data.attributes.updatedAt); + } }); }); - datesList = datesList.sort((a, b) => new Date(b) - new Date(a)); + datesList = datesList.sort((a, b) => new Date(b).getTime() - new Date(a).getTime()); for (let i = 0; i < datesList.length; i++) { articles.forEach((category) => { category.data.forEach((data) => { - if (data.attributes.updatedAt === datesList[i]) mostRecentArticles.push(data.attributes); + if (data.attributes.updatedAt === datesList[i]) { + mostRecentArticles.push(data.attributes); + } }); }); }