diff --git a/components/news/article-preview.js b/components/news/article-preview.js index 98d57130..46725c7e 100644 --- a/components/news/article-preview.js +++ b/components/news/article-preview.js @@ -84,7 +84,6 @@ export const ArticlePreview = ({ } - Read more → +} + +export const HomePageArticlePreview = ({article }) => { + const date = new Date(article.publishDate) + const [day, month, year] = [ + date.getUTCDate(), + date.getUTCMonth() + 1, + date.getUTCFullYear(), + ] + const dateString = date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); + + const articleLink = `/news/${year}/${month}/${day}/${article.slug}`; + + return + + + + {dateString} + + { + article.newsOrBlog === 'blog' ? 'Blog' : 'Feature' + } + + + + {article.title} + + + + + .hover-link': { + position: 'absolute', + bottom: 0, + right: 0, + }, + position: 'relative', + maxHeight: 'var(--maxHeight)', + overflow: 'hidden', + }}> + {article.excerpt} + Read more → + + } \ No newline at end of file diff --git a/components/projectSpotlight.js b/components/projectSpotlight.js index 763e84c4..042f177a 100644 --- a/components/projectSpotlight.js +++ b/components/projectSpotlight.js @@ -93,7 +93,7 @@ export const ProjectSpotlight = ({ selectedProjects }) => { return ( - Project Spotlight + Project Spotlight { + try { + let bodyContent = JSON.stringify({ + "pagination": { + "pageSize": 3, + "page": 1 + } + }); + + const response = await fetch('https://api.renci.org/api/post-list', { + method: "POST", + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + body: bodyContent + }); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const result = await response.json(); + return result.results + + } catch (error) { + console.log(error); + } +} \ No newline at end of file diff --git a/lib/strapi/index.js b/lib/strapi/index.js index 1806cd03..99d5120d 100644 --- a/lib/strapi/index.js +++ b/lib/strapi/index.js @@ -8,4 +8,5 @@ export * from './peopleGraphQL' export * from './fetchOurWorkTrayItems' export * from './newsAppearancesGraphQL' export * from './newsSWR' -export * from './newsGraphQL' \ No newline at end of file +export * from './newsGraphQL' +export * from './fetchHomeNews' \ No newline at end of file diff --git a/pages/index.js b/pages/index.js index 8b62fdac..43af24a2 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,13 +1,15 @@ import { Fragment } from 'react' import Head from 'next/head' import Image from 'next/image' -import { Typography } from '@mui/material' +import { Typography, Stack } from '@mui/material' import { Link, Page } from '../components' import homeHero from '../images/racks.jpg' import { ProjectSpotlight } from '../components/projectSpotlight' -import { fetchActiveStrapiProjects } from '../lib/strapi' +import { fetchActiveStrapiProjects, fetchHomeNews } from '../lib/strapi' +import { HomePageArticlePreview } from "../components/news/article-preview"; + +export default function Home({ selectedProjects, newsArray }) { -export default function Home({ selectedProjects}) { return ( + { + newsArray && ( + + Recent News + + { newsArray.map((article, i) => ( + + ))} + + + ) + } + ) } export async function getServerSideProps({ res }) { - res.setHeader( - 'Cache-Control', - 'no-cache, no-store, must-revalidate' - ) + try { + res.setHeader( + 'Cache-Control', + 'no-cache, no-store, must-revalidate' + ) - const projects = await fetchActiveStrapiProjects() - - let projectsCopy = [...projects] - let projectSelection = [] - for (let i = 0; i < 3; i += 1) { - const randomIndex = Math.floor(Math.random() * projectsCopy.length) - const randomProject = projectsCopy.splice(randomIndex, 1)[0] - //add a property that is a snippet of the original description before pushing to the array - projectSelection.push({ - ...randomProject, - }) - } + const [newsArray, projects] = await Promise.all([ + fetchHomeNews(), + fetchActiveStrapiProjects(), + ]); + + let projectsCopy = [...projects] + let projectSelection = [] + for (let i = 0; i < 3; i += 1) { + const randomIndex = Math.floor(Math.random() * projectsCopy.length) + const randomProject = projectsCopy.splice(randomIndex, 1)[0] + //add a property that is a snippet of the original description before pushing to the array + projectSelection.push({ + ...randomProject, + }) + } - return { - props: { selectedProjects: JSON.parse(JSON.stringify(projectSelection)) }, + return { + props: { + selectedProjects: JSON.parse(JSON.stringify(projectSelection)), + newsArray: JSON.parse(JSON.stringify(newsArray)) + }, + } + } catch (error) { + console.error('Error fetching data:', error); + return { + props: { selectedProjects: [], newsArray: [] } + }; } }