Skip to content

Commit e1c09b2

Browse files
committed
Prefetch section on mouse over
1 parent 7c59701 commit e1c09b2

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/components/Section.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Section.propTypes = {
137137
setScrollPosition: PropTypes.func.isRequired
138138
}
139139

140-
const SECTION_BY_ID_QUERY = gql`
140+
export const SECTION_BY_ID_QUERY = gql`
141141
query SectionContent($id: String!) {
142142
section(id: $id) {
143143
id

src/components/TableOfContents.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import { graphql } from 'react-apollo'
3+
import { graphql, withApollo, compose } from 'react-apollo'
44
import gql from 'graphql-tag'
55
import Skeleton from 'react-loading-skeleton'
66
import { NavLink } from 'react-router-dom'
77
import classNames from 'classnames'
8+
import { ApolloClient } from 'apollo-client'
89

910
import { slugify, withHyphens } from '../lib/helpers'
11+
import { SECTION_BY_ID_QUERY } from './Section'
1012

1113
const LoadingSkeleton = () => (
1214
<div>
@@ -17,7 +19,7 @@ const LoadingSkeleton = () => (
1719
</div>
1820
)
1921

20-
const TableOfContents = ({ chapters, loading }) => (
22+
const TableOfContents = ({ chapters, loading, client }) => (
2123
<nav className="TableOfContents">
2224
{loading ? (
2325
<LoadingSkeleton />
@@ -41,6 +43,14 @@ const TableOfContents = ({ chapters, loading }) => (
4143
const rootPath = location.pathname.split('/')[1]
4244
return rootPath.includes(withHyphens(chapter.title))
4345
}}
46+
onMouseOver={() => {
47+
client.query({
48+
query: SECTION_BY_ID_QUERY,
49+
variables: {
50+
id: chapter.sections[0].id
51+
}
52+
})
53+
}}
4454
>
4555
{chapterIsNumbered && (
4656
<span className="TableOfContents-chapter-number">
@@ -60,6 +70,14 @@ const TableOfContents = ({ chapters, loading }) => (
6070
}}
6171
className="TableOfContents-section-link"
6272
activeClassName="active"
73+
onMouseOver={() => {
74+
client.query({
75+
query: SECTION_BY_ID_QUERY,
76+
variables: {
77+
id: section.id
78+
}
79+
})
80+
}}
6381
>
6482
{section.title}
6583
</NavLink>
@@ -95,7 +113,8 @@ TableOfContents.propTypes = {
95113
).isRequired
96114
}).isRequired
97115
),
98-
loading: PropTypes.bool.isRequired
116+
loading: PropTypes.bool.isRequired,
117+
client: PropTypes.instanceOf(ApolloClient).isRequired
99118
}
100119

101120
const CHAPTER_QUERY = gql`
@@ -120,4 +139,7 @@ const withData = graphql(CHAPTER_QUERY, {
120139
})
121140
})
122141

123-
export default withData(TableOfContents)
142+
export default compose(
143+
withData,
144+
withApollo
145+
)(TableOfContents)

0 commit comments

Comments
 (0)