77import ButtonLink from 'components/ButtonLink' ;
88import Container from 'components/Container' ;
99import Flex from 'components/Flex' ;
10- import mountCodeExample from 'utils/mountCodeExample ' ;
10+ import CodeExample from 'components/CodeExample ' ;
1111import PropTypes from 'prop-types' ;
1212import React , { Component } from 'react' ;
1313import { graphql } from 'gatsby' ;
1414import TitleAndMetaTags from 'components/TitleAndMetaTags' ;
1515import Layout from 'components/Layout' ;
16+ import loadScript from 'utils/loadScript' ;
1617import { colors , media , sharedStyles } from 'theme' ;
1718import createOgUrl from 'utils/createOgUrl' ;
18- import loadScript from 'utils/loadScript' ;
1919import { babelURL } from 'site-constants' ;
2020import ReactDOM from 'react-dom' ;
2121import logoWhiteSvg from 'icons/logo-white.svg' ;
2222
2323class Home extends Component {
24- constructor ( props , context ) {
25- super ( props , context ) ;
26-
27- const { data} = props ;
28-
29- const code = data . code . edges . reduce ( ( map , { node} ) => {
30- map [ node . id ] = JSON . parse ( node . internal . contentDigest ) ;
31-
32- return map ;
33- } , { } ) ;
34-
35- const examples = data . examples . edges . map ( ( { node} ) => ( {
36- content : node . html ,
37- id : node . fields . slug . replace ( / ^ .+ \/ / , '' ) . replace ( '.html' , '' ) ,
38- title : node . frontmatter . title ,
39- } ) ) ;
40-
41- const marketing = data . marketing . edges . map ( ( { node} ) => ( {
42- title : node . frontmatter . title ,
43- content : node . html ,
44- } ) ) ;
45-
46- this . state = {
47- code,
48- examples,
49- marketing,
50- } ;
51- }
24+ state = {
25+ babelLoaded : false
26+ } ;
5227
5328 componentDidMount ( ) {
54- const { code, examples} = this . state ;
55-
56- examples . forEach ( ( { id} ) => {
57- renderExamplePlaceholder ( id ) ;
58- } ) ;
59-
60- function mountCodeExamples ( ) {
61- examples . forEach ( ( { id} ) => {
62- mountCodeExample ( id , code [ id ] ) ;
29+ loadScript ( babelURL ) . then ( ( ) => {
30+ this . setState ( {
31+ babelLoaded : true
6332 } ) ;
64- }
65-
66- loadScript ( babelURL ) . then ( mountCodeExamples , error => {
33+ } , error => {
6734 console . error ( 'Babel failed to load.' ) ;
68-
69- mountCodeExamples ( ) ;
7035 } ) ;
7136 }
72-
7337 render ( ) {
74- const { examples, marketing} = this . state ;
75- const { location} = this . props ;
38+ const { babelLoaded} = this . state ;
39+ const { data, location} = this . props ;
40+ const { examples, marketing} = data ;
7641
7742 return (
7843 < Layout location = { location } >
@@ -217,7 +182,7 @@ class Home extends Component {
217182 whiteSpace : 'nowrap' ,
218183 } ,
219184 } } >
220- { marketing . map ( ( column , index ) => (
185+ { marketing . edges . map ( ( { node : column } , index ) => (
221186 < div
222187 key = { index }
223188 css = { {
@@ -266,9 +231,9 @@ class Home extends Component {
266231 } ,
267232 } ,
268233 ] } >
269- { column . title }
234+ { column . frontmatter . title }
270235 </ h3 >
271- < div dangerouslySetInnerHTML = { { __html : column . content } } />
236+ < div dangerouslySetInnerHTML = { { __html : column . html } } />
272237 </ div >
273238 ) ) }
274239 </ div >
@@ -283,7 +248,7 @@ class Home extends Component {
283248 />
284249 < section css = { sectionStyles } >
285250 < div id = "examples" >
286- { examples . map ( ( example , index ) => (
251+ { examples . edges . map ( ( { node } , index ) => (
287252 < div
288253 key = { index }
289254 css = { {
@@ -297,11 +262,12 @@ class Home extends Component {
297262 marginTop : 80 ,
298263 } ,
299264 } } >
300- < h3 css = { headingStyles } > { example . title } </ h3 >
301- < div
302- dangerouslySetInnerHTML = { { __html : example . content } }
303- />
304- < div id = { example . id } />
265+ < CodeExample code = { node . code } loaded = { babelLoaded } >
266+ < h3 css = { headingStyles } > { node . frontmatter . title } </ h3 >
267+ < div
268+ dangerouslySetInnerHTML = { { __html : node . html } }
269+ />
270+ </ CodeExample >
305271 </ div >
306272 ) ) }
307273 </ div >
@@ -339,7 +305,6 @@ class Home extends Component {
339305
340306Home . propTypes = {
341307 data : PropTypes . shape ( {
342- code : PropTypes . object . isRequired ,
343308 examples : PropTypes . object . isRequired ,
344309 marketing : PropTypes . object . isRequired ,
345310 } ) . isRequired ,
@@ -382,22 +347,13 @@ const CtaItem = ({children, primary = false}) => (
382347
383348export const pageQuery = graphql `
384349 query IndexMarkdown {
385- code: allExampleCode {
386- edges {
387- node {
388- id
389- internal {
390- contentDigest
391- }
392- }
393- }
394- }
395350 examples: allMarkdownRemark(
396351 filter: {fileAbsolutePath: {regex: "//home/examples//"}}
397352 sort: {fields: [frontmatter___order], order: ASC}
398353 ) {
399354 edges {
400355 node {
356+ code
401357 fields {
402358 slug
403359 }
0 commit comments