|
| 1 | +import React, { Fragment } from 'react' |
| 2 | +import styled from 'styled-components' |
| 3 | +import PagesInThisSection from '../partials/PagesInThisSection' |
| 4 | +import PagesContext from '../contexes/PagesContext' |
| 5 | +import { nthIndex } from '../util/strings' |
| 6 | +import { Link } from 'gatsby' |
| 7 | + |
| 8 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 9 | +import { faArrowRight as icon } from '@fortawesome/free-solid-svg-icons' |
| 10 | + |
| 11 | +const CoursePageFooterWrapper = styled.footer` |
| 12 | + background-color: black; |
| 13 | + color: white; |
| 14 | + padding: 3rem; |
| 15 | +` |
| 16 | + |
| 17 | +const CoursePageFooterContent = styled.div` |
| 18 | + display: flex; |
| 19 | + justify-content: space-between; |
| 20 | + max-width: 960px; |
| 21 | + margin: 0 auto; |
| 22 | +` |
| 23 | + |
| 24 | +const StyledLink = styled(Link)` |
| 25 | + background-color: white; |
| 26 | + color: black; |
| 27 | + text-decoration: none; |
| 28 | + padding: 1rem; |
| 29 | + border-radius: 0.25rem; |
| 30 | + margin: 1rem 0; |
| 31 | + display: inline-block; |
| 32 | + width: 100%; |
| 33 | +
|
| 34 | + &:hover { |
| 35 | + text-decoration: none; |
| 36 | + font-weight: bold; |
| 37 | + color: black; |
| 38 | + } |
| 39 | +` |
| 40 | + |
| 41 | +const StyledIcon = styled(FontAwesomeIcon)` |
| 42 | + vertical-align: middle; |
| 43 | + margin-right: 1rem; |
| 44 | + margin-left: 0.5rem; |
| 45 | + color: var(--color); |
| 46 | + font-size: 1.5em; |
| 47 | +` |
| 48 | + |
| 49 | +const ButtonWrapper = styled.div` |
| 50 | + display: flex; |
| 51 | + align-items: center; |
| 52 | +` |
| 53 | + |
| 54 | +export default class CoursePageFooter extends React.Component { |
| 55 | + render() { |
| 56 | + return ( |
| 57 | + <PagesContext.Consumer> |
| 58 | + {value => { |
| 59 | + const currentPath = value.current.path |
| 60 | + let sectionPath = currentPath |
| 61 | + const sectionSeparator = nthIndex(currentPath, '/', 2) |
| 62 | + if (sectionSeparator !== -1) { |
| 63 | + sectionPath = currentPath.substr(0, sectionSeparator) |
| 64 | + } |
| 65 | + const sectionPages = value.all |
| 66 | + .filter(o => o.path.startsWith(`${sectionPath}/`)) |
| 67 | + .sort((a, b) => { |
| 68 | + a = a.path.toLowerCase() |
| 69 | + b = b.path.toLowerCase() |
| 70 | + |
| 71 | + return a > b ? 1 : b > a ? -1 : 0 |
| 72 | + }) |
| 73 | + |
| 74 | + let currentPageIndex = null |
| 75 | + |
| 76 | + sectionPages.forEach((page, i) => { |
| 77 | + if (page.path !== currentPath) { |
| 78 | + return |
| 79 | + } |
| 80 | + currentPageIndex = i |
| 81 | + }) |
| 82 | + |
| 83 | + let nextPart = null |
| 84 | + |
| 85 | + if ( |
| 86 | + currentPageIndex !== null && |
| 87 | + currentPageIndex !== sectionPages.length - 1 |
| 88 | + ) { |
| 89 | + nextPart = sectionPages[currentPageIndex + 1] |
| 90 | + } |
| 91 | + return ( |
| 92 | + <CoursePageFooterWrapper> |
| 93 | + <CoursePageFooterContent> |
| 94 | + <div> |
| 95 | + Pääsit aliluvun loppuun!{' '} |
| 96 | + {nextPart && ( |
| 97 | + <Fragment> |
| 98 | + Jatka tästä seuraavaan osaan:{' '} |
| 99 | + <ButtonWrapper> |
| 100 | + <StyledLink to={nextPart.path}> |
| 101 | + <StyledIcon icon={icon} /> |
| 102 | + {currentPageIndex + 2}. {nextPart.title} |
| 103 | + </StyledLink> |
| 104 | + </ButtonWrapper> |
| 105 | + </Fragment> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + <PagesInThisSection |
| 109 | + style={{ width: '400px', fontSize: '0.8rem', margin: 0 }} |
| 110 | + /> |
| 111 | + </CoursePageFooterContent> |
| 112 | + </CoursePageFooterWrapper> |
| 113 | + ) |
| 114 | + }} |
| 115 | + </PagesContext.Consumer> |
| 116 | + ) |
| 117 | + } |
| 118 | +} |
0 commit comments