Skip to content

Commit 082be4c

Browse files
committed
Add SECTION_QUERY for book contents
1 parent 34f3006 commit 082be4c

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/components/Section.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,60 @@
11
import React from 'react'
22
import Skeleton from 'react-loading-skeleton'
3+
import PropTypes from 'prop-types'
4+
import { graphql } from 'react-apollo'
5+
import gql from 'graphql-tag'
6+
import { withRouter } from 'react-router'
37

4-
const Section = ({ loading = true }) => (
8+
const Section = ({
9+
loading,
10+
sectionContent,
11+
location: { state: { chapter, section } }
12+
}) => (
513
<section className="Section">
614
<div className="Section-header-wrapper">
715
<header className="Section-header">
8-
<h1>Title</h1>
9-
<h2>Subtitle</h2>
16+
{chapter.number !== null ? (
17+
<div>
18+
<h1>{section.title}</h1>
19+
<h2>
20+
{'Chapter ' + chapter.number}
21+
<span className="Section-number-divider" />
22+
{'Section ' + section.number}
23+
</h2>
24+
</div>
25+
) : (
26+
<h1>{chapter.title}</h1>
27+
)}
1028
</header>
1129
</div>
1230
<div className="Section-content">
13-
{loading ? <Skeleton count={7} /> : null}
31+
{loading ? <Skeleton count={7} /> : sectionContent}
1432
</div>
1533
</section>
1634
)
1735

18-
export default Section
36+
Section.propTypes = {
37+
sectionContent: PropTypes.string,
38+
location: PropTypes.object.isRequired,
39+
loading: PropTypes.bool.isRequired
40+
}
41+
42+
const SECTION_QUERY = gql`
43+
query SectionContent($id: String!) {
44+
section(id: $id) {
45+
content
46+
}
47+
}
48+
`
49+
50+
const withData = graphql(SECTION_QUERY, {
51+
options: ({ location: { state: { section: { id } } } }) => ({
52+
variables: { id }
53+
}),
54+
props: ({ data: { section, loading } }) => ({
55+
sectionContent: section && section.content,
56+
loading
57+
})
58+
})
59+
60+
export default withRouter(withData(Section))

0 commit comments

Comments
 (0)