File tree 2 files changed +77
-5
lines changed
2 files changed +77
-5
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 5
5
6
6
<ul >
7
7
<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 >
11
13
</li >
12
14
</ul >
15
+
16
+ <SectionList :id =" currentSection" />
13
17
</template >
14
18
15
19
<script >
16
- import { gql } from ' @apollo/client/core'
17
20
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
18
27
19
28
export default {
20
29
name: ' TableOfContents' ,
30
+ components: {
31
+ SectionList
32
+ },
21
33
setup () {
34
+ const currentSection = ref (PREFACE_ID )
35
+
22
36
const { result , loading , error } = useQuery (gql `
23
37
query ChapterList {
24
38
chapters {
@@ -34,7 +48,9 @@ export default {
34
48
return {
35
49
loading,
36
50
error,
37
- chapters
51
+ chapters,
52
+ currentSection,
53
+ updateCurrentSection : newSection => (currentSection .value = newSection)
38
54
}
39
55
}
40
56
}
You can’t perform that action at this time.
0 commit comments