Skip to content

Commit

Permalink
feat(pagination): enabled pagination logic
Browse files Browse the repository at this point in the history
but there are currently no links between pages
  • Loading branch information
travi committed Jun 16, 2020
1 parent 0972f76 commit 2fcf53e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
65 changes: 45 additions & 20 deletions gatsby/create-pages.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
const path = require('path');

module.exports = ({graphql, actions}) => {
function createMeetingPages(meetings, createPage) {
meetings.forEach(({node}) => {
if (!node.fields || !node.fields.slug) return;

createPage({
path: node.fields.slug,
component: path.resolve('./src/templates/meeting.js'),
context: {slug: node.fields.slug}
});
});
}

function createArchiveListPage(meetings, createPage) {
const meetingsPerPage = 6;
const totalPages = Math.ceil(meetings.length / meetingsPerPage);

Array.from({length: totalPages}).forEach((_, index) => {
createPage({
path: 0 === index ? '/archive' : `/archive/page-${index + 1}`,
component: path.resolve('./src/templates/archive.js'),
context: {
limit: meetingsPerPage,
skip: index * meetingsPerPage,
totalPages,
currentPage: index + 1
}
});
});
}

module.exports = async ({graphql, actions}) => {
const {createPage} = actions;

return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
const result = await graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
`).then(result => {
result.data.allMarkdownRemark.edges.forEach(({node}) => {
if (!node.fields || !node.fields.slug) return;

createPage({
path: node.fields.slug,
component: path.resolve('./src/templates/meeting.js'),
context: {slug: node.fields.slug}
});
});
});
}
`);

const meetings = result.data.allMarkdownRemark.edges;

createMeetingPages(meetings, createPage);
createArchiveListPage(meetings, createPage);
};
8 changes: 5 additions & 3 deletions src/pages/archive/index.js → src/templates/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {arrayOf, shape, string} from 'prop-types';
import {graphql} from 'gatsby';
import {Archive as ArchivePage} from '@dsmjs/components';
import Layout from '../../components/layout';
import Layout from '../components/layout';

export default function Archive({data}) {
const meetings = data.allMarkdownRemark.edges;
Expand Down Expand Up @@ -30,10 +30,12 @@ Archive.propTypes = {
};

export const meetingsQuery = graphql`
query MeetingsQuery {
query MeetingsQuery($skip: Int!, $limit: Int!) {
allMarkdownRemark(
filter:{fields:{type:{eq:"meeting"}}},
sort: { fields: [frontmatter___date], order: DESC }
sort: { fields: [frontmatter___date], order: DESC },
limit: $limit,
skip: $skip
) {
edges {
node {
Expand Down

0 comments on commit 2fcf53e

Please sign in to comment.