Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import React from "react"
  2. import { StaticQuery, graphql, Link } from "gatsby"
  3. import styled from "styled-components"
  4. import { Flex } from "@rebass/grid"
  5.  
  6. const PrevNext = props => {
  7. const { data, slug, category } = props
  8. const getAllPages = data.allMarkdownRemark.edges
  9. const pages = getAllPages
  10. .filter(page => page.node.frontmatter.category === category)
  11. .sort((a, b) =>
  12. a.node.frontmatter.title > b.node.frontmatter.title
  13. ? 1
  14. : b.node.frontmatter.title > a.node.frontmatter.title
  15. ? -1
  16. : 0
  17. )
  18. const activePage = pages.findIndex(page => page.node.fields.slug === slug)
  19. const prev = activePage !== 0
  20. const next = activePage < pages.length - 1
  21. return (
  22. <Buttons
  23. as="ul"
  24. alignItems="center"
  25. justifyContent="center"
  26. px={[3, 4]}
  27. py={[3, 4]}
  28. mt={0}
  29. >
  30. {prev && (
  31. <Button type="previous" to={pages[activePage - 1].node.fields.slug}>
  32. <SubHeading>Prev</SubHeading>
  33. <Title>{pages[activePage - 1].node.frontmatter.title}</Title>
  34. </Button>
  35. )}
  36. {next && (
  37. <Button type="next" to={pages[activePage + 1].node.fields.slug}>
  38. <SubHeading>Next</SubHeading>
  39. <Title>{pages[activePage + 1].node.frontmatter.title}</Title>
  40. </Button>
  41. )}
  42. </Buttons>
  43. )
  44. }
  45.  
  46. export default props => {
  47. return (
  48. <StaticQuery
  49. query={query}
  50. render={data => (
  51. <PrevNext data={data} slug={props.slug} category={props.category} />
  52. )}
  53. />
  54. )
  55. }
  56.  
  57. const query = graphql`
  58. query {
  59. allMarkdownRemark(
  60. sort: { order: ASC, fields: [frontmatter___title] }
  61. limit: 1000
  62. ) {
  63. edges {
  64. node {
  65. fields {
  66. slug
  67. }
  68. frontmatter {
  69. title
  70. category
  71. }
  72. }
  73. }
  74. }
  75. }
  76. `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement