diff --git a/web/e.cash/data/__tests__/blog.test.js b/web/e.cash/data/__tests__/blog.test.js --- a/web/e.cash/data/__tests__/blog.test.js +++ b/web/e.cash/data/__tests__/blog.test.js @@ -5,6 +5,7 @@ import { getPageCount, getBlogPosts, + sortBlogPostsByDate, formatTimestamp, evaluateMediaLink, } from '../blog.js'; @@ -99,15 +100,15 @@ ); expect(fetch).toHaveBeenNthCalledWith( 2, - 'https://strapi.fabien.cash/api/posts?pagination[page]=1&populate=*&sort=id:desc', + 'https://strapi.fabien.cash/api/posts?pagination[page]=1&populate=*&sort=publishedAt:desc', ); expect(fetch).toHaveBeenNthCalledWith( 3, - 'https://strapi.fabien.cash/api/posts?pagination[page]=2&populate=*&sort=id:desc', + 'https://strapi.fabien.cash/api/posts?pagination[page]=2&populate=*&sort=publishedAt:desc', ); expect(fetch).toHaveBeenNthCalledWith( 4, - 'https://strapi.fabien.cash/api/posts?pagination[page]=3&populate=*&sort=id:desc', + 'https://strapi.fabien.cash/api/posts?pagination[page]=3&populate=*&sort=publishedAt:desc', ); expect(result).toEqual({ @@ -130,6 +131,32 @@ }); }); +describe('sortBlogPostsByDate', () => { + it('should sort blog posts by date in descending order', async () => { + const blogPostsSortedByDate = [ + { attributes: { title: 'Post 5', publishedAt: '2022-05-05' } }, + { attributes: { title: 'Post 4', publish_date: '2022-04-04' } }, + { attributes: { title: 'Post 3', publishedAt: '2022-03-03' } }, + { attributes: { title: 'Post 2', publish_date: '2022-02-02' } }, + { attributes: { title: 'Post 1', publishedAt: '2022-01-01' } }, + ]; + + const shuffleArray = originalArray => { + const array = [...originalArray]; + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; + }; + + const shuffledBlogPosts = shuffleArray(blogPostsSortedByDate); + + const result = await sortBlogPostsByDate(shuffledBlogPosts); + expect(result).toEqual(blogPostsSortedByDate); + }); +}); + describe('formatTimestamp', () => { it('should return a formatted date string for a valid timestamp', () => { const timestamp = '2021-07-01T00:00:00Z'; diff --git a/web/e.cash/data/blog.js b/web/e.cash/data/blog.js --- a/web/e.cash/data/blog.js +++ b/web/e.cash/data/blog.js @@ -48,6 +48,29 @@ return propsObj; } +/** + * Sort blog posts by date and return them in a props object + * @returns {object} props object containing blog posts responses + * sorted by date to be used with getStaticProps + */ +export async function sortBlogPostsByDate(posts) { + const customSort = (postA, postB) => { + const dateA = + postA.attributes.publish_date || postA.attributes.publishedAt; // Check for the presence of 'publish_date' or 'publishedAt' + const dateB = + postB.attributes.publish_date || postB.attributes.publishedAt; + + // Convert both date formats to comparable formats + const dateAFormatted = new Date(dateA).getTime(); + const dateBFormatted = new Date(dateB).getTime(); + + return dateBFormatted - dateAFormatted; // Sort in descending order (latest first) + }; + // Sort the posts array using the custom sorting function. Use slice() to avoid mutating the original array + const sortedPosts = posts.slice().sort(customSort); + return sortedPosts; +} + /** * Convert a timestamp into a more readable format * @param {string} timestamp - the timestamp to convert diff --git a/web/e.cash/pages/blog.js b/web/e.cash/pages/blog.js --- a/web/e.cash/pages/blog.js +++ b/web/e.cash/pages/blog.js @@ -5,7 +5,7 @@ import Layout from '/components/layout'; import H3 from '/components/h3'; import { Container } from '/components/atoms'; -import { getBlogPosts } from '/data/blog.js'; +import { getBlogPosts, sortBlogPostsByDate } from '/data/blog.js'; import { BlogCtn, FeaturedCardCtn, @@ -105,7 +105,12 @@ */ export async function getStaticProps() { const posts = await getBlogPosts(); - return posts; + const orderedPosts = await sortBlogPostsByDate(posts.props.posts); + return { + props: { + posts: orderedPosts, + }, + }; } export default Blog;