gatsby-attainlabs-cms 1.0.25 → 1.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/gatsby-node.js +110 -71
  2. package/package.json +1 -1
package/gatsby-node.js CHANGED
@@ -12,7 +12,7 @@ const brands = {
12
12
  "Attain Finance": "attainfinance",
13
13
  };
14
14
 
15
- const generatePage = async (blocks, layout) => {
15
+ const generatePage = async (blocks, layout) => {
16
16
  // Validate input parameters
17
17
  if (!Array.isArray(blocks)) {
18
18
  throw new Error("Invalid parameters passed to createPage.");
@@ -20,12 +20,7 @@ const generatePage = async (blocks, layout) => {
20
20
 
21
21
  // Helper function to generate Slice components
22
22
  const generateSlices = (blocks) => {
23
- return blocks
24
- .map(
25
- (b) =>
26
- `<Slice alias="${b.sliceId}" />`
27
- )
28
- .join("\n");
23
+ return blocks.map((b) => `<Slice alias="${b.sliceId}" />`).join("\n");
29
24
  };
30
25
 
31
26
  // Generate the page content
@@ -38,18 +33,16 @@ const generatePage = async (blocks, layout) => {
38
33
 
39
34
 
40
35
  export const Head = ({ pageContext }: any) => {
41
- const blocks = pageContext.blocks
42
- ? pageContext.blocks
43
- : pageContext.draft.blocks;
36
+ const blocks = pageContext.blocks || { root: { props: { meta: {} } } };
44
37
  const { title, description } = blocks.root.props.meta;
45
38
  return <SEO title={title} description={description} noIndex={true} />;
46
39
  };
47
40
 
48
41
  const Page = ({pageContext}:any ) => {
49
42
  return (
50
- <Layout>
43
+ <>
51
44
  ${generateSlices(blocks)}
52
- </Layout>
45
+ </>
53
46
  );
54
47
  };
55
48
 
@@ -254,12 +247,17 @@ exports.onPreInit = async (_, pluginOptions) => {
254
247
  res.pipe(fileStream);
255
248
  fileStream.on("finish", () => {
256
249
  fileStream.close(() => {
257
- const ext = path.extname(destFile).toLowerCase();
250
+ const ext = path.extname(destFile).toLowerCase();
258
251
  let generatedComment = "";
259
- if (ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx") {
260
- generatedComment =
261
- "// GENERATED FILE // This file is automatically generated by the AttainLabs CMS plugin. Do not edit this file directly.\n";
262
- }
252
+ if (
253
+ ext === ".ts" ||
254
+ ext === ".tsx" ||
255
+ ext === ".js" ||
256
+ ext === ".jsx"
257
+ ) {
258
+ generatedComment =
259
+ "// GENERATED FILE // This file is automatically generated by the AttainLabs CMS plugin. Do not edit this file directly.\n";
260
+ }
263
261
  const fileContent = fs.readFileSync(tmpFile, "utf8");
264
262
  fs.writeFileSync(destFile, generatedComment + fileContent);
265
263
 
@@ -287,7 +285,7 @@ exports.sourceNodes = async (
287
285
  console.log(
288
286
  `ℹ️ [gatsby-attainlabs-cms] Fetching Trustpilot data for brand: ${brand}`
289
287
  );
290
- // // Map brand names to Trustpilot business unit IDs
288
+ // // Map brand names to Trustpilot business unit IDs
291
289
  const businessIds = {
292
290
  LendDirect: "599affea0000ff0005a95acd",
293
291
  "Cash Money": "599afd420000ff0005a95a9d",
@@ -356,7 +354,51 @@ exports.sourceNodes = async (
356
354
  },
357
355
  });
358
356
  }
359
- };
357
+
358
+ // Blogs node creation
359
+ const firebaseData = await fetch(
360
+ `https://attain-finance-cms-default-rtdb.firebaseio.com/cms/brands/lenddirect/pages.json`
361
+ ).then((res) => res.json());
362
+
363
+ const allBlogs = [];
364
+
365
+ for (const [key, value] of Object.entries(firebaseData)) {
366
+ const category =
367
+ value?.draft?.blocks?.root?.props?.category.type ||
368
+ value?.blocks?.root?.props?.category.type;
369
+
370
+ if (category === "blog") {
371
+ allBlogs.push(value);
372
+ }
373
+
374
+ if (value.hasOwnProperty("children")) {
375
+ for (const [childKey, childValue] of Object.entries(value["children"])) {
376
+ const childCategory =
377
+ childValue?.draft?.blocks?.root?.props?.category.type ||
378
+ childValue?.blocks?.root?.props?.category.type;
379
+
380
+ if (childCategory === "blog") {
381
+ allBlogs.push(childValue);
382
+ }
383
+ }
384
+ }
385
+ }
386
+ for (const blog of allBlogs) {
387
+ const id = createNodeId(`Blog-${blog.id}`);
388
+ const node = {
389
+ ...blog,
390
+ id,
391
+ _id: blog.id,
392
+ parent: null,
393
+ children: [],
394
+ internal: {
395
+ type: "Blogs",
396
+ contentDigest: createContentDigest(blog),
397
+ },
398
+ };
399
+ createNode(node);
400
+ }
401
+ };
360
402
  exports.createPages = async ({ actions, store }, pluginOptions) => {
361
403
  const { brand, environment } = pluginOptions;
362
404
  const { createSlice, createPage } = actions;
@@ -385,59 +427,56 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
385
427
  const firebaseJson = await firebaseData.json();
386
428
  for (const page of Object.values(firebaseJson)) {
387
429
  if (page.template === "visual-editor" && page.published) {
388
- const {
389
- blocks: {
390
- content,
391
- root: {
392
- props: { pageUrl, layout },
393
- },
394
- },
395
- id,
396
- } = page;
397
-
398
- //Generate page's blocks slices
399
- await Promise.all(
400
- content.map(async (b) => {
401
- const name = b.props.component.name;
402
- const sliceId = `block--${pageUrl}--${name}--${id}--${crypto.randomUUID()}`;
403
- // console.log(`Creating slice: ${sliceId}`);
404
- // here we could await something per slice if needed later
405
- createSlice({
406
- id: sliceId,
407
- component: path.resolve(
408
- siteRoot,
409
- "src/cms/components/sliceWrapper.tsx"
410
- ),
411
- context: {
412
- componentPath: `./src/cms/components${b.props.component.path}/index.tsx`,
413
- ...b.props,
414
- },
415
- });
416
- b["sliceId"] = sliceId; // attach sliceId to block for later use
417
- return b;
418
- })
419
- );
420
-
421
- const pageSource = await generatePage(content, layout);
422
- const outPath = path.join(siteRoot, "src/cms/pages", `${pageUrl}.tsx`);
423
-
424
- await fse.outputFile(outPath, pageSource);
425
- await createPage({
426
- path: pageUrl === "index" ? `/` : `/${pageUrl}`,
427
- component: outPath, // ✅ a file path string
428
- context: {
429
- ...page
430
+ const {
431
+ blocks: {
432
+ content,
433
+ root: {
434
+ props: { pageUrl, layout },
430
435
  },
431
- });
432
-
433
-
434
- // const pageSource = await generatePage(content, pageUrl);
435
- // const outPath = path.join(siteRoot, "src/pages", `${pageUrl}.tsx`);
436
- // // Generate the page itself
437
- // await fse.outputFile(outPath, pageSource);
438
- // console.log(`✅ Wrote page to ${outPath}`);
439
-
436
+ },
437
+ id,
438
+ } = page;
439
+
440
+ //Generate page's blocks slices
441
+ await Promise.all(
442
+ content.map(async (b) => {
443
+ const name = b.props.component.name;
444
+ const sliceId = `block--${pageUrl}--${name}--${id}--${crypto.randomUUID()}`;
445
+ // console.log(`Creating slice: ${sliceId}`);
446
+ // here we could await something per slice if needed later
447
+ createSlice({
448
+ id: sliceId,
449
+ component: path.resolve(
450
+ siteRoot,
451
+ "src/cms/components/sliceWrapper.tsx"
452
+ ),
453
+ context: {
454
+ componentPath: `./src/cms/components${b.props.component.path}/index.tsx`,
455
+ ...b.props,
456
+ },
457
+ });
458
+ b["sliceId"] = sliceId; // attach sliceId to block for later use
459
+ return b;
460
+ })
461
+ );
462
+
463
+ const pageSource = await generatePage(content, layout);
464
+ const outPath = path.join(siteRoot, "src/cms/pages", `${pageUrl}.tsx`);
465
+
466
+ await fse.outputFile(outPath, pageSource);
467
+ await createPage({
468
+ path: pageUrl === "index" ? `/` : `/${pageUrl}`,
469
+ component: outPath, // ✅ a file path string
470
+ context: {
471
+ ...page,
472
+ },
473
+ });
474
+
475
+ // const pageSource = await generatePage(content, pageUrl);
476
+ // const outPath = path.join(siteRoot, "src/pages", `${pageUrl}.tsx`);
477
+ // // Generate the page itself
478
+ // await fse.outputFile(outPath, pageSource);
479
+ // console.log(`✅ Wrote page to ${outPath}`);
440
480
  }
441
481
  }
442
482
  };
443
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gatsby-attainlabs-cms",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",