/
dynamic-page.tsx
90 lines (82 loc) · 1.95 KB
/
dynamic-page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { lazy, Suspense } from "react";
import { graphql, PageProps } from "gatsby";
import Layout from "../components/Layout";
import { Container } from "../components/UI/Container";
type GraphQLResult = {
contentfulDynamicPage: {
title: string;
pageContent: any;
};
};
const DynamicPage = ({ data }: PageProps<GraphQLResult>) => {
const { pageContent, title } = data.contentfulDynamicPage;
return (
<Layout>
<Container>
<h2>This is the {title} page</h2>
{pageContent.map((content: any) => {
const ComponentToRender = lazy(
() => import(`../components/${content.componentToRender.name}/index`)
);
return (
<div key={content.id}>
<Suspense fallback={<div>Loading...</div>}>
<ComponentToRender content={content} />
<br />
</Suspense>
</div>
);
})}
</Container>
</Layout>
);
};
export default DynamicPage;
export const pageQuery = graphql`
query ContentfulDynamicPageQuery($id: String!) {
contentfulDynamicPage(id: { eq: $id }) {
title
pageContent {
__typename
... on Node {
... on ContentfulBanner {
id
title
copy {
copy
}
backgroundImage {
gatsbyImageData
}
componentToRender {
name
}
}
... on ContentfulFiftyFifty {
id
heading
copy {
copy
}
image {
gatsbyImageData
}
componentToRender {
name
}
}
... on ContentfulPerson {
id
name
image {
gatsbyImageData
}
componentToRender {
name
}
}
}
}
}
}
`;