Skip to content

Commit

Permalink
move fetchBlockFagDecoreated to utils/loadBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
rl-adobe committed Apr 15, 2024
1 parent c44ca3a commit 3bda145
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
5 changes: 3 additions & 2 deletions express/blocks/template-list/template-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { addAnimationToggle, createOptimizedPicture, linkImage } from '../../scr
import { fetchRelevantRows } from '../../scripts/utils/relevant.js';

import { fixIcons, getIconElement } from '../../scripts/utils/icons.js';
import { addTempWrapperDeprecated, decorateButtonsDeprecated, fetchBlockFragDecorated, decorateSocialIcons } from '../../scripts/utils/decorate.js';
import { addTempWrapperDeprecated, decorateButtonsDeprecated, decorateSocialIcons } from '../../scripts/utils/decorate.js';
import { fetchBlockFragDeprecated } from '../../scripts/utils/loadBlock.js';

import { Masonry } from '../../scripts/widgets/masonry.js';

Expand Down Expand Up @@ -1843,7 +1844,7 @@ async function replaceRRTemplateList(block, props) {
props.viewAllLink = relevantRowsData.viewAllLink || null;

if (relevantRowsData.manualTemplates === 'Y') {
const sectionFromFragment = await fetchBlockFragDecorated(`/express/fragments/relevant-rows/${relevantRowsData.templateFragment}`, 'template-list');
const sectionFromFragment = await fetchBlockFragDeprecated(`/express/fragments/relevant-rows/${relevantRowsData.templateFragment}`, 'template-list');
const newBlock = sectionFromFragment.querySelector('.template-list');

if (newBlock) {
Expand Down
29 changes: 0 additions & 29 deletions express/scripts/utils/decorate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { getLibs } from '../utils.js';
import { getIconElement } from './icons.js';

const { createTag, getConfig } = await import(`${getLibs()}/utils/utils.js`);

// This was only added for the blocks premigration. It is not to be used for new blocks.
export function decorateButtonsDeprecated(el) {
el.querySelectorAll(':scope a').forEach(($a) => {
Expand Down Expand Up @@ -85,32 +82,6 @@ export function normalizeHeadings(block, allowedHeadings) {
});
}

export async function fetchBlockFragDecorated(url, blockName) {
const location = new URL(window.location);
const { prefix } = getConfig().locale;
const fragmentUrl = `${location.origin}${prefix}${url}`;

const path = new URL(fragmentUrl).pathname.split('.')[0];
const resp = await fetch(`${path}.plain.html`);
if (resp.status === 404) {
return null;
}

const html = await resp.text();
const section = createTag('div');
section.innerHTML = html;
section.className = `section section-wrapper ${blockName}-container`;
const block = section.querySelector(`.${blockName}`);
block.dataset.blockName = blockName;
block.parentElement.className = `${blockName}-wrapper`;
block.classList.add('block');
const img = section.querySelector('img');
if (img) {
img.setAttribute('loading', 'lazy');
}
return section;
}

export function decorateSocialIcons($main) {
$main.querySelectorAll(':scope a').forEach(($a) => {
const urlObject = new URL($a.href);
Expand Down
30 changes: 30 additions & 0 deletions express/scripts/utils/loadBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getLibs } from '../utils.js';

const { createTag, getConfig } = await import(`${getLibs()}/utils/utils.js`);

// eslint-disable-next-line import/prefer-default-export
export async function fetchBlockFragDeprecated(url, blockName) {
const location = new URL(window.location);
const { prefix } = getConfig().locale;
const fragmentUrl = `${location.origin}${prefix}${url}`;

const path = new URL(fragmentUrl).pathname.split('.')[0];
const resp = await fetch(`${path}.plain.html`);
if (resp.status === 404) {
return null;
}

const html = await resp.text();
const section = createTag('div');
section.innerHTML = html;
section.className = `section section-wrapper ${blockName}-container`;
const block = section.querySelector(`.${blockName}`);
block.dataset.blockName = blockName;
block.parentElement.className = `${blockName}-wrapper`;
block.classList.add('block');
const img = section.querySelector('img');
if (img) {
img.setAttribute('loading', 'lazy');
}
return section;
}

0 comments on commit 3bda145

Please sign in to comment.