gatsby-attainlabs-cms 1.0.24 → 1.0.26

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 +105 -70
  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: "Blog",
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;
@@ -384,64 +426,57 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
384
426
  );
385
427
  const firebaseJson = await firebaseData.json();
386
428
  for (const page of Object.values(firebaseJson)) {
387
- if (page.template === "visual-editor") {
429
+ if (page.template === "visual-editor" && page.published) {
388
430
  const {
389
- draft: {
390
- blocks: {
391
- content,
392
- root: {
393
- props: { pageUrl, layout },
394
- },
431
+ blocks: {
432
+ content,
433
+ root: {
434
+ props: { pageUrl, layout },
395
435
  },
396
436
  },
397
437
  id,
398
- published,
399
438
  } = page;
400
439
 
401
- if (published) {
402
- //Generate page's blocks slices
403
- await Promise.all(
404
- content.map(async (b) => {
405
- const name = b.props.component.name;
406
- const sliceId = `block--${pageUrl}--${name}--${id}--${crypto.randomUUID()}`;
407
- // console.log(`Creating slice: ${sliceId}`);
408
- // here we could await something per slice if needed later
409
- createSlice({
410
- id: sliceId,
411
- component: path.resolve(
412
- siteRoot,
413
- "src/cms/components/sliceWrapper.tsx"
414
- ),
415
- context: {
416
- componentPath: `./src/cms/components${b.props.component.path}/index.tsx`,
417
- ...b.props,
418
- },
419
- });
420
- b["sliceId"] = sliceId; // attach sliceId to block for later use
421
- return b;
422
- })
423
- );
424
-
425
- const pageSource = await generatePage(content, layout);
426
- const outPath = path.join(siteRoot, "src/cms/pages", `${pageUrl}.tsx`);
427
-
428
- await fse.outputFile(outPath, pageSource);
429
- await createPage({
430
- path: pageUrl === "index" ? `/` : `/${pageUrl}`,
431
- component: outPath, // ✅ a file path string
432
- context: {
433
- ...page
434
- },
435
- });
436
-
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
+ );
437
462
 
438
- // const pageSource = await generatePage(content, pageUrl);
439
- // const outPath = path.join(siteRoot, "src/pages", `${pageUrl}.tsx`);
440
- // // Generate the page itself
441
- // await fse.outputFile(outPath, pageSource);
442
- // console.log(`✅ Wrote page to ${outPath}`);
443
- }
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}`);
444
480
  }
445
481
  }
446
482
  };
447
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gatsby-attainlabs-cms",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",