|
1 | 1 | import React from 'react'
|
2 | 2 | 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' |
3 | 7 |
|
4 |
| -const Section = ({ loading = true }) => ( |
| 8 | +const Section = ({ |
| 9 | + loading, |
| 10 | + sectionContent, |
| 11 | + location: { state: { chapter, section } } |
| 12 | +}) => ( |
5 | 13 | <section className="Section">
|
6 | 14 | <div className="Section-header-wrapper">
|
7 | 15 | <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 | + )} |
10 | 28 | </header>
|
11 | 29 | </div>
|
12 | 30 | <div className="Section-content">
|
13 |
| - {loading ? <Skeleton count={7} /> : null} |
| 31 | + {loading ? <Skeleton count={7} /> : sectionContent} |
14 | 32 | </div>
|
15 | 33 | </section>
|
16 | 34 | )
|
17 | 35 |
|
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