forked from rage/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseContentTemplate.js
More file actions
140 lines (127 loc) · 3.82 KB
/
CourseContentTemplate.js
File metadata and controls
140 lines (127 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Fragment } from "react"
import { graphql } from "gatsby"
import styled from "styled-components"
import rehypeReact from "rehype-react"
import { navigate, Link } from "gatsby"
import { Helmet } from "react-helmet"
import Layout from "./Layout"
import getNamedPartials from "../partials"
import CoursePageFooter from "../components/CoursePageFooter"
import { getCachedUserDetails } from "../services/moocfi"
import "./remark.css"
import PagesContext from "../contexes/PagesContext"
import LoginStateContext, {
LoginStateContextProvider,
} from "../contexes/LoginStateContext"
import Container from "../components/Container"
import { loggedIn } from "../services/moocfi"
import { capitalizeFirstLetter } from "../util/strings"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faArrowCircleUp as icon } from "@fortawesome/free-solid-svg-icons"
import EndOfSubSection from "../components/EndOfSubSection"
import { tryToScrollToSelector } from "../util/dom"
const StyledIcon = styled(FontAwesomeIcon)`
margin-right: 0.25rem;
font-size: 1em;
`
const ContentWrapper = styled.article``
const UpLink = styled(Link)`
color: #332c2cb3 !important;
font-weight: bold;
margin-bottom: 1rem !important;
display: block;
:hover {
text-decoration: none;
color: #805050b3 !important;
}
`
export default class CourseContentTemplate extends React.Component {
static contextType = LoginStateContext
async componentDidMount() {
if (!loggedIn()) {
return
}
let userInfo = await getCachedUserDetails()
const research = userInfo?.extra_fields?.research
if (research === undefined) {
navigate("/missing-info")
}
if (typeof window !== "undefined" && window.location.hash) {
const selector = window.location.hash
setTimeout(() => {
tryToScrollToSelector(selector)
}, 100)
setTimeout(() => {
tryToScrollToSelector(selector)
}, 500)
setTimeout(() => {
tryToScrollToSelector(selector)
}, 1000)
setTimeout(() => {
tryToScrollToSelector(selector)
}, 2000)
}
}
render() {
const { data } = this.props
const { frontmatter, htmlAst } = data.page
const allPages = data.allPages.edges.map((o) => o.node?.frontmatter)
const partials = getNamedPartials()
const renderAst = new rehypeReact({
createElement: React.createElement,
components: partials,
}).Compiler
const parentSectionName = capitalizeFirstLetter(
`${frontmatter.path.split(/\//g)[1].replace(/-/g, " ")}`,
)
const parentSectionPath = `/${frontmatter.path.split(/\//g)[1]}`
return (
<Fragment>
<Helmet title={frontmatter.title} />
<PagesContext.Provider value={{ all: allPages, current: frontmatter }}>
<LoginStateContextProvider>
<Layout>
<Fragment>
<Container>
<ContentWrapper>
<UpLink to={parentSectionPath}>
<StyledIcon icon={icon} />
{parentSectionName}
</UpLink>
<h1>{frontmatter.title}</h1>
{renderAst(htmlAst)}
<EndOfSubSection />
</ContentWrapper>
</Container>
<CoursePageFooter />
</Fragment>
</Layout>
</LoginStateContextProvider>
</PagesContext.Provider>
</Fragment>
)
}
}
export const pageQuery = graphql`
query ($path: String!) {
page: markdownRemark(frontmatter: { path: { eq: $path } }) {
htmlAst
html
frontmatter {
path
title
}
}
allPages: allMarkdownRemark {
edges {
node {
id
frontmatter {
path
title
}
}
}
}
}
`