Skip to content

Commit 7a21c08

Browse files
committed
Add SectionList.vue with second query
1 parent 651aa1a commit 7a21c08

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

src/components/SectionList.vue

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<template>
2+
<h2>Sections</h2>
3+
4+
<div v-if="loading">Loading...</div>
5+
6+
<div v-else-if="error">Error: {{ error.message }}</div>
7+
8+
<div v-else-if="noSections">This chapter has no sections</div>
9+
10+
<ul v-else>
11+
<li v-for="section of sections" :key="section.id">
12+
{{ section.number }}. {{ section.title }}
13+
</li>
14+
</ul>
15+
</template>
16+
17+
<script>
18+
import { useQuery, useResult } from '@vue/apollo-composable'
19+
import { gql } from '@apollo/client/core'
20+
import { computed } from 'vue'
21+
22+
export default {
23+
name: 'SectionList',
24+
props: {
25+
id: {
26+
type: Number,
27+
required: true
28+
}
29+
},
30+
setup(props) {
31+
const { result, loading, error } = useQuery(
32+
gql`
33+
query SectionList($id: Int!) {
34+
chapter(id: $id) {
35+
sections {
36+
id
37+
number
38+
title
39+
}
40+
}
41+
}
42+
`,
43+
props
44+
)
45+
46+
const sections = useResult(result, [], data => data.chapter.sections)
47+
48+
return {
49+
loading,
50+
error,
51+
sections,
52+
noSections: computed(() => sections.value.length === 1)
53+
}
54+
}
55+
}
56+
</script>

src/components/TableOfContents.vue

+21-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,34 @@
55

66
<ul>
77
<li v-for="chapter of chapters" :key="chapter.id">
8-
{{
9-
chapter.number ? `${chapter.number}. ${chapter.title}` : chapter.title
10-
}}
8+
<a @click="updateCurrentSection(chapter.id)">
9+
{{
10+
chapter.number ? `${chapter.number}. ${chapter.title}` : chapter.title
11+
}}
12+
</a>
1113
</li>
1214
</ul>
15+
16+
<SectionList :id="currentSection" />
1317
</template>
1418

1519
<script>
16-
import { gql } from '@apollo/client/core'
1720
import { useQuery, useResult } from '@vue/apollo-composable'
21+
import { gql } from '@apollo/client/core'
22+
import { ref } from 'vue'
23+
24+
import SectionList from './SectionList.vue'
25+
26+
const PREFACE_ID = -2
1827
1928
export default {
2029
name: 'TableOfContents',
30+
components: {
31+
SectionList
32+
},
2133
setup() {
34+
const currentSection = ref(PREFACE_ID)
35+
2236
const { result, loading, error } = useQuery(gql`
2337
query ChapterList {
2438
chapters {
@@ -34,7 +48,9 @@ export default {
3448
return {
3549
loading,
3650
error,
37-
chapters
51+
chapters,
52+
currentSection,
53+
updateCurrentSection: newSection => (currentSection.value = newSection)
3854
}
3955
}
4056
}

0 commit comments

Comments
 (0)