Skip to content

Commit 1ea2623

Browse files
committed
Slight refactor
1 parent e49f6c1 commit 1ea2623

File tree

10 files changed

+57
-81
lines changed

10 files changed

+57
-81
lines changed

src/components/FAQLayout/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import FAQSection from "../FAQSection"
33

44
interface Props {
55
title: string
6-
permalink: string
7-
questions: string
86
rawMarkdownBody: string
97
pageContext: any
108
}
119

1210
const index = ({
1311
title,
14-
permalink,
15-
questions,
1612
rawMarkdownBody,
1713
pageContext
1814
}: Props) => {
@@ -21,8 +17,6 @@ const index = ({
2117
<div className="documentationContent">
2218
<FAQSection
2319
title={title}
24-
permalink={permalink}
25-
questions={questions}
2620
rawMarkdownBody={rawMarkdownBody}
2721
pageContext={pageContext}
2822
/>

src/components/FAQSection/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Marked from "../Marked"
33

44
interface Props {
55
title: string
6-
permalink: string
76
rawMarkdownBody: string
87
pageContext: any
98
}

src/content/faq/BestPractices.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: Best Practices
33
layout: faq
44
permalink: /faq/#best-practices
5-
questions: Is GraphQL scalable?,Does GraphQL support offline usage?,What are the security concerns with GraphQL?,How can I set up authorization with GraphQL?,How does authentication work with GraphQL?,Is GraphQL the right fit for designing a microservice architecture?,How does versioning work in GraphQL?,How can I document my GraphQL API?
65
position: 3
76
---
87

src/content/faq/Frontend.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: Frontend
33
layout: faq
44
permalink: /faq/#frontend
5-
questions: Does GraphQL replace Redux or other state management libraries?
65
position: 5
76
---
87

src/content/faq/General.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: General
33
layout: faq
44
permalink: /faq/#general
5-
questions: Is GraphQL frontend or backend?,Does GraphQL use HTTP?,How does GraphQL affect my product’s performance?,What is a GraphQL client and why would I use one?,Does GraphQL replace ORMs?,Is GraphQL owned by Facebook?,Who is behind GraphQL?,What is the GraphQL Foundation?
65
position: 2
76
---
87

src/content/faq/GettingStarted.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: Getting Started
33
layout: faq
44
permalink: /faq/#getting-started
5-
questions: Why should I use GraphQL?,Does GraphQL replace REST?,Is GraphQL a database language like SQL?,How can I learn GraphQL?,Is GraphQL only for React or JavaScript developers?
65
position: 1
76
---
87

src/content/faq/Specification.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: Specification
33
layout: faq
44
permalink: /faq/#specification
5-
questions: What is the best way to follow specification releases?,How can I contribute to the GraphQL specification?,Where is the documentation for subscriptions?
65
position: 4
76
---
87

src/pages/faq.tsx

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,11 @@
1-
import React, { useEffect } from "react"
1+
import React from "react"
22
import Layout from "../components/Layout"
33
import FAQSection from "../components/FAQSection"
44
import { graphql } from "gatsby"
5-
6-
export const useAccordion = () => {
7-
const toggleChildrenClass = (element: React.ReactNode) => {
8-
console.log(nextUntil(element, 'h3'))
9-
Array.from(nextUntil(element, 'h3')).map(p =>
10-
p.classList.toggle('show')
11-
);
12-
};
13-
14-
var nextUntil = function (elem: any, selector: string) {
15-
16-
// Setup siblings array
17-
var siblings = [];
18-
19-
// Get the next sibling element
20-
let nextElement = elem.nextElementSibling;
21-
22-
// As long as a sibling exists
23-
while (nextElement) {
24-
25-
// If we've reached our match, bail
26-
if (nextElement.matches(selector)) break;
27-
28-
// Otherwise, push it to the siblings array
29-
siblings.push(nextElement);
30-
31-
// Get the next sibling element
32-
nextElement = nextElement.nextElementSibling;
33-
34-
}
35-
36-
return siblings;
37-
38-
};
39-
40-
useEffect(() => {
41-
const hash = location.hash ? location.hash.split('#')[1] : '';
42-
43-
if (hash) {
44-
const anchor = document && document.getElementById(hash)
45-
const heading: any = anchor && anchor.parentNode;
46-
47-
if (heading) {
48-
heading.classList.toggle('open');
49-
toggleChildrenClass(heading);
50-
}
51-
}
52-
53-
const toggleClasses = (e: any) => {
54-
if (e.target.localName !== 'h3') return;
55-
history.replaceState({}, '', '#' + e.target.getElementsByTagName('a')[0].id);
56-
history.scrollRestoration = 'manual';
57-
58-
e.target.classList.toggle('open');
59-
toggleChildrenClass(e.target);
60-
};
61-
62-
document.addEventListener('click', toggleClasses);
63-
64-
return () => document.removeEventListener('click', toggleClasses);
65-
}, []);
66-
};
5+
import { useFAQAccordion } from "../utils/useFAQAccordion"
676

687
export default ({ pageContext, data }: any) => {
69-
useAccordion()
8+
useFAQAccordion()
709

7110
const sections = data.allMarkdownRemark.edges
7211
.map((e: any) => e.node)
@@ -120,8 +59,6 @@ export const query = graphql`
12059
node {
12160
frontmatter {
12261
title
123-
permalink
124-
questions
12562
position
12663
}
12764
id

src/templates/doc.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const layoutMap: any = {
2121
const Blog = ({ data, pageContext }: Props) => {
2222
const {
2323
doc: {
24-
frontmatter: { title, date, permalink, byline, guestBio, layout, questions },
24+
frontmatter: { title, date, permalink, byline, guestBio, layout },
2525
rawMarkdownBody,
2626
},
2727
nextDoc,
@@ -35,7 +35,6 @@ const Blog = ({ data, pageContext }: Props) => {
3535
permalink={permalink}
3636
byline={byline}
3737
guestBio={guestBio}
38-
questions={questions}
3938
rawMarkdownBody={rawMarkdownBody}
4039
nextDoc={nextDoc}
4140
sideBarData={pageContext.sideBarData}
@@ -56,7 +55,6 @@ export const query = graphql`
5655
guestBio
5756
sublinks
5857
layout
59-
questions
6058
}
6159
id
6260
rawMarkdownBody

src/utils/useFAQAccordion.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useEffect } from "react"
2+
3+
export const useFAQAccordion = () => {
4+
const toggleChildrenClass = (element: React.ReactNode) => {
5+
console.log(nextUntil(element, 'h3'))
6+
Array.from(nextUntil(element, 'h3')).map(p =>
7+
p.classList.toggle('show')
8+
);
9+
};
10+
11+
const nextUntil = (elem: any, selector: string) => {
12+
const siblings = [];
13+
let nextElement = elem.nextElementSibling;
14+
15+
while (nextElement) {
16+
if (nextElement.matches(selector)) break;
17+
18+
siblings.push(nextElement);
19+
20+
nextElement = nextElement.nextElementSibling;
21+
}
22+
23+
return siblings;
24+
25+
};
26+
27+
useEffect(() => {
28+
const hash = location.hash ? location.hash.split('#')[1] : '';
29+
30+
if (hash) {
31+
const anchor = document && document.getElementById(hash)
32+
const heading: any = anchor && anchor.parentNode;
33+
34+
if (heading) {
35+
heading.classList.toggle('open');
36+
toggleChildrenClass(heading);
37+
}
38+
}
39+
40+
const toggleClasses = (e: any) => {
41+
if (e.target.localName !== 'h3') return;
42+
history.replaceState({}, '', '#' + e.target.getElementsByTagName('a')[0].id);
43+
history.scrollRestoration = 'manual';
44+
45+
e.target.classList.toggle('open');
46+
toggleChildrenClass(e.target);
47+
};
48+
49+
document.addEventListener('click', toggleClasses);
50+
51+
return () => document.removeEventListener('click', toggleClasses);
52+
}, []);
53+
};

0 commit comments

Comments
 (0)