This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
187 lines (177 loc) · 5.54 KB
/
gatsby-node.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Copyright (c) 2021 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
/* eslint-disable no-param-reassign */
const path = require('path');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const pageTemplate = path.resolve(
'src/templates/pageTemplate.jsx',
);
const peopleTemplate = path.resolve(
'src/templates/peopleTemplate.jsx',
);
/* Query for all date used in creating pages.
* minimalMain: Query to find starting data structure (number of pages, etc...)
* allTextData: Query to gather all displayed text information for all pages
* allImageData: Query to gather all displayed images for all pages
*/
return graphql(`{
minimalMain: oceAsset(slug: {eq: "minimalmaingraphql"}) {
description
oceType
oceFields {
pages {
fields {
sections {
name
id
type
typeCategory
}
}
name
slug
}
footerlogo {
id
name
slug
}
headerlogo {
id
name
slug
}
}
slug
}
allTextData: allOceAsset {
nodes {
sections {
name
fields {
type
body
heading
image {
name
id
}
actions {
name
link
}
}
id
}
}
}
allImageData: allOceAsset(filter: {staticURL: {ne: null}}) {
nodes {
name
oceId
staticURL
}
}
}`).then((result) => {
if (result.errors) {
throw result.errors;
}
const minimalMainPagesResult = result.data.minimalMain.oceFields;
const allTextDataResult = result.data.allTextData;
const allImageDataResult = result.data.allImageData;
/* Populates minimalMainPageResult with proper data such as:
* 1 - All text data associated with each section on the page
* 2 - All image data associated with each section on the page (if exists)
*
* Code loops through all pages, then sections, then all text data to find matching
* text based on ID. An image search is also performed if an image should exist with the
* section. The text data and image data (if applicable) is associated with the main data
* structure of the section, which is tied to a page, which are all tied to the variable
* minimalMainPageResult.
*/
minimalMainPagesResult.pages.forEach((page) => {
if (page.slug !== 'people') {
page.fields.sections.forEach((sectionPage) => {
allTextDataResult.nodes.forEach((sectionTexts) => {
if (sectionTexts.sections !== null) {
sectionTexts.sections.forEach((sectionText) => {
if (sectionText.id === sectionPage.id) {
sectionPage.textData = sectionText.fields;
if (sectionText.fields.image !== null) {
allImageDataResult.nodes.forEach((image) => {
if ((image.name)
.localeCompare(sectionText.fields.image.name) === 0) {
sectionPage.imageDir = image.staticURL;
}
});
} else {
sectionPage.imageName = null;
}
}
});
}
});
});
}
});
/* Creates variable allPageNameHeader for all possible pages in the header menu
* Structured as [PAGE NAME, PAGE SLUG]
*/
const allPageNameHeader = [];
minimalMainPagesResult.pages.forEach((page) => {
allPageNameHeader.push([page.name, page.slug]);
});
/* Finds header and footer image URLs to populate in each page.
*/
const headerDir = allImageDataResult.nodes.filter((image) => (image.name)
.localeCompare(minimalMainPagesResult.headerlogo.name) === 0);
const footerDir = allImageDataResult.nodes.filter((image) => (image.name)
.localeCompare(minimalMainPagesResult.footerlogo.name) === 0);
/* Populated each page with similar header and footer information
* Information includes header image, header menu, footer image, and metadata
*/
minimalMainPagesResult.pages.forEach((page) => {
page.headerAllPages = allPageNameHeader;
page.headerDir = headerDir[0].staticURL;
page.footerDir = footerDir[0].staticURL;
});
/* Creates all the pages using pageTemplate.jsx for each page in minimalMainPageResult
* Path dictated by slug (home = '/', else path is slug)
*/
minimalMainPagesResult.pages.forEach((page) => {
if (page.slug.localeCompare('home') === 0) {
createPage({
path: '/',
component: pageTemplate,
context: {
page,
buildTag: process.env.BUILD_TAG,
},
});
} else if (page.slug.localeCompare('people') === 0) {
createPage({
path: '/people',
component: peopleTemplate,
context: {
page,
slug: 'people',
channelToken: process.env.CHANNEL_TOKEN,
buildTag: process.env.BUILD_TAG,
},
});
} else {
createPage({
path: `/${page.slug}`,
component: pageTemplate,
context: {
page,
buildTag: process.env.BUILD_TAG,
},
});
}
});
});
};