Skip to content

Commit

Permalink
Update authorcard.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kailasnadh790 committed Jan 16, 2024
1 parent 8ccd96a commit c79fff4
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions cigaradvisor/blocks/authorcard/authorcard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readBlockConfig, createOptimizedPicture } from '../../scripts/aem.js';
import { isExternal, fetchData } from '../../scripts/scripts.js';
import { isExternal, fetchData, getRelativePath } from '../../scripts/scripts.js';

export default async function decorate(block) {
const configs = readBlockConfig(block);
Expand Down Expand Up @@ -30,42 +30,31 @@ export default async function decorate(block) {

const rightDiv = document.createElement('div');
rightDiv.classList.add('right-column');
// TODO: fetch author details from query-index.xlsx
const fetchUrl = `${window.hlx.codeBasePath}/drafts/Kailas/pagemeta.json`;
const authorContent = await fetchData(fetchUrl);
const authorsObj = [];
[...authors].forEach((authorPage) => {
let authorPath;
try {
const url = new URL(authorPage);
authorPath = url.pathname;
} catch (error) {
authorPath = authorPage;
}
const authorInfo = authorContent.find((obj) => obj.path === authorPath);
if (authorInfo) {
const authorDetails = {
page: authorInfo.path,
name: authorInfo.authorName,
image: authorInfo.authorImage,
};
authorsObj.push(authorDetails);
}
});
const authorWrapperSection = document.createElement('section');
authorWrapperSection.classList.add('author-wrapper');
authorWrapperSection.innerHTML = '';
[...authorsObj].forEach((author) => {
authorWrapperSection.innerHTML
+= `<div class="author-content">
<div class="overlay-image">
${createOptimizedPicture(author.image).outerHTML}
<div class="overlay-content">
<p class="align-center"><a href="${author.page}">${author.name}</a></p>
</div>
</div>
</div>`;
const authorPromises = [...authors].map(async (authorPage) => {
const authorInfo = await fetchData(getRelativePath(authorPage), '/cigaradvisor/author/query-index.json');
if (authorInfo) {
return `<div class="author-content">
<div class="overlay-image">
${createOptimizedPicture(authorInfo.image).outerHTML}
<div class="overlay-content">
<p class="align-center"><a href="${authorInfo.page}">${authorInfo.name}</a></p>
</div>
</div>
</div>`;
}
return ''; // return an empty string if there's no authorInfo
});

Promise.all(authorPromises)
.then((authorContents) => {
authorWrapperSection.innerHTML += authorContents.join('');
})
.catch((error) => {
console.error('Error fetching author info:', error);

Check warning on line 56 in cigaradvisor/blocks/authorcard/authorcard.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
});
rightDiv.replaceChildren(authorWrapperSection);
block.append(rightDiv);
}

0 comments on commit c79fff4

Please sign in to comment.