Skip to content

Commit 7dba4a8

Browse files
committed
Use a local mutation for remembering per-section scroll location
1 parent 598f089 commit 7dba4a8

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

src/components/Section.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Query, Mutation } from 'react-apollo'
55
import gql from 'graphql-tag'
66
import { withRouter } from 'react-router'
77
import get from 'lodash/get'
8+
import debounce from 'lodash/debounce'
89

910
import { deslugify } from '../lib/helpers'
1011

@@ -21,16 +22,44 @@ class Section extends Component {
2122
}, 2000)
2223
}
2324

25+
updateScrollPosition = () => {
26+
window.scrollTo(0, this.props.section.scrollY)
27+
}
28+
2429
componentDidMount() {
2530
this.viewedSection(get(this, 'props.section.id'))
31+
window.addEventListener('scroll', this.handleScroll)
32+
this.updateScrollPosition()
33+
}
34+
35+
componentWillUnmount() {
36+
window.removeEventListener('scroll', this.handleScroll)
2637
}
2738

39+
handleScroll = debounce(() => {
40+
if (this.props.section.scrollY === window.scrollY) {
41+
return
42+
}
43+
44+
this.props.setScrollPosition({
45+
variables: {
46+
id: this.props.section.id,
47+
scrollY: window.scrollY
48+
}
49+
})
50+
}, 1000)
51+
2852
componentDidUpdate(prevProps) {
53+
if (!this.props.section) {
54+
return
55+
}
56+
2957
const { id } = this.props.section
3058
const sectionChanged = get(prevProps, 'section.id') !== id
3159

3260
if (sectionChanged) {
3361
this.viewedSection(id)
62+
this.updateScrollPosition()
3463
}
3564
}
3665

@@ -93,14 +122,16 @@ Section.propTypes = {
93122
title: PropTypes.string,
94123
number: PropTypes.number,
95124
content: PropTypes.string,
96-
views: PropTypes.number
125+
views: PropTypes.number,
126+
scrollY: PropTypes.number
97127
}),
98128
chapter: PropTypes.shape({
99129
title: PropTypes.string,
100130
number: PropTypes.number
101131
}),
102132
loading: PropTypes.bool.isRequired,
103-
viewedSection: PropTypes.func.isRequired
133+
viewedSection: PropTypes.func.isRequired,
134+
setScrollPosition: PropTypes.func.isRequired
104135
}
105136

106137
const SECTION_BY_ID_QUERY = gql`
@@ -109,6 +140,7 @@ const SECTION_BY_ID_QUERY = gql`
109140
id
110141
content
111142
views
143+
scrollY @client
112144
}
113145
}
114146
`
@@ -121,6 +153,7 @@ const SECTION_BY_CHAPTER_TITLE_QUERY = gql`
121153
id
122154
content
123155
views
156+
scrollY @client
124157
}
125158
}
126159
}
@@ -136,6 +169,7 @@ const SECTION_BY_NUMBER_QUERY = gql`
136169
title
137170
content
138171
views
172+
scrollY @client
139173
}
140174
}
141175
}
@@ -150,6 +184,12 @@ const VIEWED_SECTION_MUTATION = gql`
150184
}
151185
`
152186

187+
const SET_SECTION_SCROLL_MUTATION = gql`
188+
mutation SetSectionScroll($id: String!, $scrollY: Int!) {
189+
setSectionScroll(id: $id, scrollY: $scrollY) @client
190+
}
191+
`
192+
153193
const SectionWithData = ({ location: { state, pathname } }) => {
154194
const page = deslugify(pathname)
155195

@@ -162,7 +202,8 @@ const SectionWithData = ({ location: { state, pathname } }) => {
162202
section: {
163203
...state.section,
164204
content: get(data, 'section.content'),
165-
views: get(data, 'section.views')
205+
views: get(data, 'section.views'),
206+
scrollY: get(data, 'section.scrollY')
166207
},
167208
chapter: state.chapter,
168209
loading
@@ -193,10 +234,15 @@ const SectionWithData = ({ location: { state, pathname } }) => {
193234
{queryInfo => (
194235
<Mutation mutation={VIEWED_SECTION_MUTATION}>
195236
{viewedSection => (
196-
<Section
197-
{...createProps(queryInfo)}
198-
viewedSection={viewedSection}
199-
/>
237+
<Mutation mutation={SET_SECTION_SCROLL_MUTATION}>
238+
{setScrollPosition => (
239+
<Section
240+
{...createProps(queryInfo)}
241+
viewedSection={viewedSection}
242+
setScrollPosition={setScrollPosition}
243+
/>
244+
)}
245+
</Mutation>
200246
)}
201247
</Mutation>
202248
)}

src/lib/apollo.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,26 @@ const stateLink = withClientState({
5656
defaults: {
5757
loginInProgress: false
5858
},
59-
resolvers: {}
59+
typeDefs: `
60+
type Query {
61+
loginInProgress: Boolean
62+
}
63+
type Mutation {
64+
setSectionScroll(id: String!, scrollY: Int!): Boolean
65+
}
66+
`,
67+
resolvers: {
68+
Section: {
69+
scrollY: () => 0
70+
},
71+
Mutation: {
72+
setSectionScroll: (_, { id, scrollY }, { cache, getCacheKey }) => {
73+
const cacheKey = getCacheKey({ __typename: 'Section', id })
74+
cache.writeData({ id: cacheKey, data: { scrollY } })
75+
return true
76+
}
77+
}
78+
}
6079
})
6180

6281
const link = ApolloLink.from([errorLink, stateLink, networkLink])

0 commit comments

Comments
 (0)