Skip to content

Commit 6493827

Browse files
authored
PLANET-7727: Clean up the frontendIndex.js file (#2811)
- Clean up the frontendIndex.js file. - Move elements to a new JS file.
1 parent 2db9ad9 commit 6493827

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {createRoot} from 'react-dom/client';
2+
3+
import {TableOfContentsFrontend} from '../../TableOfContents/TableOfContentsFrontend.js';
4+
import {HappyPointFrontend} from '../../HappyPoint/HappyPointFrontend.js';
5+
import {ColumnsFrontend} from '../../Columns/ColumnsFrontend.js';
6+
import {TopicLinkFrontend} from '../../TopicLink/TopicLinkFrontend.js';
7+
import {SecondaryNavigationFrontend} from '../../SecondaryNavigation/SecondaryNavigationFrontend.js';
8+
9+
// Render React components
10+
const COMPONENTS = {
11+
'planet4-blocks/submenu': TableOfContentsFrontend,
12+
'planet4-blocks/happypoint': HappyPointFrontend,
13+
'planet4-blocks/columns': ColumnsFrontend,
14+
'planet4-blocks/topic-link': TopicLinkFrontend,
15+
'planet4-blocks/secondary-navigation': SecondaryNavigationFrontend,
16+
};
17+
18+
export const setupBlockFrontend = () => {
19+
document.querySelectorAll('[data-render]').forEach(
20+
blockNode => {
21+
const blockName = blockNode.dataset.render;
22+
if (!COMPONENTS[blockName]) {
23+
return;
24+
}
25+
26+
const BlockFrontend = COMPONENTS[blockName];
27+
if (!BlockFrontend) {
28+
return;
29+
}
30+
const attributes = JSON.parse(blockNode.dataset.attributes);
31+
32+
if (!blockNode._reactRoot) {
33+
blockNode._reactRoot = createRoot(blockNode);
34+
}
35+
blockNode._reactRoot.render(<BlockFrontend {...attributes.attributes} />);
36+
}
37+
);
38+
39+
};

assets/src/frontendIndex.js

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,12 @@
33
// eslint-disable-next-line import/no-extraneous-dependencies
44
import 'regenerator-runtime/runtime';
55

6-
import {createRoot} from 'react-dom/client';
7-
import {TableOfContentsFrontend} from './blocks/TableOfContents/TableOfContentsFrontend';
8-
import {HappyPointFrontend} from './blocks/HappyPoint/HappyPointFrontend';
9-
import {ColumnsFrontend} from './blocks/Columns/ColumnsFrontend';
10-
import {TopicLinkFrontend} from './blocks/TopicLink/TopicLinkFrontend';
116
import {setupLightboxForImages} from './blocks/components/Lightbox/setupLightboxForImages';
127
import {setupParallax} from './blocks/components/Parallax/setupParallax';
13-
import {SecondaryNavigationFrontend} from './blocks/SecondaryNavigation/SecondaryNavigationFrontend';
14-
15-
// Render React components
16-
const COMPONENTS = {
17-
'planet4-blocks/submenu': TableOfContentsFrontend,
18-
'planet4-blocks/happypoint': HappyPointFrontend,
19-
'planet4-blocks/columns': ColumnsFrontend,
20-
'planet4-blocks/topic-link': TopicLinkFrontend,
21-
'planet4-blocks/secondary-navigation': SecondaryNavigationFrontend,
22-
};
8+
import {setupBlockFrontend} from './blocks/components/BlockFrontend/setupBlockFrontend';
239

2410
document.addEventListener('DOMContentLoaded', () => {
25-
document.querySelectorAll('[data-render]').forEach(
26-
blockNode => {
27-
const blockName = blockNode.dataset.render;
28-
if (!COMPONENTS[blockName]) {
29-
return;
30-
}
31-
32-
const BlockFrontend = COMPONENTS[blockName];
33-
if (!BlockFrontend) {
34-
return;
35-
}
36-
const attributes = JSON.parse(blockNode.dataset.attributes);
37-
const rootElement = createRoot(blockNode);
38-
rootElement.render(<BlockFrontend {...attributes.attributes} />);
39-
}
40-
);
41-
11+
setupBlockFrontend();
4212
setupLightboxForImages();
4313
setupParallax();
4414
});

0 commit comments

Comments
 (0)