forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(website): add simple pagination
- Loading branch information
1 parent
a1d6846
commit 53dcc76
Showing
7 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react" | ||
import { Box, Link } from "@chakra-ui/core" | ||
import { Link as GatsbyLink } from "gatsby" | ||
|
||
export const Pagination = ({ previous, next }) => { | ||
return ( | ||
<Box> | ||
<ul> | ||
{previous && ( | ||
<li> | ||
<Link as={GatsbyLink} to={previous.fields.slug}> | ||
{previous.frontmatter.title} | ||
</Link> | ||
</li> | ||
)} | ||
{next && ( | ||
<li> | ||
<Link as={GatsbyLink} to={next.fields.slug}> | ||
{next.frontmatter.title} | ||
</Link> | ||
</li> | ||
)} | ||
</ul> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const _ = require("lodash/fp") | ||
|
||
const compareCollections = ( | ||
{ fields: { collection: a } }, | ||
{ fields: { collection: b } }, | ||
) => { | ||
// comparison when one or both are "main" | ||
if (a === "main" && b === "main") return 0 | ||
if (a === "main" && b !== "main") return -1 | ||
if (a !== "main" && b === "main") return 1 | ||
|
||
// comparisons when neither are "main" | ||
if (a < b) return -1 | ||
if (a > b) return 1 | ||
return 0 | ||
} | ||
|
||
const groupByCollection = _.groupBy("fields.collection") | ||
const orderByOrderThenTitle = _.orderBy( | ||
["frontmatter.order", "frontmatter.title"], | ||
["asc", "asc"], | ||
) | ||
|
||
module.exports.sortPostNodes = nodes => { | ||
const collections = groupByCollection(nodes) | ||
const sortedCollectionNodes = _.values(collections).map(orderByOrderThenTitle) | ||
const flattened = _.flatten(_.values(sortedCollectionNodes)) | ||
const allSorted = flattened.sort(compareCollections) | ||
|
||
return allSorted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { sortPostNodes } = require("./utils") | ||
|
||
test("sortPostNodes", () => { | ||
const nodes = [ | ||
{ frontmatter: { title: "B" }, fields: { collection: "utilities" } }, | ||
{ | ||
frontmatter: { title: "A", order: 1 }, | ||
fields: { collection: "components" }, | ||
}, | ||
{ frontmatter: { title: "B" }, fields: { collection: "components" } }, | ||
{ frontmatter: { title: "C" }, fields: { collection: "components" } }, | ||
{ frontmatter: { title: "B", order: 2 }, fields: { collection: "main" } }, | ||
{ frontmatter: { title: "A" }, fields: { collection: "utilities" } }, | ||
{ frontmatter: { title: "A", order: 1 }, fields: { collection: "main" } }, | ||
] | ||
|
||
const expected = [ | ||
{ frontmatter: { title: "A", order: 1 }, fields: { collection: "main" } }, | ||
{ frontmatter: { title: "B", order: 2 }, fields: { collection: "main" } }, | ||
{ | ||
frontmatter: { title: "A", order: 1 }, | ||
fields: { collection: "components" }, | ||
}, | ||
{ frontmatter: { title: "B" }, fields: { collection: "components" } }, | ||
{ frontmatter: { title: "C" }, fields: { collection: "components" } }, | ||
{ frontmatter: { title: "A" }, fields: { collection: "utilities" } }, | ||
{ frontmatter: { title: "B" }, fields: { collection: "utilities" } }, | ||
] | ||
|
||
expect(sortPostNodes(nodes)).toEqual(expected) | ||
}) |