Add dynamic fetching to blog posts and portfolio projects to the index page

This commit is contained in:
Hazem Krimi
2021-01-14 23:43:56 +01:00
parent 69e0801f9f
commit 5eb1d6b6ec
+50 -26
View File
@@ -1,9 +1,30 @@
import React, { FC, useState } from 'react'; import { FC } from 'react';
import { useRouter } from 'next/router';
import { getPortfolioProjects } from '../lib/portfolio';
import { getBlogPosts } from '../lib/blog';
import styled from 'styled-components'; import styled from 'styled-components';
import Hero from '../components/Hero'; import Hero from '../components/Hero';
import Button from '../components/Button'; import Button from '../components/Button';
import Card from '../components/Card'; import Card from '../components/Card';
interface Props {
blogPosts: {
title: string;
author: string;
description: string;
slug: string;
date: string;
tags?: string[];
}[];
portfolioProjects: {
title: string;
description: string;
slug: string;
date: string;
tags?: string[];
}[];
}
const Wrapper = styled.div` const Wrapper = styled.div`
padding: 1rem 0rem; padding: 1rem 0rem;
display: grid; display: grid;
@@ -34,16 +55,12 @@ const Wrapper = styled.div`
.portfolio, .portfolio,
.blog { .blog {
margin: 1rem 0rem; margin: 1rem 0rem;
@media (max-width: 768px) {
margin: 0.5rem 0rem;
}
} }
.projects-wrapper, .projects-wrapper,
.articles-wrapper { .articles-wrapper {
display: grid; display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-auto-rows: minmax(100px, auto); grid-auto-rows: minmax(100px, auto);
align-items: stretch; align-items: stretch;
justify-items: center; justify-items: center;
@@ -51,42 +68,38 @@ const Wrapper = styled.div`
} }
`; `;
const Index: FC = () => { const Index: FC<Props> = ({ blogPosts, portfolioProjects }) => {
const [projects] = useState<{ title: string; description: string }[]>([ const router = useRouter();
{ title: 'Project 1', description: 'Description 1' }
]);
const [articles] = useState<{ title: string; description: string; tags: string[] }[]>([
{ title: 'Article 1', description: 'Description 1', tags: ['Tag 1', 'Tag 2'] },
{ title: 'Article 1', description: 'Description 1', tags: ['Tag 1', 'Tag 2'] },
{ title: 'Article 1', description: 'Description 1', tags: ['Tag 1', 'Tag 2'] },
{
title: 'Article 1',
description: 'Description 1',
tags: ['Tag 1', 'Tag 2', 'Tag 3', 'Tag 4', 'Tag 5', 'Tag 6']
}
]);
return ( return (
<Wrapper> <Wrapper>
<Hero /> <Hero />
<div className='content'> <div className='content'>
<h1>Portfolio</h1> <h1>Portfolio</h1>
<Button className='blue'>See More</Button> <Button className='blue' onClick={() => router.push('/portfolio')}>
See More
</Button>
<div className='portfolio'> <div className='portfolio'>
<div className='projects-wrapper'> <div className='projects-wrapper'>
{projects.length !== 0 ? ( {portfolioProjects.length !== 0 ? (
projects.map((project, index) => <Card {...project} key={index} />) portfolioProjects.map(({ slug, ...rest }) => (
<Card {...rest} key={slug} onClick={() => router.push(`/portfolio/${slug}`)} />
))
) : ( ) : (
<h4>Nothing for now</h4> <h4>Nothing for now</h4>
)} )}
</div> </div>
</div> </div>
<h1>Blog</h1> <h1>Blog</h1>
<Button className='blue'>See More</Button> <Button className='blue' onClick={() => router.push('/blog')}>
See More
</Button>
<div className='blog'> <div className='blog'>
<div className='articles-wrapper'> <div className='articles-wrapper'>
{articles.length !== 0 ? ( {blogPosts.length !== 0 ? (
articles.map((article, index) => <Card {...article} key={index} />) blogPosts.map(({ slug, ...rest }) => (
<Card {...rest} key={slug} onClick={() => router.push(`/blog/${slug}`)} />
))
) : ( ) : (
<h4>Nothing for now</h4> <h4>Nothing for now</h4>
)} )}
@@ -98,3 +111,14 @@ const Index: FC = () => {
}; };
export default Index; export default Index;
export async function getStaticProps() {
const blogPosts = getBlogPosts();
const portfolioProjects = getPortfolioProjects();
return {
props: {
blogPosts,
portfolioProjects
}
};
}