Skip to content

Commit 14928c9

Browse files
committed
Add VIEWED_SECTION_MUTATION
1 parent 0dd0d08 commit 14928c9

File tree

1 file changed

+98
-46
lines changed

1 file changed

+98
-46
lines changed

src/components/Section.js

Lines changed: 98 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,108 @@
1-
import React from 'react'
1+
import React, { Component } from 'react'
22
import Skeleton from 'react-loading-skeleton'
33
import PropTypes from 'prop-types'
4-
import { Query } from 'react-apollo'
4+
import { Query, Mutation } from 'react-apollo'
55
import gql from 'graphql-tag'
66
import { withRouter } from 'react-router'
77
import get from 'lodash/get'
88

99
import { deslugify } from '../lib/helpers'
1010

11-
const Section = ({ loading, section, chapter }) => {
12-
let headerContent = null,
13-
sectionContent = null
11+
class Section extends Component {
12+
lastSectionId = null
1413

15-
if (loading) {
16-
headerContent = (
17-
<h1>
18-
<Skeleton />
19-
</h1>
20-
)
21-
sectionContent = <Skeleton count={7} />
22-
} else if (!section) {
23-
headerContent = (
24-
<h1>
25-
<span role="img" aria-label="magnifying glass">
26-
🔍
27-
</span>{' '}
28-
404 page not found
29-
</h1>
30-
)
31-
} else {
32-
if (chapter.number !== null) {
14+
componentWillReceiveProps(props) {
15+
if (!props.section) {
16+
return
17+
}
18+
19+
const sectionChanged = props.section.id !== this.lastSectionId
20+
21+
if (sectionChanged) {
22+
setTimeout(() => {
23+
props.viewedSection({
24+
variables: { id: props.section.id }
25+
})
26+
}, 2000)
27+
this.lastSectionId = props.section.id
28+
}
29+
}
30+
31+
render() {
32+
const { loading, section, chapter } = this.props
33+
let headerContent = null,
34+
sectionContent = null,
35+
footerContent = null
36+
37+
if (loading) {
3338
headerContent = (
34-
<div>
35-
<h1>{section.title}</h1>
36-
<h2>
37-
{'Chapter ' + chapter.number}
38-
<span className="Section-number-divider" />
39-
{'Section ' + section.number}
40-
</h2>
41-
</div>
39+
<h1>
40+
<Skeleton />
41+
</h1>
42+
)
43+
sectionContent = <Skeleton count={7} />
44+
} else if (!section) {
45+
headerContent = (
46+
<h1>
47+
<span role="img" aria-label="magnifying glass">
48+
🔍
49+
</span>{' '}
50+
404 page not found
51+
</h1>
4252
)
4353
} else {
44-
headerContent = <h1>{chapter.title}</h1>
54+
if (chapter.number !== null) {
55+
headerContent = (
56+
<div>
57+
<h1>{section.title}</h1>
58+
<h2>
59+
{'Chapter ' + chapter.number}
60+
<span className="Section-number-divider" />
61+
{'Section ' + section.number}
62+
</h2>
63+
</div>
64+
)
65+
} else {
66+
headerContent = <h1>{chapter.title}</h1>
67+
}
68+
69+
sectionContent = section.content
70+
footerContent = `Viewed ${section.views.toLocaleString()} times`
4571
}
4672

47-
sectionContent = section.content
73+
return (
74+
<section className="Section">
75+
<div className="Section-header-wrapper">
76+
<header className="Section-header">{headerContent}</header>
77+
</div>
78+
<div className="Section-content">{sectionContent}</div>
79+
<footer>{footerContent}</footer>
80+
</section>
81+
)
4882
}
49-
50-
return (
51-
<section className="Section">
52-
<div className="Section-header-wrapper">
53-
<header className="Section-header">{headerContent}</header>
54-
</div>
55-
<div className="Section-content">{sectionContent}</div>
56-
</section>
57-
)
5883
}
5984

6085
Section.propTypes = {
6186
section: PropTypes.shape({
6287
title: PropTypes.string,
6388
number: PropTypes.number,
64-
content: PropTypes.string
89+
content: PropTypes.string,
90+
views: PropTypes.number
6591
}),
6692
chapter: PropTypes.shape({
6793
title: PropTypes.string,
6894
number: PropTypes.number
6995
}),
70-
loading: PropTypes.bool.isRequired
96+
loading: PropTypes.bool.isRequired,
97+
viewedSection: PropTypes.func.isRequired
7198
}
7299

73100
const SECTION_BY_ID_QUERY = gql`
74101
query SectionContent($id: String!) {
75102
section(id: $id) {
103+
id
76104
content
105+
views
77106
}
78107
}
79108
`
@@ -83,7 +112,9 @@ const SECTION_BY_CHAPTER_TITLE_QUERY = gql`
83112
chapterByTitle(title: $title) {
84113
title
85114
section(number: 1) {
115+
id
86116
content
117+
views
87118
}
88119
}
89120
}
@@ -94,14 +125,25 @@ const SECTION_BY_NUMBER_QUERY = gql`
94125
chapterByNumber(number: $chapterNumber) {
95126
number
96127
section(number: $sectionNumber) {
128+
id
97129
number
98130
title
99131
content
132+
views
100133
}
101134
}
102135
}
103136
`
104137

138+
const VIEWED_SECTION_MUTATION = gql`
139+
mutation ViewedSection($id: String!) {
140+
viewedSection(id: $id) {
141+
id
142+
views
143+
}
144+
}
145+
`
146+
105147
const SectionWithData = ({ location: { state, pathname } }) => {
106148
const page = deslugify(pathname)
107149

@@ -113,7 +155,8 @@ const SectionWithData = ({ location: { state, pathname } }) => {
113155
createProps = ({ data, loading }) => ({
114156
section: {
115157
...state.section,
116-
content: get(data, 'section.content')
158+
content: get(data, 'section.content'),
159+
views: get(data, 'section.views')
117160
},
118161
chapter: state.chapter,
119162
loading
@@ -141,7 +184,16 @@ const SectionWithData = ({ location: { state, pathname } }) => {
141184

142185
return (
143186
<Query query={query} variables={variables}>
144-
{queryInfo => <Section {...createProps(queryInfo)} />}
187+
{queryInfo => (
188+
<Mutation mutation={VIEWED_SECTION_MUTATION}>
189+
{viewedSection => (
190+
<Section
191+
{...createProps(queryInfo)}
192+
viewedSection={viewedSection}
193+
/>
194+
)}
195+
</Mutation>
196+
)}
145197
</Query>
146198
)
147199
}

0 commit comments

Comments
 (0)